you are here: codestackercodes [RSS]leozera [RSS]

remove SVN control

removes all .svn directories recursively

find . -name .svn -exec rm -rf {} \; 

find . -name .svn -print0 | xargs -0 rm -rf

screenshot file format in mac os

defaults write com.apple.screencapture type jpg

age calculation

# one method

def age
 ((Time.now - birthday)/(60*60*24)/365.2422).to_i
end

# second method

def birthday
    @birthday = Time.local( birth_year, birth_month, birth_day )
  end
  
  def age
    ((Time.now - birthday) / 1.year).to_i
  end 

ie conditional hack

<!--[if IE]>
	<link rel="stylesheet" type="text/css" href="ie-only.css" />
<![endif]-->

# Embedded CSS
<!--[if IE]>
  <style type="text/css">
    #container {font-family: arial,verdana,sans-serif,tahoma;}
  </style>
<![endif]-->

# The opposite technique, targeting only NON-IE browsers:
<!--[if IE]>
	<link rel="stylesheet" type="text/css" href="ie-only.css" />
<![endif]-->

# Target specific versions of IE
# IE 7 ONLY:
<!--[if IE 7]>
	<link href="IE-7-SPECIFIC.css" rel="stylesheet" type="text/css">
<![endif]-->

# IE 6 ONLY:
<!--[if IE 6]>
	<link rel="stylesheet" type="text/css" href="IE-6-SPECIFIC.css" />
<![endif]-->

# IE 5 ONLY:
<!--[if IE 5]>
	<link rel="stylesheet" type="text/css" href="IE-5-SPECIFIC.css" />
<![endif]-->

# IE 5.5 ONLY:
<!--[if IE 5.5000]>
<link rel="stylesheet" type="text/css" href="IE-55-SPECIFIC.css" />
<![endif]-->

# VERSION OF IE VERSION 6 OR LOWER: (I find this one pretty handy)
<!--[if lt IE 7]>
	<link rel="stylesheet" type="text/css" href="IE-6-OR-LOWER-SPECIFIC.css" />
<![endif]-->

popular commands in your shell

my results:

146 svn
91 ./script/server
90 cd
38 ls
19 ssh
18 mate
12 rake
11 sudo
8 ping
8 find

history|awk '{print $2}'|awk 'BEGIN {FS="|"} {print $1}'|sort|uniq -c|sort -rn|head -10

get your mac's serial code

/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Serial Number/ {print $3}'

string manipulation

# using 'insert'


myString = "Paris in Spring" 
myString.insert 8, " the"  # returns "Paris in the Spring"


# using 'gsub' for remove non-alphabetic characters from a string


myString = "Only a test!!!" 
myString.gsub(/[^a-zA-Z|\s]/,'') # returns "Only a test"


# using 'gsub' for search & replace


myString = "I love ASP"
myString.gsub('ASP','Ruby') # returns "I love Ruby"

rake tasks for gem dependencies

rake gems:install # Installs all required gems for this application

rake gems:unpack # Unpacks the specified gem into vendor/gems

show flickr photos with javascript

<script type="text/javascript">
function jsonFlickrFeed(o){
	var i = 0;
	while(o.items[i]){
    	document.write('<img src="' + o.items[i].media.m + '" alt="' + o.items[i].title +'">');
   		i++;
	}
}
</script>
<script src="http://api.flickr.com/services/feeds/photos_public.gne?id=28203925@N08&lang=en-us&format=json" type="text/javascript"></script>

jquery quick start

source: http://www.shafqatahmed.com/2008/08/mybutton-font-f.html

// Makes each odd tr element to have a blue backcolor

$("tr:odd").css("background-color", "blue");


// What if we want to make the header row have a different look ? Show Me

$("#results tr:first").css("background", "black");
$("#results tr:first").css("color", "white");
$("#results tr:first").css("font-weight", "bold");


// We also could have assigned a class name to the header tr element like this 

$("#results tr:first").addClass("header");


//All span that are inside a div element (may not be immediate child of the div) will be selected.

$("div span").css("border", "1px solid");

//Here is how we select the links that are descendent of span which are direct children of a div

$("div > span a")

//Here is how we can select a div with id mydiv 

$("div#mydiv")


// Here is how we select all links under div elements that have css class set to mycss

$("div.mycss a")

// if we want to find a link that points to http://www.google.com then we would write

$("a[href=http://www.google.com]")


// Modifications

$("body").prepend("<img src='banner.jpg' />");
$("div#mydialog").html("<p>Hi There!</p>");
$("#mytable").wrap("<div></div>");


