<?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>James Wilding&#039;s Weblog</title>
	<atom:link href="http://jameswilding.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://jameswilding.net</link>
	<description>Buddhist businessman, freelance web developer</description>
	<lastBuildDate>Sun, 05 Sep 2010 09:38:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How To Use RSpec with Rails 3</title>
		<link>http://jameswilding.net/feeder/?FeederAction=clicked&amp;feed=Posts+%28RSS2%29&amp;seed=http%3A%2F%2Fjameswilding.net%2F2010%2F09%2F04%2Fhow-to-use-rspec-with-rails-3%2F&amp;seed_title=How+To+Use+RSpec+with+Rails+3</link>
		<comments>http://jameswilding.net/2010/09/04/how-to-use-rspec-with-rails-3/#comments</comments>
		<pubDate>Sat, 04 Sep 2010 18:38:25 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[rails3]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://jameswilding.net/?p=731</guid>
		<description><![CDATA[Because Rails 3 is still pretty new (less than a week old as I write this), there&#8217;s relatively little documentation about how to use RSpec with this latest version of Rails [update: what I should have said is that docs for Rspec and Rails 3 are good, but perhaps less well-organised than for older versions of [...]]]></description>
			<content:encoded><![CDATA[<p>Because Rails 3 is still pretty new (less than a week old as I write this), <del datetime="2010-09-05T09:37:57+00:00">there&#8217;s relatively little documentation about how to use RSpec with this latest version of Rails</del> [update: what I should have said is that docs for Rspec and Rails 3 are good, but perhaps less well-organised than for older versions of Rails. See comments below.].What I want to do here is bring together the instructions I&#8217;ve found on using RSpec with Rails 3, and share some tips on how to get your BDD cycle running smoothly.</p>
<p>[Note: from this point on, I'll use "Rails" to mean "Rails 3"]</p>
<h3>Installing Rspec for Rails 3</h3>
<p>The first new thing to be aware of is the rspec-rails gem; because Rails 3 is much more modular and extensible than previous versions of the framework, this one gem can do all the work of plugging RSpec into your Rails apps.</p>
<p>Install the gem thus:</p>
<pre><code>gem install rspec-rails --pre</code></pre>
<p>The <code>--pre</code> tells Rubygems to install the beta version of the gem, which you&#8217;ll need for Rails 3 development.</p>
<p>The next step is to tell your Rails app to use the rspec-rails gem; this is done quite simply in your app&#8217;s Bundle file. Because you don&#8217;t need RSpec in production, add the gem to the development and test groups only:</p>
<pre><code># in path/to/your/app/Bundle
group :development, :test do
  # the version number may be different for you.
  # Use gem list rspec-rails --local on your command line
  # to get the exact version number.
  gem 'rspec-rails', '2.0.0.beta.20'
end
</code></pre>
<p>The final step is to run the following in the root of your Rails app:</p>
<pre><code>script/rails generate rspec:install</code></pre>
<p>This sets up a spec/ folder and some helper files.</p>
<h3>Using RSpec</h3>
<p>Now you&#8217;ve installed the rspec-rails gem, using RSpec is actually really simple. The gem tells Rails to use rspec to generate test files, which means that generators will create spec files without you having to pass in any extra options (if you check the options for <code>rails generate model</code>, you&#8217;ll see that there&#8217;s a <code>--test-framework</code> option. You <em>don&#8217;t</em> need to use this to specify RSpec; after installing rspec-rails, RSpec will be used by default).</p>
<p>So, to generate a Person model with corresponding person_spec.rb, all you need to do is this (the same applies to controllers, helpers, etc):</p>
<pre><code>rails generate model Person</code></pre>
<p>Check out spec/models/person_spec.rb to see the stub spec for your Person model. That&#8217;s really all you need to know to start using RSpec on Rails 3, but do checkout <a href="http://github.com/rspec/rspec-rails">the rspec-rails repository on Github</a>: the README gives more in-depth explanations of a lot of the points I&#8217;ve made here, and also has extra tips on writing specs for controllers, views, responses, and routes.</p>
<h3>Pro Tips</h3>
<p>A few cools things I&#8217;ve found using RSpec in my new Rails apps:</p>
<h4>Helper Specs</h4>
<p>Helpers specs have access to a special object in the <code>helper</code> object, which includes your helper. For example, in person_helper_spec.rb the helper object has access to all the methods in PersonHelper. This makes it really easy to test your helper methods:</p>
<pre><code># Assumes a PersonHelper with a #speak method.
describe PersonHelper do
  describe '#speak' do
    # roughly equivalent to:
    #
    # helper = Object.new
    # helper.send :include, PersonHelper
    # helper.speak.should == 'Hello'
    it 'says Hello' do
      helper.speak.should == 'Hello'
    end
  end
end
</code></pre>
<h4>Spec Support Files</h4>
<p>Although it&#8217;s not there by default, if you create a folder spec/support, then every file under that folder will be required when you run specs. This is really useful if you want to keep custom RSpec matchers, mocks, or other supporting code, in separate files (for example, you might want to keep custom matcher code in spec/support/my_matcher.rb and mocks in spec/support/mocks.rb).</p>
<p>Hopefully this post has helped you can a handle on how to use RSpec with the latest version of Rails. If you spot any other &#8220;pro tips&#8221;, post them in the comments! Thanks for reading.</p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilding.net/2010/09/04/how-to-use-rspec-with-rails-3/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>You Don&#8217;t Need An Apple TV&#8230;</title>
		<link>http://jameswilding.net/feeder/?FeederAction=clicked&amp;feed=Posts+%28RSS2%29&amp;seed=http%3A%2F%2Fjameswilding.net%2F2010%2F09%2F01%2Fyou-dont-need-an-apple-tv%2F&amp;seed_title=You+Don%26%238217%3Bt+Need+An+Apple+TV%26%238230%3B</link>
		<comments>http://jameswilding.net/2010/09/01/you-dont-need-an-apple-tv/#comments</comments>
		<pubDate>Wed, 01 Sep 2010 19:00:06 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://jameswilding.net/?p=727</guid>
		<description><![CDATA[&#8230;yet. One day in the near future (I give it ten years) Apple-TV-like gadgets will be ubiquitous. What constantly amazes me about Apple is the company&#8217;s ability to look into the future and see what people will want before &#8220;people&#8221; know it themselves. This is a clever trick, and one that makes Apple a ton [...]]]></description>
			<content:encoded><![CDATA[<p>&#8230;yet. One day in the near future (I give it ten years) Apple-TV-like gadgets will be ubiquitous.</p>
<p>What constantly amazes me about Apple is the company&#8217;s ability to look into the future and see what people will want before &#8220;people&#8221; know it themselves. This is a clever trick, and one that makes Apple a ton of money, but it isn&#8217;t exactly difficult: for any budding entrepreneur, the question to ask yourself is &#8220;what would be cool?&#8221;.</p>
<p>Throw away those expectations/limitations. Think fantasy, not reality. A car operated by your mind? Draw up some blueprints. It&#8217;s Apple&#8217;s ability to think not just outside, but way beyond the box that has put them ahead; they have an almost naive, child-like optimism when it comes to new products. A touchscreen phone that&#8217;s also a miniature computer? Yeah, why not.</p>
<p>And why not indeed. Cool is the way forward. If you&#8217;re going to make money you might as well do it bringing enjoyment to people&#8217;s lives, and &#8220;people&#8221; don&#8217;t care about profit margins or technological limitations, they just like stuff that&#8217;s easy and fun to use. Stuff that&#8217;s new, stuff that&#8217;s different. And stuff that feels a little bit like it&#8217;s from the future. <a href="http://www.apple.com/ipad/">Magic</a> is probably <a href="http://www.quotationspage.com/quote/776.html">the right word</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilding.net/2010/09/01/you-dont-need-an-apple-tv/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Reasons To Start Using Rails 3 Now</title>
		<link>http://jameswilding.net/feeder/?FeederAction=clicked&amp;feed=Posts+%28RSS2%29&amp;seed=http%3A%2F%2Fjameswilding.net%2F2010%2F08%2F31%2Freasons-to-use-rails-3%2F&amp;seed_title=Reasons+To+Start+Using+Rails+3+Now</link>
		<comments>http://jameswilding.net/2010/08/31/reasons-to-use-rails-3/#comments</comments>
		<pubDate>Tue, 31 Aug 2010 17:40:26 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://jameswilding.net/?p=724</guid>
		<description><![CDATA[Tempted to wait for Rails 3.1, for security updates, more features, or just to give yourself time to learn new APIs? Don&#8217;t be. Rails 3 is ready to use now; here are three reasons to start using it to build your web apps. Framework Agnosticism This is huge. In previous versions of Rails, we were locked [...]]]></description>
			<content:encoded><![CDATA[<p>Tempted to wait for Rails 3.1, for security updates, more features, or just to give yourself time to learn new APIs? Don&#8217;t be. Rails 3 is ready to use <em>now</em>; here are three reasons to start using it to build your web apps.</p>
<h3>Framework Agnosticism</h3>
<p>This is huge. In previous versions of Rails, we were locked into ActiveRecord for database abstraction, and Prototype for Javascript. Both are fine choices, but Rails 3 gives you the option to use other libraries if you want to (you can stick with the defaults as before too).</p>
<p>I love jQuery, so I&#8217;ll be using that in all my Rails 3 projects instead of Prototype. You could also use <a href="http://datamapper.org/">DataMapper</a> or <a href="http://sequel.rubyforge.org/">Sequel</a> in place of ActiveRecord, if you so desire. The important thing is that Rails 3 is more of a framework in the truest sense: a codebase into which we can plug our own favourite libraries, rather than a monolithic API without much flexibility when it comes to libraries.</p>
<h3>Improved Plugin API</h3>
<p>What better evidence do you need than this: Rails 3 itself is built on its own plugin API (internals like ActiveRecord and ActionMailer use the same code as third-party plugins to do their stuff &#8212; <a href="http://weblog.rubyonrails.org/2010/8/29/rails-3-0-it-s-done">you can read more about this on the Rails blog</a>). This makes things about 100% easier for plugin developers, and stops us from feeling like we sometimes have to dig into the dark depths of Rails, where we shouldn&#8217;t be, to make our plugins work.</p>
<h3>More Thoughtful Code</h3>
<p>As with any update, a huge amount of thought has gone into Rails 3 and I&#8217;ve been particularly impressed by the clarity of code turned out on this release. It feels like Rails has got leaner, cleaner, and better structured; this can only be good for us Rails developers in the long run: cleaner framework code means better performance and makes it easier to troubleshoot bugs (in the Rails code and in your own). <a href="http://api.rubyonrails.org/">Check out the new ActionMailer API</a> for an example: it&#8217;s a great improvement over Rails 2.x &#8212; no longer the model/controller mongrel that it used to be!</p>
<p>Those are my top three reasons to use Rails 3 <strong>now</strong>. If you&#8217;re wavering, <a href="http://api.rubyonrails.org/">jump in and give it a try</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilding.net/2010/08/31/reasons-to-use-rails-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Anti-Marketing</title>
		<link>http://jameswilding.net/feeder/?FeederAction=clicked&amp;feed=Posts+%28RSS2%29&amp;seed=http%3A%2F%2Fjameswilding.net%2F2010%2F08%2F03%2Fanti-marketing%2F&amp;seed_title=Anti-Marketing</link>
		<comments>http://jameswilding.net/2010/08/03/anti-marketing/#comments</comments>
		<pubDate>Tue, 03 Aug 2010 12:00:55 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[business]]></category>
		<category><![CDATA[marketing]]></category>

		<guid isPermaLink="false">http://jameswilding.net/?p=685</guid>
		<description><![CDATA[Everybody hates it when a random stranger calls you up and asks you if you&#8217;d be interested in [insert product or service here]. Equally so with spam emails, junk mail, and door-to-door salesmen (God bless door-to-door salesmen, but who buys kitchen cleaners on the doorstep anymore?). This is all unsurprising of course: unsolicited marketing is [...]]]></description>
			<content:encoded><![CDATA[<p>Everybody hates it when a random stranger calls you up and asks you if you&#8217;d be interested in [insert product or service here]. Equally so with spam emails, junk mail, and door-to-door salesmen (God bless door-to-door salesmen, but who buys kitchen cleaners on the doorstep anymore?).</p>
<p>This is all unsurprising of course: unsolicited marketing is impersonal, intrusive, and more often than not irrelevant to the people it targets. So, I might be putting myself in line for some criticism when I say: I&#8217;ve been promoting my website design business using sales calls for the past three years.</p>
<p>What? Yes, that&#8217;s right: sales calls. Hello sir or madam, can I interested you in bla bla bla. However, it works, and what&#8217;s more people respond very well to it for one simple reason: I make every effort not to sound like I&#8217;m selling something.</p>
<ol>
<li><strong>I&#8217;m ridiculously polite:</strong> I usually begin my calls by asking if I&#8217;ve got the right number and apologising for intruding on the person&#8217;s time. If they&#8217;re not interested, I wish them good luck with their business and thank them for their time.</li>
<li><strong>I&#8217;m friendly:</strong> if the person I&#8217;m calling wants to talk about their dog, I&#8217;ll talk about their dog (no, I&#8217;m not selling dog products). I take time answer their questions; I don&#8217;t read from a script, I respond to each person individually.</li>
<li><strong>I&#8217;m helpful: </strong>if someone wants advice, even if they&#8217;re not going to pay me money, I give it. If they have a problem with their website, I offer to email them a solution even if they&#8217;ve said no to my sales pitch. I gain nothing financially from this, but I tell you what: it makes me feel good :)</li>
</ol>
<p>I can imagine marketing professionals trembling as they read this. How many hours have I wasted chatting about some old lady&#8217;s dog when I could be making the next sale? Well, maybe a total of two or three hours over three years. But it stops me from feeling like I&#8217;m one of those awful, godless people who&#8217;s calling people out of the blue with the sole intention of getting their money. Call me crazy, but I say that&#8217;s worth the extra time.</p>
<p>I&#8217;m surprised that more businesses don&#8217;t take this approach to marketing. Every sales call I&#8217;ve ever had was from some guy/girl sat in a call centre somewhere, with zero interest in me as a person. Business is about people! People + demand + other people + supply = business. When you&#8217;re sat behind a computer, you tend to forget that. Why not go out of your way to be decent to the people who don&#8217;t pay you money, as well as those who do? At the very least, you&#8217;ll get a good reputation.</p>
<p>So: anti-marketing. Marketing that doesn&#8217;t feel like marketing. It&#8217;s the way forward.</p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilding.net/2010/08/03/anti-marketing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How To Work Fewer Hours</title>
		<link>http://jameswilding.net/feeder/?FeederAction=clicked&amp;feed=Posts+%28RSS2%29&amp;seed=http%3A%2F%2Fjameswilding.net%2F2010%2F07%2F28%2Fhow-to-work-fewer-hours%2F&amp;seed_title=How+To+Work+Fewer+Hours</link>
		<comments>http://jameswilding.net/2010/07/28/how-to-work-fewer-hours/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 12:00:29 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[business]]></category>
		<category><![CDATA[productivity]]></category>

		<guid isPermaLink="false">http://jameswilding.net/?p=675</guid>
		<description><![CDATA[In this post I want to share some advice on how you can work fewer hours without compromising productivity. If you&#8217;re self-employed or freelance, the goal here is to free up more of your time to relax and enjoy yourself or to work on other projects. If you&#8217;re an employee working for the man, our [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I want to share some advice on how you can work fewer hours without compromising productivity.</p>
<p>If you&#8217;re self-employed or freelance, the goal here is to free up more of your time to relax and enjoy yourself or to work on other projects. If you&#8217;re an employee working for the man, our aim is to cut down on the hours which you have to work to accomplish your key tasks (leaving you more time to either catch up on your todo list, or <a href="http://en.wikipedia.org/wiki/Solitaire_(Windows)">play solitaire</a>).</p>
<p>For me, cutting down your work hours is a matter of asking three simple questions about your work:</p>
<h3>1: Does This Work Need To Be Done?</h3>
<p>The first question is the most obvious &#8212; cut out the things that aren&#8217;t absolutely essential, and you&#8217;ll save yourself time. The criteria by which you judge a task&#8217;s importance will vary, but a good rule of thumb is this: if you don&#8217;t do this &#8220;important&#8221; task, will the consequences be irreversible? You&#8217;ll be surprised how many times you&#8217;ll answer &#8220;no&#8221; to that question.</p>
<h3>2: Do <em>I</em> Need To Be Doing This?</h3>
<p>Often it&#8217;s faster and more productive to have someone else do something for you &#8212; especially when the other person is an expert and you&#8217;re not. Don&#8217;t be afraid to delegate and free up your time to focus on the essentials.</p>
<p>By the way, this question is different to &#8220;Do I <em>want</em> to be doing this&#8221;! Sorry. Sometimes you have to work on something that you don&#8217;t want to work on: that&#8217;s unavoidable. The idea is to delegate work which can be done equally well, or better, by someone else.</p>
<h3>3: How Can This Be Done Better?</h3>
<p>Not &#8220;done faster&#8221;; &#8220;done <em>better</em>&#8220;. This question helps you future-proof your work: the best way to avoid emergencies and panic in the future is to do a good job now.</p>
<p>Make a list of the key goals of whatever project you&#8217;re working on, and ask yourself whether you&#8217;re going about accomplishing those goals in the best way. Change isn&#8217;t always necessary, but sometimes you&#8217;ll realise that a lot needs to be altered &#8212; that can be daunting, but it&#8217;s worth putting in a little more time now to save yourself a lot of time in the future.</p>
<h3>The Results</h3>
<p><strong>Does this work in practice?</strong> For the past three months I&#8217;ve been using this strategy and I&#8217;d say I&#8217;m around 25% more productive, and 25% less busy. Most importantly, I feel much more relaxed and on top of my work. So give it a try &#8212; I hope you get the same results.</p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilding.net/2010/07/28/how-to-work-fewer-hours/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Work Without Working</title>
		<link>http://jameswilding.net/feeder/?FeederAction=clicked&amp;feed=Posts+%28RSS2%29&amp;seed=http%3A%2F%2Fjameswilding.net%2F2010%2F07%2F26%2Fwork-without-working%2F&amp;seed_title=Work+Without+Working</link>
		<comments>http://jameswilding.net/2010/07/26/work-without-working/#comments</comments>
		<pubDate>Mon, 26 Jul 2010 12:00:40 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[buddhism]]></category>
		<category><![CDATA[business]]></category>
		<category><![CDATA[meditation]]></category>
		<category><![CDATA[productivity]]></category>
		<category><![CDATA[zen]]></category>

		<guid isPermaLink="false">http://jameswilding.net/?p=617</guid>
		<description><![CDATA[Real productivity comes from creativity, and to get creative you absolutely have to let your mind relax. That&#8217;s when the good stuff comes: clever ideas, cool solutions to old problems, and that magic feeling that get when things get done effortlessly. Work is too much like work You know that feeling you get when you&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p>Real productivity comes from creativity, and to get creative you absolutely have to let your mind relax. That&#8217;s when the good stuff comes: clever ideas, cool solutions to old problems, and that magic feeling that get when things get done effortlessly.</p>
<h3>Work is too much like work</h3>
<p>You know that feeling you get when you&#8217;ve been thinking long and hard over a particular problem: your head feels like mush, your eyes hurt, you can&#8217;t focus. Your mind feels tight, it feels like you&#8217;ve lost touch with your real energy. After close to ten years&#8217; of meditation, I can tell you that this is a Bad Thing.</p>
<p>My suggestion is that &#8220;work&#8221;, in the traditional sense of the word, is one of the worst ways to get things done. Work means mental exertion, which in most cases nowadays means a ridiculous focus on logic, and the idea that &#8220;nose to the grindstone&#8221; is the only way to get results.</p>
<p>Doesn&#8217;t that sound awful? How about relaxing and letting your subconscious take care of things, instead. It really works: I&#8217;ve lost count of the numbers of times a solution to a difficult problem had come to me, without effort, while I&#8217;ve been walking, cooking, or playing guitar.</p>
<h3>Stop thinking, stop trying</h3>
<p>The burden of &#8220;work&#8221; is too much thinking. Logic, planning, analysis: all of these are good as part of an holistic approach, but bad when they take the lead on business decisions. They serve a purpose, but intuition and the subconscious are much more powerful. Yes, that&#8217;s right: I run my business on gut instinct. It&#8217;s so much more effective!</p>
<p>My advice is to let your mind slip back into <a href="http://www.wisegeek.com/what-are-alpha-waves.htm">alpha waves</a>, and let &#8220;important&#8221; decisions take care of themselves. My approach nowadays is to arm myself with all the facts, then completely ignore the situation for a few days: meditate, go walking, work on something else. After my subconscious has had the time to work on the problem, the answer presents itself naturally, and without effort.</p>
<h3>Making it work for you</h3>
<p>This all sounds very Zen, but how does it apply to you? Well, just try it. You can&#8217;t control your own mind, but you can give your mind a break: next time you catch yourself thinking away too hard at a problem, just drop it. Leave the office, go outside, listen to music, do something relaxing. And I think &#8220;relaxing&#8221; is the key word here: you need to put yourself into situations that allow you to unwind, situations that don&#8217;t require logic: this gives your mind the space it needs to work creatively.</p>
<p>Ignore the received &#8220;wisdom&#8221; that says you have to work hard (and think hard) in order to get things done.</p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilding.net/2010/07/26/work-without-working/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby&#8217;s OpenStruct: An Introduction</title>
		<link>http://jameswilding.net/feeder/?FeederAction=clicked&amp;feed=Posts+%28RSS2%29&amp;seed=http%3A%2F%2Fjameswilding.net%2F2010%2F07%2F24%2Fruby-openstruct%2F&amp;seed_title=Ruby%26%238217%3Bs+OpenStruct%3A+An+Introduction</link>
		<comments>http://jameswilding.net/2010/07/24/ruby-openstruct/#comments</comments>
		<pubDate>Sat, 24 Jul 2010 13:43:06 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://jameswilding.net/?p=714</guid>
		<description><![CDATA[OpenStructs are magic &#8212; they&#8217;re like hashes, but far less fussy about what methods you throw at them. The key feature of OpenStructs is this: they allow you to arbitrarily set and access attributes for your models, on the fly. Here&#8217;s an example: require 'ostruct' s = OpenStruct.new s.name # =&#62; nil s.name = 'James' [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://ruby-doc.org/core/classes/OpenStruct.html">OpenStructs</a> are magic &#8212; they&#8217;re like hashes, but far less fussy about what methods you throw at them. The key feature of OpenStructs is this: they allow you to arbitrarily set and access attributes for your models, on the fly.</p>
<p>Here&#8217;s an example:</p>
<pre><code>require 'ostruct'

s = OpenStruct.new
s.name # =&gt; nil
s.name = 'James'
s.name # =&gt; 'James'
</code></pre>
<p>Note that we don&#8217;t need to initialise the &#8216;name&#8217; attribute: we just set the value and Ruby takes care of the rest. Want to add a new attribute? Easy.</p>
<pre><code>s.age # =&gt; nil
s.age = 29
s.age # =&gt; 29
</code></pre>
<p>We can add any attribute we want to our objects, on the fly, and <strong>different instances of the OpenStruct class can have different attributes</strong>. Importantly, we don&#8217;t have to worry about what methods we send to our OpenStructs, because anything that doesn&#8217;t have a value just returns nil <a class="simple-footnote" title="This can be a curse as well as a blessing, because you&#8217;ll often get nil when you&#8217;d expect Ruby to raise a NoMethodError. Checkout OpenStruct&#8217;s method_missing to see what&#8217;s going on behind the scenes." id="return-note-714-1" href="#note-714-1"><sup>1</sup></a>:</p>
<pre><code>s.location # =&gt; nil
s.hobbies # =&gt; nil
</code></pre>
<h3>Clearing Values</h3>
<p>To unset a value, we can do one of two things: in most cases, it&#8217;s the obvious&#8230;</p>
<pre><code>2.age # =&gt; 29
s.age = nil
s.age # =&gt; nil
</code></pre>
<p>&#8230;but to grab the value before setting the attribute back to nil, we can use delete_field <a class="simple-footnote" title="This is one of OpenStruct&#8217;s few built-in instance methods which don&#8217;t act as attribute accessors. Check the docs for details." id="return-note-714-2" href="#note-714-2"><sup>2</sup></a>:</p>
<pre><code># Returns 29, and sets 'age' to nil
age = s.delete_field('age')
age # =&gt; 29
s.age # =&gt; nil
</code></pre>
<h3>Initialising With Values</h3>
<p>We can create new OpenStruct objects with attribute/value pairs by providing a hash:</p>
<pre><code>s = OpenStruct.new(:name =&gt; 'James', :occupation =&gt; 'Rubyist')
s.name # =&gt; 'James'
s.occupation # =&gt; 'Rubyist'
</code></pre>
<h3>How It Works</h3>
<p>Behind the scenes, Ruby uses a hash to implement OpenStruct&#8217;s easy access so we get all the flexibility of a hash with a nice, clean, method-style access laid on top. Don&#8217;t worry about the details: just throw whatever you want into your OpenStructs, and let Ruby take care of your data.</p>
<h3>Advanced Initialization</h3>
<p>If you want object with the flexibility of OpenStructs but with default values for some attributes then something like the following would work:</p>
<pre><code>require 'ostruct'

class MyStruct &lt; OpenStruct
  # Hard-code age and status
  def initialize(hash = {})
    super hash.merge(:age =&gt; 29, :role =&gt; 'Manager')
  end
end

s = MyStruct.new(:name =&gt; 'Bob')
s.name # =&gt; 'Bob'
s.age # =&gt; 29
s.role # =&gt; 'Manager'

# We can override the default values
s = MyStruct.new(:age =&gt; 45)
s.age # =&gt; 45
</code></pre>
<h3>OpenStruct vs Struct</h3>
<p>Ruby&#8217;s Struct is subtly, but importantly, different to OpenStruct. How to use Struct is another blog post, but put simply Structs are less flexible. Check the <a href="http://ruby-doc.org/core/classes/Struct.html">Struct docs</a> for more info :)</p>
<div class="simple-footnotes"><p class="notes">Notes:</p><ol><li id="note-714-1">This can be a curse as well as a blessing, because you&#8217;ll often get nil when you&#8217;d expect Ruby to raise a NoMethodError. Checkout <a href="http://gist.github.com/488692">OpenStruct&#8217;s method_missing</a> to see what&#8217;s going on behind the scenes. <a href="#return-note-714-1">&#8617;</a></li><li id="note-714-2">This is one of OpenStruct&#8217;s few built-in instance methods which don&#8217;t act as attribute accessors. Check <a href="http://ruby-doc.org/core/classes/OpenStruct.html">the docs</a> for details. <a href="#return-note-714-2">&#8617;</a></li></ol></div>]]></content:encoded>
			<wfw:commentRss>http://jameswilding.net/2010/07/24/ruby-openstruct/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Paperclip on Rails 3</title>
		<link>http://jameswilding.net/feeder/?FeederAction=clicked&amp;feed=Posts+%28RSS2%29&amp;seed=http%3A%2F%2Fjameswilding.net%2F2010%2F07%2F24%2Fpaperclip-rails-3%2F&amp;seed_title=Paperclip+on+Rails+3</link>
		<comments>http://jameswilding.net/2010/07/24/paperclip-rails-3/#comments</comments>
		<pubDate>Sat, 24 Jul 2010 10:56:39 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[paperclip]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rails3]]></category>

		<guid isPermaLink="false">http://jameswilding.net/?p=695</guid>
		<description><![CDATA[You want to run Rails 3. You want to use the excellent Paperclip plugin to mess with your attached files. Not a problem. It Just Works Paperclip on Rails 3 now just works. Back in February 2010, I wrote about how to use Paperclip on an early beta release of Rails 3 &#8212; back then, [...]]]></description>
			<content:encoded><![CDATA[<p>You want to run Rails 3. You want to use <a href="http://github.com/thoughtbot/paperclip">the excellent Paperclip plugin</a> to mess with your attached files. Not a problem.</p>
<h3>It Just Works</h3>
<p>Paperclip on Rails 3 now just works.</p>
<p><a href="http://jameswilding.net/2010/02/07/paperclip-on-rails-3-beta/">Back in February 2010</a>, I wrote about how to use Paperclip on an early beta release of Rails 3 &#8212; back then, a few small hacks were required to get Paperclip running. Now Rails 3 is at beta 4, and it&#8217;s safe to assume that we&#8217;re as close as we can be to an official Rails release &#8212; minus a few last minute tweaks, of course &#8212; and both the framework itself and the Paperclip plugin are more stable and more compatible. The good news, then, is that it&#8217;s very easy to use the Rails 3 and Paperclip together.</p>
<h3>How To Use Paperclip With Rails 3</h3>
<p>These instructions are good for Rails 3 beta 4, and Paperclip <a href="http://github.com/thoughtbot/paperclip/commit/9223a917a821731986603e89a9b2e4d1a4fdd68a">9223a917</a>. They&#8217;ll probably work on other versions of Rails and Paperclip, too.</p>
<ol>
<li>Install Rails 3</li>
<li>Create a new Rails app</li>
<li>Install Paperclip</li>
<li>Create a model</li>
<li>Create a controller and views</li>
<li>Define your routes</li>
</ol>
<p>You can find the important files (controller, model, views, and routes) in <a href="http://gist.github.com/488579">this gist</a>, and the full application code is <a href="http://github.com/jameswilding/paperclip_example">available on Github</a>.</p>
<h4>Install Rails 3</h4>
<p>Install the latest prerelease of Rails with the following:</p>
<pre>$ gem install rails --pre</pre>
<h4>Create a new Rails app</h4>
<p>The command for creating Rails apps has changed in Rails 3. In the bright new future, everything is done using &#8220;rails&#8221; <a class="simple-footnote" title="I&#8217;ve found that, on Mac OSX 10.6, I have to use &#8220;/usr/bin/rails&#8221; (that&#8217;s where my Rails binary lives) instead of &#8220;rails&#8221;. No doubt that will be fixed soon." id="return-note-695-1" href="#note-695-1"><sup>1</sup></a>:</p>
<pre>$ rails new paperclip_example</pre>
<h4>Install Paperclip</h4>
<p>Paperclip&#8217;s master branch is now good to go with Rails 3. Install from Github thus:</p>
<pre>$ rails plugin install git://github.com/thoughtbot/paperclip.git</pre>
<h4>Create a model</h4>
<p>In order to work with Paperclip, your model needs a few special database columns. In this case, I&#8217;m creating a User class which will have an attached avatar image.</p>
<pre>$ rails generate model User \
avatar_file_name:string \
avatar_content_type:string \
avatar_file_size:integer \
avatar_updated_at:datetime
$ rake db:migrate</pre>
<p>I use Paperclip&#8217;s has_attached_file method to define my avatar attachment:</p>
<pre>class User &lt; ActiveRecord::Base
  has_attached_file :avatar, :styles =&gt; {
    :thumb =&gt; '50x'
  }
end</pre>
<h4>Create a controller and views</h4>
<p>I&#8217;m setting up a simple UsersController with index, new, and create actions (and corresponding routes). You&#8217;ll probably want to go further, but if all you&#8217;re interested in is proof-of-concept then this is enough.</p>
<pre>$ rails generate controller Users</pre>
<p>See <a href="http://gist.github.com/488579">this gist</a> for the code in my UsersController, and the HTML in my views.</p>
<h4>Define your routes</h4>
<p>In config/routes.rb:</p>
<pre>PaperclipExample::Application.routes.draw do |map|
  resources :users, :only =&gt; [:index, :new, :create]
  root :to =&gt; 'users#index'
end</pre>
<p>Lastly, if you&#8217;re mapping your app&#8217;s root to a controller, remember to delete public/index.html.</p>
<h3>And You&#8217;re Done</h3>
<p>That&#8217;s really all there is to it. Use rails server to start your app and view the results. Did it work for you?</p>
<div class="simple-footnotes"><p class="notes">Notes:</p><ol><li id="note-695-1">I&#8217;ve found that, on Mac OSX 10.6, I have to use &#8220;/usr/bin/rails&#8221; (that&#8217;s where my Rails binary lives) instead of &#8220;rails&#8221;. No doubt that will be fixed soon. <a href="#return-note-695-1">&#8617;</a></li></ol></div>]]></content:encoded>
			<wfw:commentRss>http://jameswilding.net/2010/07/24/paperclip-rails-3/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Manifest: My Favourite Simple WordPress Theme</title>
		<link>http://jameswilding.net/feeder/?FeederAction=clicked&amp;feed=Posts+%28RSS2%29&amp;seed=http%3A%2F%2Fjameswilding.net%2F2010%2F07%2F21%2Fmanifest-the-best-simple-wordpress-theme%2F&amp;seed_title=Manifest%3A+My+Favourite+Simple+WordPress+Theme</link>
		<comments>http://jameswilding.net/2010/07/21/manifest-the-best-simple-wordpress-theme/#comments</comments>
		<pubDate>Wed, 21 Jul 2010 14:00:44 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[blogging]]></category>
		<category><![CDATA[manifest]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://jameswilding.net/?p=614</guid>
		<description><![CDATA[Looking for a simple WordPress theme recently, I stumbled on Manifest. Well, I love it. There are so many WordPress themes out there that (in my opinion) go overboard on the graphics, but Manifest  takes the opposite approach: content comes first (as you can see: I&#8217;m using it right here on my blog). Jim Barraud, [...]]]></description>
			<content:encoded><![CDATA[<p>Looking for a simple WordPress theme recently, I stumbled on <a href="http://jimbarraud.com/manifest/">Manifest</a>. Well, I love it. There are so many WordPress themes out there that (in my opinion) go overboard on the graphics, but Manifest  takes the opposite approach: content comes first (as you can see: I&#8217;m using it right here on my blog).</p>
<p>Jim Barraud, Manifest&#8217;s creator, describes the theme thus:</p>
<blockquote><p>A clean and streamlined theme that focused on the content and not the distractions. It utilizes a single column, 500 pixel wide layout. No sidebars. No widgets.</p></blockquote>
<p>There&#8217;s so much to love about that quote. Single column. Focus on content. No sidebars. No widgets. Simplicity, simplicity, simplicity.</p>
<p>I&#8217;ve always loved websites and blogs which are pared right back to the bone &#8212; nearly too simple &#8212; for the simple reason that I spend so much time on the web! After surfing through websites which bash me round the head with flashy graphics, it&#8217;s such a relief when I come to rest at a site designed by someone who knows the meaning of <a href="http://en.wikipedia.org/wiki/White_space_(visual_arts)">negative space</a> and subdued (if any) graphics.</p>
<p>And aspiring writers: this is the kind of presentation that you should be wrapping around your lovingly crafted blog posts: let your language speak for itself.</p>
<p>So: a beautiful, simple, elegant theme: <a href="http://jimbarraud.com/manifest/">download Manifest here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilding.net/2010/07/21/manifest-the-best-simple-wordpress-theme/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DODOCase for iPad</title>
		<link>http://jameswilding.net/feeder/?FeederAction=clicked&amp;feed=Posts+%28RSS2%29&amp;seed=http%3A%2F%2Fjameswilding.net%2F2010%2F07%2F20%2Fdodocase-for-ipad%2F&amp;seed_title=DODOCase+for+iPad</link>
		<comments>http://jameswilding.net/2010/07/20/dodocase-for-ipad/#comments</comments>
		<pubDate>Tue, 20 Jul 2010 13:43:20 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[dodocase]]></category>
		<category><![CDATA[ipad]]></category>

		<guid isPermaLink="false">http://jameswilding.net/?p=665</guid>
		<description><![CDATA[DODOCase is a Moleskinesque case for the iPad, hand-made using bamboo and fabric. It looks lovely, and is the first iPad case I&#8217;ve seen which strikes the right balance between form and function (unlike Apple&#8217;s own iPad case, which looks like something out of Star Trek). I don&#8217;t even own an iPad, but I&#8217;m already [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.dodocase.com/">DODOCase</a> is a <a href="http://www.moleskine.co.uk/">Moleskinesque</a> case for the iPad, hand-made using bamboo and fabric. It looks lovely, and is the first iPad case I&#8217;ve seen which strikes the right balance between form and function (unlike Apple&#8217;s own iPad case, which looks like something out of Star Trek).</p>
<p><a title="DodoCase for iPad by adamjackson1984, on Flickr" href="http://www.flickr.com/photos/adamjackson/4747541391/"><img src="http://farm5.static.flickr.com/4121/4747541391_3622cb2167.jpg" alt="DodoCase for iPad" width="500" height="375" /></a></p>
<p>I don&#8217;t even own an iPad, but I&#8217;m already blogging about the DODOCase  and have bookmarked the website, so kudos to these guys for excellent marketing!</p>
<p class="subtle">Photo by <a href="http://www.flickr.com/photos/adamjackson/4747541391/">adamjackson</a> on Flickr (<a href="http://creativecommons.org/licenses/by-nd/2.0/deed.en">license</a>)</p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilding.net/2010/07/20/dodocase-for-ipad/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
