<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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/"
	>

<channel>
	<title>Doodads and Whatnot</title>
	<atom:link href="http://hazy.stupor.org/blog/feed" rel="self" type="application/rss+xml" />
	<link>http://hazy.stupor.org/blog</link>
	<description></description>
	<lastBuildDate>Wed, 11 Nov 2009 08:04:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A letter to Random House Australia regarding eBooks.</title>
		<link>http://hazy.stupor.org/blog/archives/38</link>
		<comments>http://hazy.stupor.org/blog/archives/38#comments</comments>
		<pubDate>Wed, 11 Nov 2009 08:02:55 +0000</pubDate>
		<dc:creator>toholio</dc:creator>
				<category><![CDATA[Ranty]]></category>
		<category><![CDATA[amazon]]></category>
		<category><![CDATA[book]]></category>
		<category><![CDATA[discworld]]></category>
		<category><![CDATA[ebook]]></category>
		<category><![CDATA[fiction]]></category>
		<category><![CDATA[kindle]]></category>
		<category><![CDATA[novel]]></category>
		<category><![CDATA[pratchett]]></category>
		<category><![CDATA[publisher]]></category>
		<category><![CDATA[reading]]></category>
		<category><![CDATA[terrypratchett]]></category>
		<category><![CDATA[tp]]></category>

		<guid isPermaLink="false">http://hazy.stupor.org/blog/?p=38</guid>
		<description><![CDATA[The following is a letter I sent to the Random House Australia. As you can tell from reading it I am unhappy that it is not possible for me to buy the books I want from them in the format I want.

27th October 2009
Ebook publishing.
Random House Australia
Level 3 100 Pacific Highway North Sydney, N.S.W., 2060 Australia
Dear sir or madam,
			I [...]]]></description>
			<content:encoded><![CDATA[<p>The following is a letter I sent to the Random House Australia. As you can tell from reading it I am unhappy that it is not possible for me to buy the books I want from them in the format I want.</p>
<blockquote><p>
27th October 2009<br />
Ebook publishing.<br />
Random House Australia<br />
Level 3 100 Pacific Highway North Sydney, N.S.W., 2060 Australia</p>
<p>Dear sir or madam,</p>
<p>			I have followed with keen interest the international launch of the Kindle electronic reader from Amazon.com. When I had established that a minimum of books which interest me are available I placed an order and am now a <em>very</em> satisfied user of the device. It is the second e-ink device I have owned and without a doubt its best feature is the ease with which content may be sampled and purchased.</p>
<p>It was with some irritation however that I noticed many if not all books from your catalogue are not available for purchase in Kindle format within Australia. This is despite them being available from the Kindle store for other regions. I appreciate that this is due to the way that publishing rights are negotiated but, as I will outline, this doesn&#8217;t matter to me.</p>
<p>I shall also outline why I have come to the conclusion that the greatest piracy threat publishers face is themselves.</p>
<p>Let us look at the example of Terry Pratchett&#8217;s recent book Unseen Academicals. I would very much like to read this book but I don&#8217;t necessarily feel the need to own a paper copy. In the event that I might desire to own a paper copy I still may not bother because of a simple truth: I am lazy. I want to take the easiest route to reading the novel at a reasonable price. The less effort involved the more likely my purchase.</p>
<p>Were the book available via the Kindle store I would have bought it within a few moments with not much more than a quick taping of a few buttons. It wouldn&#8217;t even have been necessary for me to get out of my hammock or comfy chair. However, the book is not available to me in this way. What am I to do?</p>
<p>There are a few options. For the sake of making my overall point plain I shall list them in order of ease, starting with the easiest.</p>
<ol>
<li>I could not read the book. You get nothing, Mr. Pratchett gets nothing, and I get nothing.</li>
<li>I could pirate the book. A few quick Google searches suggest that it&#8217;s definitely available. The quality is likely to be variable but from the comments of the people who have pirated it would appear to be readable.</li>
<li>I could create a fake U.S. address and have the book &#8220;shipped&#8221; there. You get nothing, Mr. Pratchett gets paid, and I get to read the book.</li>
<li>I could go to the library. This is closer to my house than any decently sized bookstore and reserving the book online is often as fast as ordering it. You get a small amount of money per reader, Mr. Pratchett gets a small amount of money per reader, and I get to read the book.</li>
<li>I could go to a physical bookstore. The outcome of which is obvious.</li>
</ol>
<p>Critical in all of this is the observation that by not making the title available for the device I use you&#8217;ve made several options which don&#8217;t net you any income more attractive than buying from you. Obviously, given my writing to you, I have significant objections to piracy and would not want to deprive Mr. Pratchett of the income but as for you? I&#8217;m not sure it upsets me as much. If the author is paid and some publisher continues to make future titles available it doesn&#8217;t seem that it would change my enjoyment of the books significantly.</p>
<p>Much of the chatter on internet forums regarding the Kindle launch has focused on ways in which users may access the U.S. Kindle store. This is a very positive sign that people are willing to pay for the content they consume. I doubt however that they will be return exclusively to paper books should this &#8220;loophole&#8221; be closed. They will instead move to the next easiest option, piracy.</p>
<p>It is to be hoped that what you see as the solution to this is making your catalogue available in formats for which people have shown a desire. If your position is that electronic book formats require more protection or better region controls then I have little sympathy for you and hope that you see the error in this before you find your ebook offerings replaced by foreign or illegitimate markets.</p>
<p>If you are currently negotiating with Amazon or preparing your content for sale in Kindle format please disregard this letter.</p>
<p>Yours sincerely,</p>
<p>Name Removed.
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://hazy.stupor.org/blog/archives/38/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A letter to the Minister for Housing</title>
		<link>http://hazy.stupor.org/blog/archives/36</link>
		<comments>http://hazy.stupor.org/blog/archives/36#comments</comments>
		<pubDate>Fri, 24 Apr 2009 09:29:16 +0000</pubDate>
		<dc:creator>toholio</dc:creator>
				<category><![CDATA[Random]]></category>
		<category><![CDATA[Ranty]]></category>
		<category><![CDATA[fhog]]></category>
		<category><![CDATA[first home]]></category>
		<category><![CDATA[grant]]></category>
		<category><![CDATA[home buyer]]></category>
		<category><![CDATA[housing]]></category>
		<category><![CDATA[letter]]></category>

		<guid isPermaLink="false">http://hazy.stupor.org/blog/?p=36</guid>
		<description><![CDATA[The following is a letter I sent to the Minister for Housing. I also sent slightly modified versions to my local member and to the Prime Minister.
The Hon. Tanya Plibersek MP
Minister for Housing
Minister for the Status of Women
111-117 Devonshire Street
Surry Hills, N.S.W., 2010
Dear Minister,
I have read with interest the Prime Minister&#8217;s recent comments regarding the [...]]]></description>
			<content:encoded><![CDATA[<p>The following is a letter I sent to the Minister for Housing. I also sent slightly modified versions to my local member and to the Prime Minister.</p>
<blockquote><p>The Hon. Tanya Plibersek MP<br />
Minister for Housing<br />
Minister for the Status of Women<br />
111-117 Devonshire Street<br />
Surry Hills, N.S.W., 2010</p>
<p>Dear Minister,</p>
<p>I have read with interest the Prime Minister&#8217;s recent comments regarding the possibility of the boosted First Home Buyers Grant ending as scheduled. As a person saving for my first home I am very much in favour of ending the grant as it appears to me only to push up prices and encourage people to enter the property market before they are ready. I would urge you to consider ways in which the grant might be minimised, applied only to new construction or withdrawn in the future, if this is not already planned. I would also ask you to consider how negative gearing for existing dwellings may be removed in the future or made to apply only to rental profits.</p>
<p>The First Home Saver Account scheme is in my opinion a far better initiative than grants to people entering the property market. I believe it would be in the best interests of people entering the property market to increase the government contribution percentage, increase the cap on the government contributions, provide a grant only to those who have met the withdrawal criteria or have a proven savings history, or some combination of these options.</p>
<p>I look forward to seeing how the upcoming budget and future policy will address my interests in this matter.</p>
<p>Sincerely,</p>
<p>Name Removed.
</p></blockquote>
<p>As you can guess I&#8217;m not happy with the meddling that is happening in the property market.</p>
]]></content:encoded>
			<wfw:commentRss>http://hazy.stupor.org/blog/archives/36/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A letter to the Minister for Broadband, Communications and the Digital Economy</title>
		<link>http://hazy.stupor.org/blog/archives/37</link>
		<comments>http://hazy.stupor.org/blog/archives/37#comments</comments>
		<pubDate>Sun, 02 Nov 2008 09:34:55 +0000</pubDate>
		<dc:creator>toholio</dc:creator>
				<category><![CDATA[Random]]></category>
		<category><![CDATA[Ranty]]></category>
		<category><![CDATA[australia]]></category>
		<category><![CDATA[broadband]]></category>
		<category><![CDATA[letter]]></category>
		<category><![CDATA[stephen conroy]]></category>

		<guid isPermaLink="false">http://hazy.stupor.org/blog/?p=37</guid>
		<description><![CDATA[The following is a letter I wrote to the Minister for Broadband, Communications and the Digital Economy which I penned in response to the Australian Government&#8217;s unworkable internet filtering scheme.

Senator the Hon. Stephen Conroy
Minister for Broadband, Communications and the Digital Economy
Level 4, 4 Treasury Place
Melbourne
Victoria, 3002
Dear Minister,
As an employee of an Australian company which provides [...]]]></description>
			<content:encoded><![CDATA[<p>The following is a letter I wrote to the Minister for Broadband, Communications and the Digital Economy which I penned in response to the Australian Government&#8217;s unworkable internet filtering scheme.</p>
<blockquote><p>
Senator the Hon. Stephen Conroy<br />
Minister for Broadband, Communications and the Digital Economy<br />
Level 4, 4 Treasury Place<br />
Melbourne<br />
Victoria, 3002</p>
<p>Dear Minister,</p>
<p>As an employee of an Australian company which provides secure internet based communications and transaction exchange software, and an internet user I am deeply troubled by plans to introduce mandatory internet filtering.</p>
<p>Such filtering has been shown in countries where it is used to be largely ineffective. The massive and ever growing amount of information available means that it will never be possible to eliminate inappropriate content from the internet. It would be better to focus on efforts such as law enforcement and education which would help reduce the instances of this content being created or sought in the first place.</p>
<p>The size of the problem will mean that only known and high profile items will make it to the list of inappropriate content in a timely fashion. The ability to effectively filter only these targets will make us a target of ridicule. It has become a not uncommon activity to show a &#8216;Chinese view&#8217; of a website beside a &#8216;normal view&#8217; of the same site. I doubt it will do Australia&#8217;s reputation good to be added as the third party in these comparisons.</p>
<p>The technical realities of the problem will adversely affect  the cost and performance of internet services in Australia. As well as being a nuisance to Australian internet users it will also force internet based services operating out of Australia to relocate hosting and technical operations to foreign countries.</p>
<p>Furthermore, as an employee of an Australian company which provides internet based secure transaction exchange services, I have serious security concerns with the implementation of this plan. The sales of our communications products rely in large part on the level  of privacy and security we can offer our clients. The use of &#8216;man in the middle&#8217; attacks, required by the tested filtering software for Secure Sockets Layer connections, will remove our ability to assure customers that their security has not been compromised. The correct operation of the proposed filters will be indistinguishable from a hostile party performing &#8216;man in the middle&#8217; attacks.</p>
<p>While Australian customers may in time come to accept this as unavoidable (since online banking and other operations will suffer the same problem) it will make our products unmarketable to foreign customers without moving our hosting and server infrastructure overseas. This will make a number of our Australian staff redundant.</p>
<p>I regret that the nature of the plans became clear to me too late for it to have been a factor in my voting choices for the recent election. I do not doubt that these plans are ultimately motivated by good intentions but I fear that the originators have bought snake oil and wish to continue applying it to gain the favour of a few &#8216;key&#8217; politicians. </p>
<p>Please consider how the money for the filtering scheme may be better spent. I would suggest that increasing funding to education and law enforcement would better meet its objectives and the requirements of all Australians.</p>
<p>Sincerely,</p>
<p>Name Removed.
</p></blockquote>
<p>I received the same form letter reply as everyone else who wrote to the minister.</p>
]]></content:encoded>
			<wfw:commentRss>http://hazy.stupor.org/blog/archives/37/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby serialport gem.</title>
		<link>http://hazy.stupor.org/blog/archives/35</link>
		<comments>http://hazy.stupor.org/blog/archives/35#comments</comments>
		<pubDate>Mon, 01 Sep 2008 01:10:18 +0000</pubDate>
		<dc:creator>toholio</dc:creator>
				<category><![CDATA[Electronics]]></category>
		<category><![CDATA[Nerdy]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[io]]></category>
		<category><![CDATA[ruby-serialport]]></category>
		<category><![CDATA[rubygem]]></category>
		<category><![CDATA[serialport]]></category>

		<guid isPermaLink="false">http://hazy.stupor.org/blog/?p=35</guid>
		<description><![CDATA[Ruby-serialport doesn&#8217;t play well with RubyGems. Trying to require 'serialport' when using RubyGems results in:

NameError: (eval):1:in `private_class_method': undefined method `create' for class `Class'
        from (eval):1
        from (eval):1
        from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
     [...]]]></description>
			<content:encoded><![CDATA[<p>Ruby-serialport doesn&#8217;t play well with RubyGems. Trying to <tt>require 'serialport'</tt> when using RubyGems results in:</p>

<div class="wp_syntax"><div class="code"><pre class="text">NameError: (eval):1:in `private_class_method': undefined method `create' for class `Class'
        from (eval):1
        from (eval):1
        from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
        from (irb):2</pre></div></div>

<p>The quick fix to get around this is by doing:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#CC00FF; font-weight:bold;">Kernel</span>::<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'serialport'</span></pre></div></div>

<p>It would be nicer if we could avoid the conflict with RubyGems. It would be even nicer if the serialport library was itself packaged as a gem. Turns out this <a href="http://github.com/aaronp/ruby-serialport/">has been done by aaronp</a> but it doesn&#8217;t take advantage of GitHub&#8217;s automatic gem builder and doesn&#8217;t build correctly using GCC.</p>
<p><a href="http://github.com/toholio/ruby-serialport/">I&#8217;ve cloned and tidied the git repository</a>. This gem version should build the needed native extension and work on both POSIX compatible and Windows operating systems.</p>
<h3>Installing the serialport gem</h3>
<p>If you haven&#8217;t configured GitHub as a gem source yet you will need to run the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="text">gem sources -a http://gems.github.com</pre></div></div>

<p>Then, you can install the gem as normal:</p>

<div class="wp_syntax"><div class="code"><pre class="text">sudo gem install toholio-serialport</pre></div></div>

<p>Unlike the official ruby-serialport extension, this gem works with RubyGems (duh!). When you want to use it you should only need to do the following:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><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;">'serialport'</span></pre></div></div>

<p>On a side note: git and GitHub rock. Forking, contributing to and merging projects <i>should</i> be this simple.</p>
]]></content:encoded>
			<wfw:commentRss>http://hazy.stupor.org/blog/archives/35/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Nickel Silver is moving to GitHub.</title>
		<link>http://hazy.stupor.org/blog/archives/33</link>
		<comments>http://hazy.stupor.org/blog/archives/33#comments</comments>
		<pubDate>Sun, 31 Aug 2008 09:51:06 +0000</pubDate>
		<dc:creator>toholio</dc:creator>
				<category><![CDATA[Nerdy]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://hazy.stupor.org/blog/?p=33</guid>
		<description><![CDATA[I&#8217;m falling in love with GitHub. To see if this love will last I&#8217;ve moved my LocoNet server gem to GitHub from RubyForge. The RubyForge gem still exists (and will continue to do so) but changes will be happening at GitHub (probably permanently).
To get the new version of the gem you will need to have [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m falling in love with <a href="http://github.com/">GitHub</a>. To see if this love will last I&#8217;ve moved my LocoNet server gem <a href="http://github.com/toholio/nickel-silver-server/tree/master">to GitHub</a> <a href="http://rubyforge.org/projects/nickel-silver/">from RubyForge</a>. The RubyForge gem still exists (and will continue to do so) but changes will be happening at GitHub (probably permanently).</p>
<p>To get the new version of the gem you will need to have GitHub configured as a gem source. So if you haven&#8217;t already, you must run:</p>

<div class="wp_syntax"><div class="code"><pre class="text">gem sources -a http://gems.github.com</pre></div></div>

<p>To get the latest version of the gem run:</p>

<div class="wp_syntax"><div class="code"><pre class="text">sudo gem install toholio-nickel-silver-server</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://hazy.stupor.org/blog/archives/33/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby on Nickel-Silver Rails</title>
		<link>http://hazy.stupor.org/blog/archives/29</link>
		<comments>http://hazy.stupor.org/blog/archives/29#comments</comments>
		<pubDate>Wed, 19 Mar 2008 10:15:32 +0000</pubDate>
		<dc:creator>toholio</dc:creator>
				<category><![CDATA[Electronics]]></category>
		<category><![CDATA[Nerdy]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[gserver]]></category>
		<category><![CDATA[loconet]]></category>
		<category><![CDATA[models]]></category>
		<category><![CDATA[nicklesilver]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[trains]]></category>

		<guid isPermaLink="false">http://hazy.stupor.org/blog/archives/29</guid>
		<description><![CDATA[It&#8217;s been hot here in Melbourne so rather than actually leaving the house to do anything I&#8217;ve been sitting in front of a fan mucking about with my computer. Yesterday afternoon I created a Ruby implementation of a LocoNetOverTCP server. I&#8217;ve decided to call it Nickel-Silver because that&#8217;s the common name of the alloy most [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been hot here in Melbourne so rather than actually leaving the house to do anything I&#8217;ve been sitting in front of a fan mucking about with my computer. Yesterday afternoon I created a Ruby implementation of a <a href="http://loconetovertcp.sourceforge.net/Protocol/LoconetOverTcp.html">LocoNetOverTCP</a> server. I&#8217;ve decided to call it Nickel-Silver because that&#8217;s the common name of the alloy most model railroad track is made of and some trifling project has already used the name &#8220;Ruby on Rails&#8221;. Here&#8217;s a quick list of Nickel-Silver&#8217;s features:</p>
<ul>
<li>Implements all of LocoNetOverTCP protocol version 1</li>
<li>Multithreaded (inherits from GServer)</li>
<li>Easily extendable interface for LocoNet hardware devices</li>
<li>Only about 100 lines of code (according to <tt>egrep -c -v "^[ ]*$|^[ ]*#"</tt>)</li>
<li>Base implementation uses only the standard library</li>
<li>Included &#8220;driver&#8221; for the LocoBuffer-USB hardware uses a single extra package, <a href="http://rubyforge.org/projects/ruby-serialport/">ruby-serialport</a></li>
<li>It&#8217;s written in Ruby</li>
</ul>
<h3>Hardware setup</h3>
<p>Currently the only &#8220;driver&#8221; provided connects to a LocoNet network via a <a href="http://www.rr-cirkits.com/locobuffer-usb/LB-usb-flyer.pdf">LocoBuffer-USB</a>. So a complete hardware setup looks something like the following diagram.</p>
<p><center><a href='http://hazy.stupor.org/blog/wp-content/uploads/2008/03/nickelsilver-wiring-diagram.png' title='Electrical setup for LocoNetOverTCP server'><img src='http://hazy.stupor.org/blog/wp-content/uploads/2008/03/nickelsilver-wiring-diagram.png' alt='Electrical setup for LocoNetOverTCP server' /></a></center></p>
<p>LocoNetOverTCP clients connect to the server running on the computer which acts as a proxy to the LocoNet. This removes the need for each computer to have its own LocoNet hardware interface. It also removes the need for the clients to be physically located near layout.</p>
<h3>Server setup</h3>
<p>Assuming we&#8217;re going to use the included LocoBuffer-USB &#8220;driver&#8221; we need to do the following to start a server.</p>
<ol>
<li>Install ruby-serialport if you haven&#8217;t already.</li>
<li>Install the nickel-silver-server gem</li>
<li>Open a new ruby script in your favourite editor or start IRB.</li>
<li>Determine the serial port your LocoBuffer-USB is connected to.</li>
<li>Create an instance of the LocoBufferUSB &#8220;driver&#8221; class.</li>
<li>Create an instance of the server with the &#8220;driver&#8221; as a parameter.</li>
<li>Run the script</li>
</ol>
<p>To install the server&#8217;s gem open a terminal and run:</p>

<div class="wp_syntax"><div class="code"><pre class="text">gem install nickel-silver-server</pre></div></div>

<p>That should get everything that&#8217;s required with the exception of ruby-serialport which you must install manually.</p>
<p>My LocoBuffer-USB is connected to my computer via the virtual serial port <tt>/dev/cu.usbserial-FTQ1P4JC</tt> so a complete script for my server would look like the following.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#008000; font-style:italic;">#!/usr/bin/ruby</span>
&nbsp;
<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;">'nickel-silver-server'</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># connect to a LocoBufferUSB on the virtual serial port /dev/cu.usbserial-FTQ1P4JC'</span>
interface = <span style="color:#6666ff; font-weight:bold;">NickelSilver::Server::Interface::LocoBufferUSB</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#996600;">'/dev/cu.usbserial-FTQ1P4JC'</span> <span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># create a server using the default port (i.e. 5626, 'loco' spelt on a phone keypad)</span>
<span style="color:#008000; font-style:italic;"># using our freshly connected LocoBuffer-USB</span>
server = <span style="color:#6666ff; font-weight:bold;">NickelSilver::Server::LocoNetServer</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span> interface <span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># start the server</span>
server.<span style="color:#9900CC;">start</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># wait for the server to stop before exiting</span>
server.<span style="color:#9900CC;">join</span></pre></div></div>

<h3>Documentation</h3>
<p>Currently this post is the only documentation not contained within the gem. You can view the gem&#8217;s rdoc documentation at <a href="http://nickel-silver.rubyforge.org/nickel-silver-server/rdoc/">http://nickel-silver.rubyforge.org/nickel-silver-server/rdoc/</a></p>
<h3>Sending trains down the track</h3>
<p>Until boredom strikes again there isn&#8217;t a Ruby throttle program that can interface with Nickel-Silver. Instead we can use the excellent Java program <a href="http://jmri.sourceforge.net/">JMRI</a> to talk to our trains.</p>
<p>JMRI already knows about LocoNetOverTCP servers right out of the box. Starting JMRI and looking at the preferences we can wrangle things to look like the following.<br />
<center><a href='http://hazy.stupor.org/blog/wp-content/uploads/2008/03/nickelsilver-jmri-prefs.png' title='JMRI connecting to Nickel Silver'><img src='http://hazy.stupor.org/blog/wp-content/uploads/2008/03/nickelsilver-jmri-prefs.png' alt='JMRI connecting to Nickel Silver' /></a></center></p>
<p>Now we&#8217;re ready to go. Selecting <tt>Tools > Throttles > New Throttle</tt> gives us a throttle window where we can enter a locomotive address and make it move.</p>
<h3>Implementing a new &#8220;driver&#8221;</h3>
<p>The only LocoNet computer interface I have access to is a LocoBuffer-USB. I&#8217;d highly recommend one to anyone who is interested but acknowledge that there are many people with other hardware.</p>
<p>As long as you can send and receive LocoNet packets via your hardware in Ruby (or in any language that may be used for Ruby extensions) you can write a &#8220;driver&#8221;.</p>
<p>The only things a &#8220;driver&#8221; needs to be able to do are:</p>
<ul>
<li>Store incoming packets as FixNums representing bytes in a buffer array</li>
<li>Send outgoing bytes (represented as FixNums in a buffer array) to LocoNet</li>
<li>Use a Mutex to lock access to the buffers when in use (remember Nickel-Silver is multithreaded)</li>
<li>Provide a method that causes your interface to start collecting packets</li>
</ul>
<p>The interface is simple. Only the following public methods are needed:</p>
<ul>
<li>Accessors for <tt>input_buffer</tt>, <tt>output_buffer</tt> and <tt>io_mutex</tt></li>
<li><tt>run()</tt> which starts buffering</li>
</ul>
<p>How you do this is up to you but you can take a look at <tt>LocoBufferUSB.rb</tt> to get an idea of how it might be done. A stub driver might look like the following&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> SomeLocoNetInterface
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:input_buffer</span>, <span style="color:#ff3333; font-weight:bold;">:output_buffer</span>, <span style="color:#ff3333; font-weight:bold;">:io_mutex</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize
    <span style="color:#008000; font-style:italic;"># these may be modified at any time by the server</span>
    <span style="color:#0066ff; font-weight:bold;">@input_buffer</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#0066ff; font-weight:bold;">@output_buffer</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># only make changes when locked using @io_mutex</span>
    <span style="color:#0066ff; font-weight:bold;">@io_mutex</span> = <span style="color:#CC00FF; font-weight:bold;">Mutex</span>.<span style="color:#9900CC;">new</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> run
    <span style="color:#CC0066; font-weight:bold;">loop</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#008000; font-style:italic;"># get incoming bytes</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> byte_waiting?
        <span style="color:#0066ff; font-weight:bold;">@io_mutex</span>.<span style="color:#9900CC;">synchronize</span> <span style="color:#9966CC; font-weight:bold;">do</span>
          <span style="color:#008000; font-style:italic;"># byte getting code here</span>
          <span style="color:#0066ff; font-weight:bold;">@input_buffer</span> &lt;&lt; get_byte
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      <span style="color:#008000; font-style:italic;"># send outgoing bytes</span>
      <span style="color:#9966CC; font-weight:bold;">until</span> <span style="color:#0066ff; font-weight:bold;">@output_buffer</span>.<span style="color:#9900CC;">empty</span>? <span style="color:#9966CC; font-weight:bold;">do</span>
        <span style="color:#0066ff; font-weight:bold;">@io_mutex</span>.<span style="color:#9900CC;">synchronize</span> <span style="color:#9966CC; font-weight:bold;">do</span>
          <span style="color:#008000; font-style:italic;"># send a byte</span>
          send_byte<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0066ff; font-weight:bold;">@output_buffer</span>.<span style="color:#9900CC;">shift</span> <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>
    <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;">end</span></pre></div></div>

<p>Remember that either buffer may be empty or already containing data and that you <em>must</em> lock the mutex for the minimum amount of time possible.</p>
<h3>Isn&#8217;t there already a LocoNetOverTCP server project?</h3>
<p>Yes there is! It&#8217;s called <a href="http://loconetovertcp.sourceforge.net/Server/index.html">LbServer</a> and is hosted over at <a href="http://sourceforge.net/projects/loconetovertcp">SourceForge</a>.</p>
<p>If you&#8217;re looking for a mature LocoNetOverTCP server that&#8217;s probably the place you should start. Unlike my implementation there are more people than just me using it and they probably have more bugs fixed than I&#8217;m likely to notice any time soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://hazy.stupor.org/blog/archives/29/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>has_wiki_field: quickly building a wiki in Ruby on Rails</title>
		<link>http://hazy.stupor.org/blog/archives/28</link>
		<comments>http://hazy.stupor.org/blog/archives/28#comments</comments>
		<pubDate>Mon, 17 Dec 2007 02:46:12 +0000</pubDate>
		<dc:creator>toholio</dc:creator>
				<category><![CDATA[Nerdy]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://hazy.stupor.org/blog/archives/28</guid>
		<description><![CDATA[Has the boss just discovered wikis? Does he now want to be able to create links between records in your intranet applications? Need to quickly add wiki functionality to your models and views so you can get back on with your life?
That&#8217;s what happened to me recently. Since the wiki code was almost exactly the [...]]]></description>
			<content:encoded><![CDATA[<p>Has the boss just discovered wikis? Does he now want to be able to create links between records in your intranet applications? Need to quickly add wiki functionality to your models and views so you can get back on with your life?</p>
<p>That&#8217;s what happened to me recently. Since the wiki code was almost exactly the same for all of the applications that needed changing, I&#8217;ve put the code into a plugin. Hopefully someone else will find it useful.</p>
<h3>Installing the plugin</h3>
<p>The plugin is installed in the same manner as any other. Fire up your favourite terminal and navigate to the root of your rails application. Then run the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="text">script/plugin install http://hazy.stupor.org/svn/has_wiki_field/trunk/has_wiki_field/</pre></div></div>

<p>That should be all that is required.</p>
<h3>Using the plugin</h3>
<p>The following example should get you up and running quickly but make sure you read the efficiency note below.</p>
<p>Lets say you have a Person model that has name and bio attributes. We would like to be able to link to other people from within a bio. So in Allan&#8217;s record we might want the bio to read &#8220;This man is friends with [[Betty]]&#8221; and have [[Betty]] automatically change into a link to her record when it is displayed.</p>
<p>We, start by adding has_wiki_field to the Person model:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> Person &lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_wiki_field <span style="color:#ff3333; font-weight:bold;">:field</span> =&gt; <span style="color:#996600;">&quot;name&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:sql_match</span> =&gt; <span style="color:#996600;">&quot;LIKE&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The :field option specifies what field should be matched by the text in [[links]]. The :sql_match option specifies how the records should be matched. :sql_match defaults to &#8220;=&#8221; but using &#8220;LIKE&#8221; will allow case insensitive links.</p>
<p>Now we need to change show.html.erb for the people controller so that links are generated from the bio.</p>
<p>Where previously we might have displayed a bio using:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">&lt;%= h <span style="color:#0066ff; font-weight:bold;">@person</span>.<span style="color:#9900CC;">bio</span> %&gt;</pre></div></div>

<p>We will now use the wikify method, passing it the name of the attribute to wikify and a code block which generates the html for links. A code block is used to allow flexibility in link generation. The following block simply calls link_to but you could, for example, write a block which looks up an image associated with your model and displays that instead.</p>
<p>Anyway, for our Person model we add the following to the view:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">&lt;%= <span style="color:#0066ff; font-weight:bold;">@person</span>.<span style="color:#9900CC;">wikify</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:bio</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> |name, object|
    <span style="color:#9966CC; font-weight:bold;">if</span> object.<span style="color:#0000FF; font-weight:bold;">nil</span>?
      h name
    <span style="color:#9966CC; font-weight:bold;">else</span>
      link_to name, object
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
%&gt;</pre></div></div>

<p>In a real application this should be put into a method in people_helper to help keep things tidy.</p>
<p>That&#8217;s it! Where [[Some Name]] appears in a Person&#8217;s bio it will be turned into a link to the person with the name &#8216;Some Name&#8217;.</p>
<p>Remember to read the important efficiency note section below before using this in a production environment.</p>
<h3>An important note about efficiency</h3>
<p>The default method used for finding which object a [[link]] points to is not efficient. For sites that receive more than a tiny amount of traffic it should be replaced by your own model specific method.</p>
<p>The default method takes the contents of each [[link]] and queries the database for the first record that has &#8216;link&#8217; as the value for the field specified in the model, I.e.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">find<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:first</span>, <span style="color:#ff3333; font-weight:bold;">:conditions</span> =&gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;somefieldname = ?&quot;</span>, key<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>This will obviously require a database look-up for every link which could slow your site down tremendously for pages containing many links.</p>
<p>To fix this you must provide your own method to match objects with the keys extracted from [[links]]. The class method to override for this is object_for_key(key).</p>
<p>For example, if we have a class called Person we might replace the object_for_key method like so:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> Person &lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_wiki_field
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">object_for_key</span><span style="color:#006600; font-weight:bold;">&#40;</span> key <span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;"># some super fast method for matching the key</span>
    <span style="color:#008000; font-style:italic;"># from a link to a person object, goes here...</span>
    <span style="color:#008000; font-style:italic;">#</span>
    <span style="color:#008000; font-style:italic;"># return the Person object that matches or nil if there is no match</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Naturally, it&#8217;s up to you to manage the data used by your object_for_key method. wiki_options[:field] and wiki_options[:sql_match] will be available to you in this method if they were set as options to has_wiki_field.</p>
<h3>Future plans</h3>
<p>The plugin doesn&#8217;t do much currently. If I&#8217;d only needed it for a single application there&#8217;s a good chance it wouldn&#8217;t have made it into a plugin at all. It has provided me with a reasonable starting point for my applications and hopefully it&#8217;ll get better as time goes on.</p>
<p>Some ideas for future versions:</p>
<ul>
<li>Add a controller action which takes a key and redirects to the appropriate record or creates a new record with the needed field already filled.</li>
<li>Using the new controller method above, allow links of the form [[Some Key#Model]] where &#8220;Model&#8221; specifies what type of thing links are pointing to. Ta-dah! Now we can link between all the models in an application. </li>
<li>Implement some basic object_for_key methods for common situations. The plugin should attempt to gently direct programers towards these methods.</li>
<li>Implement a basic link caching mechanism. When a has_wiki_field model is saved links should be extracted and stored in a table. Then all current links for a model can be loaded in a single database transaction.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://hazy.stupor.org/blog/archives/28/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generating an iCalendar feed from a controller in Ruby on Rails</title>
		<link>http://hazy.stupor.org/blog/archives/27</link>
		<comments>http://hazy.stupor.org/blog/archives/27#comments</comments>
		<pubDate>Wed, 12 Dec 2007 04:54:17 +0000</pubDate>
		<dc:creator>toholio</dc:creator>
				<category><![CDATA[Nerdy]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://hazy.stupor.org/blog/archives/27</guid>
		<description><![CDATA[Recently I needed to set-up an application to keep track of regular safety inspections for power tools and other equipment. The application is very trivial but my client was quite pleased with one particular feature which wasn&#8217;t actually requested.
The &#8220;bonus&#8221; feature was an iCalendar feed which allows the client to have his computers subscribe to [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I needed to set-up an application to keep track of regular safety inspections for power tools and other equipment. The application is very trivial but my client was quite pleased with one particular feature which wasn&#8217;t actually requested.</p>
<p>The &#8220;bonus&#8221; feature was an iCalendar feed which allows the client to have his computers subscribe to the feed and keep up to date with what equipment needs inspecting and when. Very swish and, thanks to Rails and a nice gem, very easy.</p>
<p>So how&#8217;s it done? Easy!</p>
<p>Lets start by installing <a href="http://icalendar.rubyforge.org/">the icalendar gem</a>. Open a terminal and install it as normal:</p>

<div class="wp_syntax"><div class="code"><pre class="text">user@host railsapp$ gem install icalendar</pre></div></div>

<p>Change to the vendor directory of your Rails application and unpack the gem:</p>

<div class="wp_syntax"><div class="code"><pre class="text">user@host railsapp$ cd vendor/
user@host vendor$ gem unpack icalendar</pre></div></div>

<p>Make sure it will be loaded by adding it to the end of config/environment.rb in your Rails application:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'icalendar-1.0.2/lib/icalendar'</span></pre></div></div>

<p>Now we&#8217;re ready to add a calendar feed to our application. Lets say we have a table that contains the names and scheduled service dates of some tools. The client wants, or is going to get, a feed which marks each tool needing service in their calendar when their servicing is due.</p>
<p>Generating the iCalendar data is simple. The following method from tool_controller creates an event for each tool and inserts it into a calendar. The to_ical call at the end of the method get the calendar as a string which can be served to the user. Note that this method assumes the @tools variable has already been set (you could always add a call to Tool.find(:all) at the start of the method if needed).</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">def</span> generate_ical
  cal = <span style="color:#6666ff; font-weight:bold;">Icalendar::Calendar</span>.<span style="color:#9900CC;">new</span>
  <span style="color:#0066ff; font-weight:bold;">@tools</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |tool|
    <span style="color:#008000; font-style:italic;"># create the event for this tool</span>
    event = <span style="color:#6666ff; font-weight:bold;">Icalendar::Event</span>.<span style="color:#9900CC;">new</span>
    event.<span style="color:#9900CC;">start</span> = tool.<span style="color:#9900CC;">inspection_date</span>
    event.<span style="color:#9966CC; font-weight:bold;">end</span> = tool.<span style="color:#9900CC;">inspection_date</span>
    event.<span style="color:#9900CC;">summary</span> = <span style="color:#996600;">&quot;Service of &quot;</span> + tool.<span style="color:#9900CC;">name</span> + <span style="color:#996600;">&quot; is due.&quot;</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># insert the event into the calendar</span>
    cal.<span style="color:#9900CC;">add</span> event
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># return the calendar as a string</span>
  cal.<span style="color:#9900CC;">to_ical</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Next, we add the icalendar feed to respond_to in the tools_controller&#8217;s index method. We&#8217;ll make this call the generate_ical method to get the calendar data.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">def</span> index
  <span style="color:#0066ff; font-weight:bold;">@tools</span> = Tool.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  respond_to <span style="color:#9966CC; font-weight:bold;">do</span> |format|
    <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#008000; font-style:italic;"># index.html.erb</span>
    <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>  <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:xml</span> =&gt; <span style="color:#0066ff; font-weight:bold;">@tools</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">ics</span>  <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:text</span> =&gt; <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">generate_ical</span> <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></pre></div></div>

<p>And now you should be able to open your calendar application and subscribe to the feed straight from the tools controller using a URL like http://example.com/tools.ics</p>
]]></content:encoded>
			<wfw:commentRss>http://hazy.stupor.org/blog/archives/27/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Easy filtering by optional criteria in Rails applications</title>
		<link>http://hazy.stupor.org/blog/archives/26</link>
		<comments>http://hazy.stupor.org/blog/archives/26#comments</comments>
		<pubDate>Wed, 12 Dec 2007 02:26:51 +0000</pubDate>
		<dc:creator>toholio</dc:creator>
				<category><![CDATA[Nerdy]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://hazy.stupor.org/blog/archives/26</guid>
		<description><![CDATA[I&#8217;ve frequently needed a very simple search method that allows partial matches for any combination of fields in a table. The following snippet shows how an ActiveRecord derived object might have a filter to allow for selection of records given a set of partial values.

class YourClass &#60; ActiveRecord::Base
   def self.filter&#40; partial_values &#41;
  [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve frequently needed a very simple search method that allows partial matches for any combination of fields in a table. The following snippet shows how an ActiveRecord derived object might have a filter to allow for selection of records given a set of partial values.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> YourClass &lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
   <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">filter</span><span style="color:#006600; font-weight:bold;">&#40;</span> partial_values <span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;"># don't bother at all if there is no search object</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> find<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> partial_values
&nbsp;
    con_string = <span style="color:#996600;">&quot;&quot;</span>
    con_array = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># build collection of conditions</span>
    partial_values.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |key,value|
      <span style="color:#9966CC; font-weight:bold;">if</span> value != <span style="color:#996600;">&quot;&quot;</span> <span style="color:#9966CC; font-weight:bold;">then</span>
        con_string += <span style="color:#996600;">&quot; and &quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> con_array.<span style="color:#9900CC;">size</span> &gt; <span style="color:#006666;">0</span>
        con_string += <span style="color:#996600;">&quot;#{key} LIKE ?&quot;</span>
        con_array &lt;&lt; <span style="color:#996600;">&quot;%#{value}%&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># construct the actual conditions array</span>
    conditions = <span style="color:#006600; font-weight:bold;">&#91;</span>con_string<span style="color:#006600; font-weight:bold;">&#93;</span>
    con_array.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> |item| conditions &lt;&lt; item <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
    find<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#ff3333; font-weight:bold;">:conditions</span> =&gt; conditions<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></pre></div></div>

<p>To use this you would obtain a set of search parameters, one for each filterable column, and pass it to YourClass.filter as a hash to get the matching rows.</p>
<p>So if you had a table with title and category columns you might create a page containing a form to collect partial value for filtering. When creating the form, assuming you will use Rails&#8217; form helpers, the fields_for :collection function is nice as it will allow for easy collection of a hash for the field values.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">&lt;% form_tag your_object_path, <span style="color:#ff3333; font-weight:bold;">:method</span> =&gt;<span style="color:#996600;">&quot;get&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span> %&gt;
	&lt;% fields_for <span style="color:#ff3333; font-weight:bold;">:partial_values</span> <span style="color:#9966CC; font-weight:bold;">do</span> |f| %&gt;
		Title contains: &lt;%= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:title</span> %&gt;&lt;br /&gt;
		Category contains: &lt;%= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:category</span> %&gt;&lt;br /&gt;
	&lt;% <span style="color:#9966CC; font-weight:bold;">end</span> %&gt;
	&lt;%= submit_tag <span style="color:#996600;">&quot;Filter items&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#0000FF; font-weight:bold;">nil</span> %&gt;
&lt;% <span style="color:#9966CC; font-weight:bold;">end</span> %&gt;</pre></div></div>

<p>Then once the form is submitted you would get the appropriate results in your controller using:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#0066ff; font-weight:bold;">@your_objects</span> = YourClass.<span style="color:#9900CC;">filter</span><span style="color:#006600; font-weight:bold;">&#40;</span> params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:partial_values</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://hazy.stupor.org/blog/archives/26/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MacOS X PIC toolbox</title>
		<link>http://hazy.stupor.org/blog/archives/22</link>
		<comments>http://hazy.stupor.org/blog/archives/22#comments</comments>
		<pubDate>Wed, 13 Sep 2006 15:23:35 +0000</pubDate>
		<dc:creator>toholio</dc:creator>
				<category><![CDATA[Electronics]]></category>
		<category><![CDATA[Nerdy]]></category>

		<guid isPermaLink="false">http://hazy.stupor.org/blog/archives/22</guid>
		<description><![CDATA[I recently needed to install all the PIC programming tools I use on several Macs. Since people often ask for help installing these kinds of things I&#8217;ve decided to put together an installer package containg all the tools I use for the majority of my PIC&#8217;n needs. Hopefully someone else will find it useful.
The installer [...]]]></description>
			<content:encoded><![CDATA[<p>I recently needed to install all the PIC programming tools I use on several Macs. Since people often ask for help installing these kinds of things I&#8217;ve decided to put together an installer package containg all the tools I use for the majority of my PIC&#8217;n needs. Hopefully someone else will find it useful.</p>
<p>The installer contains universal binaries for:
<ul>
<li><a href="http://home.pacbell.net/theposts/picmicro/">Jeff Post&#8217;s PK2</a> v2.01 &#8211; for operating the PICkit2 programmer hardware</li>
<li><a href="http://gputils.sourceforge.net/">GPUTILS</a> v0.13.4 &#8211; assembler, linker, etc</li>
<li><a href="http://sdcc.sourceforge.net/">sdcc</a> v2.6.0 &#8211; C compiler</li>
</ul>
<p>I have only tested the installer and resulting binaries on Macs running 10.4.7 (x86 and PPC) so if you try it under a different version of OS X, <a href="mailto:tjrichard@optusnet.com.au">please let me know how it works</a>.</p>
<p>The following installer contains all three packages (you can choose which ones to install).<br />
<center><a href="http://hazy.stupor.org/vault/PIC/PIC%20toolbox.zip"><img id="image23" src="http://hazy.stupor.org/blog/wp-content/uploads/2006/09/Metapackage.png" alt="Metapackage icon." /><br />PIC toolbox (8.1MB)</a></center></p>
<p><b>IMPORTANT:</b>The installer wont reconfigure your PATH, MANPATH or SDCC_HOME environment variables automatically. The things to add are:</p>
<ul>
<li><tt>/usr/local/bin</tt> to <tt>PATH</tt></li>
<li><tt>/usr/local/man</tt> to <tt>MANPATH</tt></li>
<li><tt>SDCC_HOME=/usr/local</tt></li>
</ul>
<p>For most people these can be configured by opening a terminal and running the following commands:</p>
<pre>
echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bash_profile
echo 'export MANPATH=/usr/local/man:$MANPATH' >> ~/.bash_profile
echo 'export SDCC_HOME=/usr/local' >> ~/.bash_profile
</pre>
]]></content:encoded>
			<wfw:commentRss>http://hazy.stupor.org/blog/archives/22/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.635 seconds -->
