<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>CodeJoust Blog</title>
	
	<link>http://iain.codejoust.com</link>
	<description>Iain's thoughts, ideas, interests from CodeJoust</description>
	<lastBuildDate>Wed, 07 Jul 2010 21:05:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/iain-blog" /><feedburner:info uri="iain-blog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Ubuntu “Mac” Dock</title>
		<link>http://feedproxy.google.com/~r/iain-blog/~3/wAgvc4ApWl4/</link>
		<comments>http://iain.codejoust.com/2010/07/ubuntu-mac-dock/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 21:05:01 +0000</pubDate>
		<dc:creator>Code Dude</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iain.codejoust.com/?p=380</guid>
		<description><![CDATA[Recently, I&#8217;ve been looking around for a good program that replicates the Mac OSX dock. 
The first thing I found was a program called Avant Window Navigator. I installed this and although I was reasonably pleased with it, it lacked some important features. These included easy drag and drop onto and off of the dock [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I&#8217;ve been looking around for a good program that replicates the Mac OSX dock. </p>
<p>The first thing I found was a program called Avant Window Navigator. I installed this and although I was reasonably pleased with it, it lacked some important features. These included easy drag and drop onto and off of the dock and also a quick responding intellihide which hides the bar when a window is over it and then shows it again when you move your mouse to the bottom of the screen. Avant Window Navigator was some reason had buggy support for drag and drop and I also had to wait a second or two for the dock to appear when in intellihide mode-rather annoying. </p>
<p>Then I found Docky which promotes itself as &#8220;The finest dock no money can buy.&#8221; Simply open the Ubuntu Software Center and search for &#8220;Docky&#8221;. Once you have located it click install and you&#8217;re on your way to a better dock. </p>
<p>Unlike AWN (<b>A</b>vant <b>W</b>indow <b>N</b>avigator), Docky has seamless drag and drop support and their intellihide feature is perfect. The instant you hover over the bottom of your screen, up pops Docky&#8230;very time saving. </p>
<p>Docky allows you to customize the dock by giving 6 theme options, 4 dock hiding modes, variable icon sizes and the option to use &#8220;3D Mode&#8221;. </p>
<p>It also comes with what it calls &#8220;Docklet&#8221;-little apps that run in the dock and perform certain tasks such as email alerts, battery moniter, and CPU moniter, among others. </p>
<p>Another thing I found interesting was the ability to add multiple docks with the click of a button. I&#8217;m not sure I&#8217;d ever use that, but an interesting feature none the less. </p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/iain-blog?a=wAgvc4ApWl4:1wCAxIu_UHU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/iain-blog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/iain-blog?a=wAgvc4ApWl4:1wCAxIu_UHU:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/iain-blog?i=wAgvc4ApWl4:1wCAxIu_UHU:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/iain-blog/~4/wAgvc4ApWl4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.codejoust.com/2010/07/ubuntu-mac-dock/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://iain.codejoust.com/2010/07/ubuntu-mac-dock/</feedburner:origLink></item>
		<item>
		<title>Where are you?</title>
		<link>http://feedproxy.google.com/~r/iain-blog/~3/L_wS5QP3-vw/</link>
		<comments>http://iain.codejoust.com/2010/04/whereru/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 14:27:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iain.codejoust.com/?p=367</guid>
		<description><![CDATA[javascript:(function(){var d,s,a,h,gc;d=document;s=function(){gc=google.loader.ClientLocation.address;alert('You\'re in '+gc.city+' '+ gc.region+', '+gc.country+'.'); };if(typeof(google)!==undefined){h=d.getElementsByTagName('head')[0];a=d.createElement('script');a.src='http://www.google.com/jsapi';a.onload=s;h.appendChild(a);}else{s();}})();


Click here to copy the code / bookmarklet.


window.select = function(){ var l = document.getElementById('loader'); l.focus(); l.select(); l.copy(); };
var loader = document.getElementById('loader'); loader.value = document.getElementById('code').innerHTML;
loader.onclick=  function(){ select(); }

Copy and paste the above code into your addressbar, then press enter, and it&#8217;ll tell you where you are.
Try it!
It&#8217;s a [...]]]></description>
			<content:encoded><![CDATA[<pre id="code" lang="javascript">javascript:(function(){var d,s,a,h,gc;d=document;s=function(){gc=google.loader.ClientLocation.address;alert('You\'re in '+gc.city+' '+ gc.region+', '+gc.country+'.'); };if(typeof(google)!==undefined){h=d.getElementsByTagName('head')[0];a=d.createElement('script');a.src='http://www.google.com/jsapi';a.onload=s;h.appendChild(a);}else{s();}})();</pre>
<p><textarea id="loader" style="width:400px; position:relative; height:45px;z-index:400; opacity:0.2;"></textarea><br />
<span style="position:absolute; margin-top:-60px;margin-left:10px;" onclick="select();"><br />
Click here to copy the code / bookmarklet.<br />
</span><br />
<script>
window.select = function(){ var l = document.getElementById('loader'); l.focus(); l.select(); l.copy(); };
var loader = document.getElementById('loader'); loader.value = document.getElementById('code').innerHTML;
loader.onclick=  function(){ select(); }
</script><br />
Copy and paste the above code into your addressbar, then press enter, and it&#8217;ll tell you where you are.<br />
<a href="javascript:eval(document.getElementById('code').innerHTML);">Try it!</a><br />
It&#8217;s a fun prank to scare anyone if you give it to them.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/iain-blog?a=L_wS5QP3-vw:m7s3FxbtXms:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/iain-blog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/iain-blog?a=L_wS5QP3-vw:m7s3FxbtXms:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/iain-blog?i=L_wS5QP3-vw:m7s3FxbtXms:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/iain-blog/~4/L_wS5QP3-vw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.codejoust.com/2010/04/whereru/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://iain.codejoust.com/2010/04/whereru/</feedburner:origLink></item>
		<item>
		<title>nil.</title>
		<link>http://feedproxy.google.com/~r/iain-blog/~3/gh46D-LMN_s/</link>
		<comments>http://iain.codejoust.com/2010/03/nil/#comments</comments>
		<pubDate>Fri, 12 Mar 2010 02:19:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iain.codejoust.com/?p=359</guid>
		<description><![CDATA[hi i'm nil
and not .
but also
not lots
chillnil?
but not
dill nil?
i'm just
nil.
]]></description>
			<content:encoded><![CDATA[<pre>hi i'm nil
and not .
but also
not lots
chillnil?
but not
dill nil?
i'm just
nil.</pre>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/iain-blog?a=gh46D-LMN_s:XA4i0aO4gZo:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/iain-blog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/iain-blog?a=gh46D-LMN_s:XA4i0aO4gZo:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/iain-blog?i=gh46D-LMN_s:XA4i0aO4gZo:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/iain-blog/~4/gh46D-LMN_s" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.codejoust.com/2010/03/nil/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://iain.codejoust.com/2010/03/nil/</feedburner:origLink></item>
		<item>
		<title>Flashcards – Linux</title>
		<link>http://feedproxy.google.com/~r/iain-blog/~3/ZaAVSJ_NvCc/</link>
		<comments>http://iain.codejoust.com/2010/02/flashcards-linux/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 16:36:58 +0000</pubDate>
		<dc:creator>Iain</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iain.codejoust.com/?p=354</guid>
		<description><![CDATA[Here is a small program to help study Spanish words, by iterating them through a screen using a very simple memorization algorithm. (Repeating the 2nd to last seen card)
It will work with any short word-definition memorization.

Download
or
View on GitHub
]]></description>
			<content:encoded><![CDATA[<p>Here is a small program to help study Spanish words, by iterating them through a screen using a very simple memorization algorithm. (Repeating the 2nd to last seen card)</p>
<p>It will work with any short word-definition memorization.</p>
<div class="box" style="width: 300px; margin: 0pt auto; height: 125px; line-height: 30px; font-size: 125%; text-align:center;">
<p><a href="http://github.com/codejoust/SpeedCard/zipball/master">Download</a></p>
<p>or</p>
<p><a href="http://github.com/codejoust/SpeedCard/">View on GitHub</a></div>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/iain-blog?a=ZaAVSJ_NvCc:4K9YCn7atnI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/iain-blog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/iain-blog?a=ZaAVSJ_NvCc:4K9YCn7atnI:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/iain-blog?i=ZaAVSJ_NvCc:4K9YCn7atnI:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/iain-blog/~4/ZaAVSJ_NvCc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.codejoust.com/2010/02/flashcards-linux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://iain.codejoust.com/2010/02/flashcards-linux/</feedburner:origLink></item>
		<item>
		<title>Upcoming Changes</title>
		<link>http://feedproxy.google.com/~r/iain-blog/~3/HB5sqaMytNk/</link>
		<comments>http://iain.codejoust.com/2010/02/upcoming-changes/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 05:47:52 +0000</pubDate>
		<dc:creator>Iain</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iain.codejoust.com/?p=349</guid>
		<description><![CDATA[I&#8217;m considering migrating this single blog into two seperate blogs.
One personal, with more essays, pictures, art, etc., while this one will have more code, technical stuff, ramblings about Google, and the such.
What are your thoughts? I&#8217;ll setup redirects, don&#8217;t worry!
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m considering migrating this single blog into two seperate blogs.</p>
<p>One personal, with more essays, pictures, art, etc., while this one will have more code, technical stuff, ramblings about Google, and the such.</p>
<p>What are your thoughts? I&#8217;ll setup redirects, don&#8217;t worry!</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/iain-blog?a=HB5sqaMytNk:r2ZdxQ6r9Yw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/iain-blog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/iain-blog?a=HB5sqaMytNk:r2ZdxQ6r9Yw:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/iain-blog?i=HB5sqaMytNk:r2ZdxQ6r9Yw:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/iain-blog/~4/HB5sqaMytNk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.codejoust.com/2010/02/upcoming-changes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://iain.codejoust.com/2010/02/upcoming-changes/</feedburner:origLink></item>
		<item>
		<title>Very Basic PHP ORM</title>
		<link>http://feedproxy.google.com/~r/iain-blog/~3/e1UiTfHCr0s/</link>
		<comments>http://iain.codejoust.com/2010/02/very-basic-php-orm/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 05:31:47 +0000</pubDate>
		<dc:creator>Iain</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iain.codejoust.com/?p=345</guid>
		<description><![CDATA[Recently, I&#8217;ve needed to go back to PHP, and decided to forgo an full ORM engine instead to use PHP PDO functions, and a little class trickery.
I wanted something that&#8217;s easy to call, and allows me to setup the database, setup variables, and return an array -> object result.
PHP 5 introduced quite a bit of [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I&#8217;ve needed to go back to PHP, and decided to forgo an full <abbr title="Object Relationship Mapping">ORM</abbr> engine instead to use PHP PDO functions, and a little class trickery.</p>
<p>I wanted something that&#8217;s easy to call, and allows me to setup the database, setup variables, and return an array -> object result.</p>
<p>PHP 5 introduced quite a bit of <abbr title="Object Oriented Programming">OOP</abbr> methods, tricks, and functions. Although PHP still largely is a functional, procedural based programming language, its support for objects is getting slowly better.</p>
<p>Regarding the ORM, I decided to make two classes, (which could be expanded to more, I might end up with two in the final process). One contains a bunch of functions which build <abbr title="Structured Query Language">SQL</abbr> queries from a <code>$args</code> array, and class variables. That way, the queries can be changed in one central place, and can be formulated with the database.</p>
<p>The other class, the database class, takes care of connecting to the database, calling the method within the other class to build the query, execute the query, and return the result as objects in an array.</p>
<p>Pretty simple&#8211; but it took some time to remember all the functions and work around the class structure, Ruby is so much nicer to manipulate classes. PHP lambdas are very poor.</p>
<p>Here is the code for the Database Controller:</p>
<div style="height:350px;overflow:auto;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> DBRunner <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'dbname'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'user'</span><span style="color: #339933;">=&gt;</span>null<span style="color: #339933;">,</span> <span style="color: #0000ff;">'password'</span><span style="color: #339933;">=&gt;</span>null<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$dbdriver</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbdriver</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> connect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        try <span style="color: #009900;">&#123;</span>
             <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbdriver</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PDO<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mysql:host=localhost;dbname='</span><span style="color: #339933;">.</span><span style="color: #000088;">$db</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$db</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> catch<span style="color: #009900;">&#40;</span>PDOException <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
             <span style="color: #b1b100;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Error Connecting to DB: '</span><span style="color: #339933;">+</span><span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Please use for development ONLY. Handle properly in production.</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbdriver</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Closes the PDO Connection</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$method_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">method_exists</span><span style="color: #009900;">&#40;</span>Model<span style="color: #339933;">,</span> <span style="color: #000088;">$method_name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//If our method exists in the Model class</span>
            <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$arg</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//Simple Escaping</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arg</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$arg</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arg</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000088;">$model</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Model<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Make a new 'model' class</span>
            <span style="color: #000088;">$model</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">args</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$args</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Set the args</span>
            <span style="color: #000088;">$sql_string</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$model</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$method_name</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Call the method to get the query</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">statement</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbdriver</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql_string</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Build Results</span>
                <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">statement</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch</span><span style="color: #009900;">&#40;</span>PDO<span style="color: #339933;">::</span><span style="color: #004000;">FETCH_OBJ</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$object</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result</span><span style="color: #339933;">,</span> <span style="color: #000088;">$object</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Return Results</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Database Error - '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Error Debugging - Development</span>
                <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbdriver</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errorInfo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Database Method Not Found'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<p>And example code for the Model class:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Model<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$args</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">function</span> getAllPeople<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         <span style="color: #000088;">$ids</span> <span style="color: #339933;">=</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ids'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;SELECT name, email, pic FROM people WHERE id IN (<span style="color: #006699; font-weight: bold;">{$ids}</span>);&quot;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/iain-blog?a=e1UiTfHCr0s:rxwxhgLiGLY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/iain-blog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/iain-blog?a=e1UiTfHCr0s:rxwxhgLiGLY:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/iain-blog?i=e1UiTfHCr0s:rxwxhgLiGLY:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/iain-blog/~4/e1UiTfHCr0s" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.codejoust.com/2010/02/very-basic-php-orm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://iain.codejoust.com/2010/02/very-basic-php-orm/</feedburner:origLink></item>
		<item>
		<title>Wordpress Quick Tip</title>
		<link>http://feedproxy.google.com/~r/iain-blog/~3/meRc0a2sosw/</link>
		<comments>http://iain.codejoust.com/2010/02/wordpress-quick-tip/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 00:50:19 +0000</pubDate>
		<dc:creator>Code Dude</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[help]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[Websites]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[xhtml]]></category>

		<guid isPermaLink="false">http://iain.codejoust.com/?p=315</guid>
		<description><![CDATA[There are times when you might want to display your blog posts on a page other than the blog page in Wordpress. To successfully do this, requires a short piece of PHP code. For example, you could show your latest work on the homepage on your website. Here&#8217;s the code I used to show my [...]]]></description>
			<content:encoded><![CDATA[<p>There are times when you might want to display your blog posts on a page other than the blog page in Wordpress. To successfully do this, requires a short piece of PHP code. For example, you could show your latest work on the homepage on your website. Here&#8217;s the code I used to show my latest work on a portfolio I was building for myself. </p>
<p>There is however, something else that made this extra hard. I wanted to integrate it with the Jquery <a href="http://fancybox.net/">Fancybox</a> plugin. To do required some extra code that didn&#8217;t make things simpler. </p>
<p>The basic code outline for using Fancybox for your images is this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;a href=&quot;the url to the normal sized version of the image&quot; title=&quot;The name of the image” class=&quot;zoom&quot;&gt;
&lt;img src=&quot;the url to thumbnail sizes version of the image&quot; /&gt;
&lt;/a&gt;</pre></div></div>

<p>This presents some problems with integrating this into Wordpress. Here how I changed it to work with Wordpress posts.</p>
<p><img class="hide" style="display:none;" src="http://iain.codejoust.com/wp-content/uploads/2010/02/php1.png" alt="php" width="556" height="225" class="alignnone size-full wp-image-324" /></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$postslist</span> <span style="color: #339933;">=</span> get_posts<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'numberposts=3&amp;order=ASC&amp;orderby=title'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$postslist</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> 
	setup_postdata<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
	&lt;div class=&quot;project&quot;&gt;
		&lt;a class=&quot;zoom&quot; 
                      rel=&quot;group&quot;
                      title=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;
                      href=&quot;wp-content/themes/starkers/style/images/<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>.png&quot;&gt;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_content<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	        &lt;/a&gt;
	&lt;h4&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/h4&gt; 
	&lt;/div&gt;			
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The first 4 lines are the php that grab the posts. (Note I set it to show the 3 latest posts.)<br />
In the 5th line I enclose each post in a div with an id of &#8220;project&#8221;.<br />
In the 6th line, there are the basic Fancybox classes that make it work in this particular image.<br />
On the 7th line, I set the name of the image to the title of the post<br />
On the 8th line, I set the url of the normal sized version of the image, in this case it was an image in the theme&#8217;s image folder. Notice how I used &#8220;the_title();&#8221; in the url to make it dynamic for each post.<br />
On the 9th line I just set it to grab the content of the post<br />
10th line-closing out the link<br />
On the 11th line I grab the title of the post<br />
And then close the div and end the foreach statement.</p>
<p>There is one thing you have to do manually to make this work. Set a unique category for the posts to be used here. In this case I used the category &#8220;1&#8243;</p>
<p>Also, the body of the post must only have the small version of the image in it. </p>
<p>Pretty cool, heh?</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/iain-blog?a=meRc0a2sosw:xBim889e1X4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/iain-blog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/iain-blog?a=meRc0a2sosw:xBim889e1X4:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/iain-blog?i=meRc0a2sosw:xBim889e1X4:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/iain-blog/~4/meRc0a2sosw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.codejoust.com/2010/02/wordpress-quick-tip/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://iain.codejoust.com/2010/02/wordpress-quick-tip/</feedburner:origLink></item>
		<item>
		<title>The Evolution of a Programmer</title>
		<link>http://feedproxy.google.com/~r/iain-blog/~3/3O03-7fjrXc/</link>
		<comments>http://iain.codejoust.com/2010/02/the-evolution-of-a-programmer/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 02:48:19 +0000</pubDate>
		<dc:creator>Iain</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iain.codejoust.com/?p=308</guid>
		<description><![CDATA[The Original Python Version

In some ways, it shows the differences between the methods and reasons of different programmers.
Personally, I like

def fact&#40;x&#41;:
    return x &#62; 1 and x * fact&#40;x - 1&#41; or 1
print fact&#40;6&#41;

 out of them all. (or would write).
The Lambda version sacrifices clarity in variable names, and understood logic.
The Ruby [...]]]></description>
			<content:encoded><![CDATA[<h3>The Original Python Version</h3>
<div style="height:400px; overflow:scroll;"><script src="http://gist.github.com/289467.js?file=Evolution+of+a+Python+programmer.py"></script></div>
<p>In some ways, it shows the differences between the methods and reasons of different programmers.<br />
Personally, I like</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> fact<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> x <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">1</span> <span style="color: #ff7700;font-weight:bold;">and</span> x <span style="color: #66cc66;">*</span> fact<span style="color: black;">&#40;</span>x - <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #ff4500;">1</span>
<span style="color: #ff7700;font-weight:bold;">print</span> fact<span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span></pre></div></div>

<p> out of them all. (or would write).<br />
The Lambda version sacrifices clarity in variable names, and understood logic.</p>
<h3>The Ruby Version</h3>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># The evolution of a Ruby programmer</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> sum<span style="color:#006600; font-weight:bold;">&#40;</span>list<span style="color:#006600; font-weight:bold;">&#41;</span>
  total = <span style="color:#006666;">0</span>
  <span style="color:#9966CC; font-weight:bold;">for</span> i <span style="color:#9966CC; font-weight:bold;">in</span> 0..<span style="color:#9900CC;">list</span>.<span style="color:#9900CC;">size</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span>
    total = total <span style="color:#006600; font-weight:bold;">+</span> list<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  total
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> sum<span style="color:#006600; font-weight:bold;">&#40;</span>list<span style="color:#006600; font-weight:bold;">&#41;</span>
  total = <span style="color:#006666;">0</span>
  list.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>item<span style="color:#006600; font-weight:bold;">|</span>
    total <span style="color:#006600; font-weight:bold;">+</span>= item
  <span style="color:#9966CC; font-weight:bold;">end</span>
  total
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> sum<span style="color:#006600; font-weight:bold;">&#40;</span>list<span style="color:#006600; font-weight:bold;">&#41;</span>
  total = <span style="color:#006666;">0</span>
  list.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span> total <span style="color:#006600; font-weight:bold;">+</span>= i<span style="color:#006600; font-weight:bold;">&#125;</span>
  total
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> sum<span style="color:#006600; font-weight:bold;">&#40;</span>list<span style="color:#006600; font-weight:bold;">&#41;</span>
  list.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>a,b<span style="color:#006600; font-weight:bold;">|</span> a<span style="color:#006600; font-weight:bold;">+</span>b<span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">Array</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> sum
    inject<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>a,b<span style="color:#006600; font-weight:bold;">|</span> a<span style="color:#006600; font-weight:bold;">+</span>b<span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> sum<span style="color:#006600; font-weight:bold;">&#40;</span>list<span style="color:#006600; font-weight:bold;">&#41;</span>
  list.<span style="color:#9900CC;">reduce</span><span style="color:#006600; font-weight:bold;">&#40;</span>:<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>And, of course, there&#8217;s always using <a href="http://stackoverflow.com/">StackOverflow</a> to find a good technique and build upon it.<br />
For ruby, also, the <a href="http://ruby-doc.org/core/">rubydocs</a> are invaluable for refactoring. Lots of great built-in stuff.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/iain-blog?a=3O03-7fjrXc:m1aCVpniBfs:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/iain-blog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/iain-blog?a=3O03-7fjrXc:m1aCVpniBfs:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/iain-blog?i=3O03-7fjrXc:m1aCVpniBfs:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/iain-blog/~4/3O03-7fjrXc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.codejoust.com/2010/02/the-evolution-of-a-programmer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://iain.codejoust.com/2010/02/the-evolution-of-a-programmer/</feedburner:origLink></item>
		<item>
		<title>Ruby DSL – Fetching a Table</title>
		<link>http://feedproxy.google.com/~r/iain-blog/~3/3m6uoY87IQM/</link>
		<comments>http://iain.codejoust.com/2010/02/ruby-site-table-fetching/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 03:18:47 +0000</pubDate>
		<dc:creator>Iain</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iain.codejoust.com/?p=297</guid>
		<description><![CDATA[Why Ruby Rocks
Ruby is a great programming language for writing DSL languages. It allows for core modifications, and, notably, supports blocks of code to be passed into methods as parameters. This, along with a loose and intuitive syntax, makes it easier to write a clean and easy-to-use interface for a class.
The problem with Tables
Tables are [...]]]></description>
			<content:encoded><![CDATA[<h3>Why Ruby Rocks</h3>
<p><a href="http://www.ruby-lang.org/">Ruby</a> is a great programming language for writing <abbr title="Domain Specific Language">DSL</abbr> languages. It allows for core modifications, and, notably, supports blocks of code to be passed into methods as parameters. This, along with a loose and intuitive syntax, makes it easier to write a clean and easy-to-use interface for a class.</p>
<h3>The problem with Tables</h3>
<p>Tables are often used to show large amounts of information in <abbr title="hypertext markup language">HTML</a>, but they aren&#8217;t easily searchable, combinable, and querable. Tables in a database are all of the above. If a table online could easily be converted to a database table, then it would be easier to analyze, and view over time. Copying the table by hand is impractical, but tables can be parsed by a number of libraries.</p>
<h3>Why Nokogiri Rocks</h3>
<p><a href="http://nokogiri.org/">Nokogiri</a> is Ruby library that parses XML-based formats, including html, quite well.<br />
It allows for xpath expressions, and css expressions. For this implementation of a table parser, I chose to use xpath. It is a more powerful parent of css, and it allows for more complex queries, which make dealing with unmarked and messy html easier. If you don&#8217;t know xpath, a good walk-through can be found at <a href="http://www.w3schools.com/XPath/default.asp">W3schools</a>.</p>
<h3>So, gimme the code</h3>
<p>Here&#8217;s the library code for the parser:<br />
(<a href='http://iain.codejoust.com/wp-content/uploads/2010/02/get-table.rb'>Download</a>)</p>
<div style="height:350px;overflow:auto;">

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/ruby</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'nokogiri'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'open-uri'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'time'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'pp'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> TableParser
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:rows</span>
  <span style="color:#9966CC; font-weight:bold;">alias</span> <span style="color:#ff3333; font-weight:bold;">:to_s</span> <span style="color:#ff3333; font-weight:bold;">:rows</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>doc<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@doc</span> = doc
    <span style="color:#0066ff; font-weight:bold;">@cond</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> go<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">instance_eval</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@output</span> = get_rows
    run_after_hook
    <span style="color:#0066ff; font-weight:bold;">@output</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> to_s
    <span style="color:#0066ff; font-weight:bold;">@output</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> save!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@db</span> = has_db<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> run_after_hook
    <span style="color:#0066ff; font-weight:bold;">@db</span>.<span style="color:#9900CC;">insert</span><span style="color:#006600; font-weight:bold;">&#40;</span>@output<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@db</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> has_db<span style="color:#006600; font-weight:bold;">&#40;</span>db, collection<span style="color:#006600; font-weight:bold;">&#41;</span> 
    <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'mongo'</span>
    <span style="color:#6666ff; font-weight:bold;">Mongo::Connection</span>.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">db</span><span style="color:#006600; font-weight:bold;">&#40;</span>db<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span>collection<span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> get_rows
      <span style="color:#0066ff; font-weight:bold;">@rows</span>.<span style="color:#9900CC;">collect</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>row<span style="color:#006600; font-weight:bold;">|</span>
       row_levels = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
       flag = <span style="color:#0000FF; font-weight:bold;">false</span>
       <span style="color:#0066ff; font-weight:bold;">@cond</span>.<span style="color:#9900CC;">collect</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>name, xpath, block<span style="color:#006600; font-weight:bold;">|</span>
         <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>xpath.<span style="color:#0000FF; font-weight:bold;">nil</span>? <span style="color:#006600; font-weight:bold;">||</span> xpath.<span style="color:#9900CC;">empty</span>?<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> !!block<span style="color:#006600; font-weight:bold;">&#41;</span>
          column = row.<span style="color:#9900CC;">at_xpath</span><span style="color:#006600; font-weight:bold;">&#40;</span>xpath<span style="color:#006600; font-weight:bold;">+</span><span style="color:#996600;">'/text()'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_s</span>.<span style="color:#9900CC;">strip</span>
          flag = <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#9966CC; font-weight:bold;">if</span> column.<span style="color:#9900CC;">empty</span>?
        <span style="color:#9966CC; font-weight:bold;">end</span>
        column = convert_column<span style="color:#006600; font-weight:bold;">&#40;</span>block, column<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> block 
        row_levels<span style="color:#006600; font-weight:bold;">&#91;</span>name<span style="color:#006600; font-weight:bold;">&#93;</span> = column
       <span style="color:#9966CC; font-weight:bold;">end</span>
       <span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#9966CC; font-weight:bold;">if</span> flag
       row_levels
     <span style="color:#9966CC; font-weight:bold;">end</span>.<span style="color:#9900CC;">compact</span>!
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> convert_column<span style="color:#006600; font-weight:bold;">&#40;</span>block, column<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> block.<span style="color:#9900CC;">is_a</span>? <span style="color:#CC0066; font-weight:bold;">String</span>
     column.<span style="color:#9900CC;">instance_eval</span><span style="color:#006600; font-weight:bold;">&#40;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">elsif</span> block.<span style="color:#9900CC;">is_a</span>? <span style="color:#CC0066; font-weight:bold;">Proc</span>
       block.<span style="color:#9900CC;">call</span> column
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#9966CC; font-weight:bold;">case</span> block
        <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#ff3333; font-weight:bold;">:int</span>
          column.<span style="color:#CC0066; font-weight:bold;">gsub!</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#91;</span>^<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          column.<span style="color:#9900CC;">to_i</span>
        <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#ff3333; font-weight:bold;">:float</span>
          column.<span style="color:#CC0066; font-weight:bold;">gsub!</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#91;</span>^<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">9</span>\.<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          column.<span style="color:#9900CC;">to_f</span>
        <span style="color:#9966CC; font-weight:bold;">else</span>
          column
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> using_table<span style="color:#006600; font-weight:bold;">&#40;</span>xpath<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@rows</span> = <span style="color:#0066ff; font-weight:bold;">@doc</span>.<span style="color:#9900CC;">xpath</span><span style="color:#006600; font-weight:bold;">&#40;</span>xpath<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> fetch<span style="color:#006600; font-weight:bold;">&#40;</span>name, location, block = <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@cond</span>.<span style="color:#9900CC;">push</span> <span style="color:#006600; font-weight:bold;">&#91;</span>name, location, block<span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> reject<span style="color:#006600; font-weight:bold;">&#40;</span>xpath, no = <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">eval</span> <span style="color:#996600;">&quot;@rows.reject{|tr|#{no ? '!' : '!!'}tr.at(xpath)&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> TableFetcher
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:table</span>, <span style="color:#ff3333; font-weight:bold;">:doc</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> get_page<span style="color:#006600; font-weight:bold;">&#40;</span>uri<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@table</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    page = <span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>uri<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@doc</span> = <span style="color:#6666ff; font-weight:bold;">Nokogiri::HTML</span><span style="color:#006600; font-weight:bold;">&#40;</span>page<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> get_table<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    table_parser = TableParser.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>@doc<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@table</span>.<span style="color:#9900CC;">push</span> table_parser
    table_parser.<span style="color:#9900CC;">go</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0000FF; font-weight:bold;">__FILE__</span> == $<span style="color:#006666;">0</span>
  <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">exists</span>? ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">'Usage: Use another file to specify rules.'</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">'You can use an argument to include a file.'</span>
  <span style="color:#9966CC; font-weight:bold;">else</span>
    <span style="color:#CC0066; font-weight:bold;">load</span> ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

</div>
<p>It&#8217;s the library that takes your instructions, and provides an interface to Nokogiri, suited to <a href="">mongodb</a> and HTML tables.</p>
<h3>Sample Usage:</h3>
<p>Say, I needed to capture an HTML table to a mongodb database for quick searching, I could use the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'get-table.rb'</span>
&nbsp;
fetcher = TableFetcher.<span style="color:#9900CC;">new</span>
fetcher.<span style="color:#9900CC;">get_page</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'http://www.science.co.il/PTelements.asp'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
elements = fetcher.<span style="color:#9900CC;">get_table</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  fetch <span style="color:#ff3333; font-weight:bold;">:number</span>, <span style="color:#996600;">'td[1]'</span>, <span style="color:#ff3333; font-weight:bold;">:int</span>
  fetch <span style="color:#ff3333; font-weight:bold;">:weight</span>, <span style="color:#996600;">'td[3]'</span>, <span style="color:#ff3333; font-weight:bold;">:float</span>
  fetch <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#996600;">'td[4]'</span>
  fetch <span style="color:#ff3333; font-weight:bold;">:symbol</span>, <span style="color:#996600;">'td[5]'</span>
  fetch <span style="color:#ff3333; font-weight:bold;">:electron_configuration</span>, <span style="color:#996600;">'td[12]'</span>
  fetch <span style="color:#ff3333; font-weight:bold;">:ionization_energy</span>, <span style="color:#996600;">'td[13]'</span>, <span style="color:#ff3333; font-weight:bold;">:float</span>
  using_table <span style="color:#996600;">'//table[@class=&quot;tabint8&quot;]/tr[td[13]]'</span>
  save! <span style="color:#996600;">'chemistry'</span>, <span style="color:#996600;">'periodic_elements'</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
pp elements</pre></div></div>

<h3>What does this do?</h3>
<p>The first line requires the beforementioned <code>get-table.rb</code> library.<br />
The second line is grabs the webpage, and the third and remaining calls the actual parser.<br />
The lines after the <code>get_table do</code> block specify the table, in xpath, and provide types for conversion.</p>
<p>There is some hidden power in the third argument. If you pass the symbols: <code>:float</code>, <code>:int</code>, it&#8217;ll format the database row, or your returned hash, to that format. If it&#8217;s a strong, it&#8217;ll eval that string in the current context. Therefore, providing <code>chomp</code> will result in <code>column.chomp</code>, and leading and trailing whitespace will be removed. If you want to use a block instead, pass a <code>lambda { |column| Time.parse(column) }</code> or a <code>Proc.new { |col| col.split(',') }</code> into the parameter, to format that row.</p>
<p>And that&#8217;s all! I&#8217;ll be posting a more detailed overview on how to write a DSL in ruby at a later date. I hope you liked it. </p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/iain-blog?a=3m6uoY87IQM:HYGgiIf2Gig:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/iain-blog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/iain-blog?a=3m6uoY87IQM:HYGgiIf2Gig:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/iain-blog?i=3m6uoY87IQM:HYGgiIf2Gig:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/iain-blog/~4/3m6uoY87IQM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.codejoust.com/2010/02/ruby-site-table-fetching/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://iain.codejoust.com/2010/02/ruby-site-table-fetching/</feedburner:origLink></item>
		<item>
		<title>Photos of the Snow</title>
		<link>http://feedproxy.google.com/~r/iain-blog/~3/U03VMn1u-50/</link>
		<comments>http://iain.codejoust.com/2010/01/photos-of-the-snow/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 21:13:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iain.codejoust.com/?p=293</guid>
		<description><![CDATA[Picture of the Majestic Snow:
To not bother you with more than necessary&#8230;.

The wonderful beautiful blanket of snow showered atop the mountain today and yesterday, bringing an exuberant delight to those there.
Here are the remains of the dwaning fall.
]]></description>
			<content:encoded><![CDATA[<p>Picture of the Majestic Snow:</p>
<p>To not bother you with more than necessary&#8230;.</p>
<p><a href="http://iain.codejoust.com/wp-content/uploads/2010/01/snowtree.jpg"><img src="http://iain.codejoust.com/wp-content/uploads/2010/01/snowtree-300x199.jpg" alt="Snow falling between the trees." title="Snow falling between the trees." width="300" height="199" class="aligncenter size-medium wp-image-294" /></a></p>
<p>The wonderful beautiful blanket of snow showered atop the mountain today and yesterday, bringing an exuberant delight to those there.</p>
<p>Here are the remains of the dwaning fall.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/iain-blog?a=U03VMn1u-50:JNZBrF6eNZg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/iain-blog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/iain-blog?a=U03VMn1u-50:JNZBrF6eNZg:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/iain-blog?i=U03VMn1u-50:JNZBrF6eNZg:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/iain-blog/~4/U03VMn1u-50" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iain.codejoust.com/2010/01/photos-of-the-snow/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://iain.codejoust.com/2010/01/photos-of-the-snow/</feedburner:origLink></item>
	</channel>
</rss>