// How to replace all hr elements (horizontal line) with a br element

$("<br/>").replaceAll("hr");

php script for mysql backup

<?php

function backup_tables($host,$user,$pass,$name,$tables = '*'){
	
	$link = mysql_connect($host,$user,$pass);
	mysql_select_db($name,$link);
	
	//get all of the tables
	if($tables == '*')
	{
		$tables = array();
		$result = mysql_query('SHOW TABLES');
		while($row = mysql_fetch_row($result))
		{
			$tables[] = $row[0];
		}
	}
	else
	{
		$tables = is_array($tables) ? $tables : explode(',',$tables);
	}
	
	//cycle through
	foreach($tables as $table)
	{
		$result = mysql_query('SELECT * FROM '.$table);
		$num_fields = mysql_num_fields($result);
		for ($i = 0; $i < $num_fields; $i++) 
		{
			$return.= 'DROP TABLE '.$table.';';
			
			$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
			$return.= "\n\n".$row2[1].";\n\n";
			
			while($row = mysql_fetch_row($result))
			{
				$return.= 'INSERT INTO '.$table.' VALUES(';
				for($j=0; $j<$num_fields; $j++) 
				{
					$row[$j] = addslashes($row[$j]);
					$row[$j] = ereg_replace("\n","\\n",$row[$j]);
					if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
					if ($j<($num_fields-1)) { $return.= ','; }
				}
				$return.= ");\n";
			}
		}
		$return.="\n\n\n";
	}
	
	//save file
	$handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
	fwrite($handle,$return);
	fclose($handle);
}

backup_tables('localhost','username','password','blog');

?>

yahoo! search boss sample

# first, install bossman

gem sources -a http://gems.github.com
gem install jpignata-bossman

# the sample

require 'rubygems'
require 'bossman'
include BOSSMan

BOSSMan.application_id = "Your Application ID here"

news = BOSSMan::Search.news("Mac OS", 0, 3)
news.results.each do |result|
  puts "#{result.title} [from #{result.source}]"
end

another css reset

source: http://meyerweb.com/eric/thoughts/2007/05/01/reset-reloaded/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
	margin: 0;
	padding: 0;
	border: 0;
	outline: 0;
	font-weight: inherit;
	font-style: inherit;
	font-size: 100%;
	font-family: inherit;
	vertical-align: baseline;
}
/* remember to define focus styles! */
:focus {
	outline: 0;
}
body {
	line-height: 1;
	color: black;
	background: white;
}
ol, ul {
	list-style: none;
}
/* tables still need 'cellspacing="0"' in the markup */
table {
	border-collapse: separate;
	border-spacing: 0;
}
caption, th, td {
	text-align: left;
	font-weight: normal;
}
blockquote:before, blockquote:after,
q:before, q:after {
	content: "";
}
blockquote, q {
	quotes: "" "";
}

disable shadow in screen window captures

defaults write com.apple.screencapture disable-shadow -bool true
killall SystemUIServer

yahoo finance sample

require 'yahoo_finance'
data = YahooFinance.quotes(['petr4.sa'], [:ask, :last_trade_price, :last_trade_date])
puts data[0].symbol + " => " + data[0].ask

passing extra parameters to prototype observer handlers

a simple listener: $('myForm').observe('submit', validateMyForm) dont support extra params in the function (in the case, validateMyForm. the solution:

$('myForm').observe('submit', function(event) {  
    validateMyForm(event, param1, param2);  
}); 

Drop all tables in a MySQL Database

run in shell:

echo DROP TABLE `echo 'SHOW TABLES;' | mysql -u USER -p DATABASE | tail -n +2 | sed -e 's/$/,/'` | sed -e 's/,$/;/'

another ie 6 hack

p {			
	background: green !important; /* Major browsers other than IE 6 and below respect the importance immediately */
	background: red; /* IE 6 and below use this value instead, even though the above was marked as important */
}

valid Flash example for XHTML 1.0 Strict (XHTML 1.1)

<object type="application/x-shockwave-flash" data="images/banner.swf" width="288" height="128">
<param name="movie" value="images/banner.swf" />
<img src="banner.gif" width="288" height="128" alt="banner" />
</object>

Add recent application menu to your dock

source: http://brunomiranda.com/past/2008/6/14/add_recent_application_menu_to/

defaults write com.apple.dock persistent-others -array-add '{ "tile-data"  = { "list-type" = 1; }; "tile-type" = "recents-tile"; }'
killall Dock
Displaying Code 241 - 260 of 270 in total