<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>The Modest Rubyist</title>
  <id>http://www.themodestrubyist.com</id>
  <updated>2010-02-21T00:00:00-08:00</updated>
  <author>
    <name>Jordan Ryan West</name>
  </author>
  <entry>
    <title>Welcome to the Modest Rubyist</title>
    <link href="http://www.themodestrubyist.com/2010/02/21/welcome-to-the-modest-rubyist/" rel="alternate"/>
    <id>http://www.themodestrubyist.com/2010/02/21/welcome-to-the-modest-rubyist/</id>
    <published>2010-02-21T00:00:00-08:00</published>
    <updated>2010-02-21T00:00:00-08:00</updated>
    <author>
      <name>Jordan Ryan West</name>
    </author>
    <summary type="html">&lt;p&gt;To get off on the right foot, I&amp;rsquo;ll be 100% honest, I&amp;rsquo;m not sure what
the long term goal of starting this blog is&amp;hellip;&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;To get off on the right foot, I&amp;rsquo;ll be 100% honest, I&amp;rsquo;m not sure what
the long term goal of starting this blog is. Right now I believe it
has a couple purposes. First, I really wanted to get my hands on
&lt;a href="http://github.com/cloudhead/toto"&gt;toto&lt;/a&gt;. Toto really is all it claims
to be by the way. It took 5 minutes to setup and learn how to
use. Launching on heroku was quick. See &lt;a href="http://www.rubyinside.com/deploy-blog-with-toto-and-heroku-2962.html"&gt;this article on rails
inside&lt;/a&gt;
for easy deployment on heroku, which is how this blog is hosted. My
second reason for starting this blog is because &lt;a href="http://jrwest.tumblr.com"&gt;my
tumblelog&lt;/a&gt; is starting to get cluttered with
the mixture of ruby and random blog posts.&lt;/p&gt;

&lt;p&gt;I have been programming in Ruby for a little over a year now and have, like
many others, I really fallen in love. The Ruby community is great, making
it an easy language to pick up quickly. I&amp;rsquo;m hoping this blog may one
day give a little back.&lt;/p&gt;

&lt;p&gt;Coming from PHP, I fell in love with the ease of RubyGems, never
loving PEAR. Ruby has examples of simple to use interfaces all over
and as rubyists we really appreciate them. I still use PHP because all
of &lt;a href="http://westwardwd.com/portfolio"&gt;my clients&lt;/a&gt; still use PHP powered
websites. However, most of my day-to-day coding outside of work is
done in Ruby.&lt;/p&gt;

&lt;p&gt;I will be posting my own articles on this blog as well as resources I
have found useful in my own work. Maybe someone besides me will one
day find it useful.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Toto es Todo: Blogging Made Real Easy</title>
    <link href="http://www.themodestrubyist.com/2010/02/22/toto-es-todo-blogging-made-real-easy/" rel="alternate"/>
    <id>http://www.themodestrubyist.com/2010/02/22/toto-es-todo-blogging-made-real-easy/</id>
    <published>2010-02-22T00:00:00-08:00</published>
    <updated>2010-02-22T00:00:00-08:00</updated>
    <author>
      <name>Jordan Ryan West</name>
    </author>
    <summary type="html">&lt;p&gt;Please excuse my spanish, I used to be fluent in French so I
approximated. I thought it fitting that I blog about
&lt;a href="http://cloudhead.io/toto"&gt;toto&lt;/a&gt; and &lt;a href="http://www.heroku.com"&gt;Heroku&lt;/a&gt; the really
awesome software powering &lt;a href="www.themodestrubyist.com"&gt;The Modest
Rubyist&lt;/a&gt;&amp;hellip;&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;Please excuse my spanish, I used to be fluent in French so I
approximated. I thought it fitting that I blog about
&lt;a href="http://cloudhead.io/toto"&gt;toto&lt;/a&gt; and &lt;a href="http://www.heroku.com"&gt;Heroku&lt;/a&gt; the really
awesome software powering &lt;a href="www.themodestrubyist.com"&gt;The Modest
Rubyist&lt;/a&gt;. In this post I will talk a bit about how I
setup &lt;a href="www.themodestrubyist.com"&gt;The Modest Rubyist&lt;/a&gt; using these two
tools but mostly about why they are so awesome.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://cloudhead.io/toto"&gt;Toto&lt;/a&gt; is blogging using tools
you as a rubyist are probably intimately familiar with: your text
editor and, hopefully, git. Each article is simply a .txt
file. Mix in some YAML at the top of the file for metadata and
&lt;a href="http://daringfireball.net/projects/markdown/"&gt;Markdown&lt;/a&gt; in the body
for flavor and a toto post you have.&lt;/p&gt;

&lt;p&gt;The quickest, and suggested way to get up and running with toto is to
clone &lt;a href="http://github.com/cloudhead/dorothy"&gt;Dorthy&lt;/a&gt;. This repository
is a skeleton blog running toto. Toto is built on top of Rack so
Dorthy provides a simple config.ru file with enough code comments to
easily understand what is going on. ERB is used for templating so if
your familiar with the basics of Rails views you will be able to tweak
the look &amp;amp; feel of your blog. Dorthy comes with a basic template which
I found to be a great starting point. Dorthy&amp;rsquo;s template uses HTML5&amp;rsquo;s
article &amp;amp; section tags which is a plus, in my opinion.&lt;/p&gt;

&lt;p&gt;Once you have Dorthy on your machine you can view it as is by starting
it up using one of several Ruby web servers. I chose &lt;a href="http://code.macournoyer.com/thin/"&gt;Thin&lt;/a&gt;. To boot up
your blog locally with Thin type the following at the command line:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Thin start -R config.ru
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can install Thin with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gem install thin
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You will need g++ installed. I had no problems on my laptop running
Ubuntu 9.10.&lt;/p&gt;

&lt;p&gt;Blogging is pretty useless however if only you can see it on your
local machine. Toto was designed with cloud hosting like
&lt;a href="http://heroku.com"&gt;Heroku&lt;/a&gt; in mind. Instead of rewriting the
instructions I used to get up and running on Heroku you can read &lt;a href="http://www.rubyinside.com/deploy-blog-with-toto-and-heroku-2962.html"&gt;this
great write up&lt;/a&gt;
on &lt;a href="http://www.railsinside.com"&gt;Rails Inside&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Once your setup on Heroku, its time to write your first article. Add a
new .txt file in the articles directory of your blog root directory
following the naming convention of the file provided with Dorthy. Use
that same file as a template for your article. There should be 3
simple YAML fields at the top of your file (title, author, date),
followed by a blank line, and then the body of your post. Remove the
example article, commit your changes and push up to Heroku. Your new
post is live to the world.&lt;/p&gt;

&lt;p&gt;Now that you&amp;rsquo;ve seen the ease of Toto I&amp;rsquo;d like to direct your
attention to some other advantages you may or may not have
noticed. One reason I really like toto is how easy it is to backup not
only your content but your entire layout, as well. If you create a new GitHub
repository for example you can remove the origin remote pointing to
Dorthy and point a new remote at your repository. To back up everything
just do a push to it. Want to experiment with a new design idea that
you came up with? Branch out in your local
repository, work on your changes and simply merge &amp;lsquo;em in when your
ready. Back up as often as you please; make the changes public by doing another push to Heroku.
Another advantage I have heard tossed around is, &amp;ldquo;Go ahead, try to hack my toto
admin panel! You will be looking for it forever, it exists on my hard
drive, muahhaha!&amp;rdquo;&lt;/p&gt;

&lt;p&gt;As you can see toto is one awesome piece of Ruby goodness. It may not
be software we can sell our clients but its a great way for the rubyist with
a want to express him/herself, blog style, easily. One last resource
I&amp;rsquo;d like to leave you with is these
&lt;a href="http://blog.heroku.com/archives/2009/10/7/heroku_casts_setting_up_custom_domains/"&gt;screencasts&lt;/a&gt;
by Oren at Heroku. This helpful walkthrough shows how to setup a
domain purchased from &lt;a href="http://www.godaddy.com"&gt;GoDaddy&lt;/a&gt; to point to your blog. You will
need to provide Heroku with some billing information but it is for
fraud prevention purposes only, you will not be charged (I am
personally vouching for this as I went through this process last night). The
screencasts show how to both setup your blog with or without email. I
chose to not have email for this domain for now. If you do plan to use
email you should watch both videos in order. Also, don&amp;rsquo;t be alarmed if
GoDaddy takes some time to propogate your DNS changes. Unlike the
screencasts, the newest update to GoDaddy&amp;rsquo;s Total DNS Panel removed the
status listings. You will need to wait a little longer than Oren makes
it seem to run commands such as host &amp;lt;your domain&gt; to complete the setup process.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>When Monkey Patching Goes Wrong</title>
    <link href="http://www.themodestrubyist.com/2010/02/22/when-monkey-patching-goes-wrong/" rel="alternate"/>
    <id>http://www.themodestrubyist.com/2010/02/22/when-monkey-patching-goes-wrong/</id>
    <published>2010-02-22T00:00:00-08:00</published>
    <updated>2010-02-22T00:00:00-08:00</updated>
    <author>
      <name>Jordan Ryan West</name>
    </author>
    <summary type="html">&lt;p&gt;One of Ruby&amp;rsquo;s coolest features is the ability to open up an existing
class and modify its behaviours. There are times when this can go
incredibly wrong however. &lt;a href="http://aslakhellesoy.com/"&gt;Aslak Hellesoy&lt;/a&gt;
reports a &lt;a href="https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4034-monkey-patched-erb-breaks-other-libraries-relying-on-standard-behaviour-from-erb"&gt;Rails
bug&lt;/a&gt;
which breaks the Ruby standard library, ERB, in
turn breaking his wonderful BDD tool, &lt;a href="http://cukes.info"&gt;cucumber&lt;/a&gt;&amp;hellip;&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;One of Ruby&amp;rsquo;s coolest features is the ability to open up an existing
class and modify its behaviours. There are times when this can go
incredibly wrong however. &lt;a href="http://aslakhellesoy.com/"&gt;Aslak Hellesoy&lt;/a&gt;
reports a &lt;a href="https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4034-monkey-patched-erb-breaks-other-libraries-relying-on-standard-behaviour-from-erb"&gt;Rails
bug&lt;/a&gt;
which breaks the Ruby standard library, ERB, in
turn breaking his wonderful BDD tool, &lt;a href="http://cukes.info"&gt;cucumber&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Although many people have developed awesome new tools to do great
things with Ruby we need to remember its great core features as
well. As Aslak points out, the Rails team should have subclassed ERB
for their purposes and used that throughout Rails core. It wont affect
any developer using Rails because ERB is completely abstracted away
and we work with templates.&lt;/p&gt;

&lt;p&gt;This is not to say monkey patching is always bad. But the Rails team
modified a core assumption of ERB: that output was not escaped. By
modifying the public ERB API directly other applications were fatally
affected.&lt;/p&gt;

&lt;p&gt;So back to the subclass. Lets just take a simple example using
ERB. The intetion of the Rails core was to, by default, escape
possibly malicious, HTML/Javascript when using &amp;lt;%= %&gt;. This is a
change over from the previous version of Rails where you would need to
write &amp;lt;%=h %&gt;.&lt;/p&gt;

&lt;p&gt;Again keeping it simple, say we want to pass our string of ERB &amp;ldquo;&amp;lt;%=
my_string %&gt;&amp;rdquo; to ERB.new. From ERB&amp;rsquo;s docs we would assume that if
my_string contains any HTML tags they would be included in the
output. However, the Rails monkey patch changes this assumption. This is
a big no-no. Instead there should be some class like the following&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;   class SafeERB &amp;lt; ERB
      def initialize(string)   # ERB.new takes more args
                               # but I'm just stubbing here

        # code that parses ERB safely like &amp;lt;%=h %&amp;gt;
      end
    end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Not implementing safe html_escapee by default in this way is what caused this bug in a tool
used by many developers. Obviously not acceptable. Of course, its
isolated to cucumber&amp;rsquo;s use with actionpack (I think this is where view
stuff is still done in Rails but I may be wrong) but this is still very
important.&lt;/p&gt;

&lt;p&gt;As programmers we are often given many hammers to pound the same
nail. We need to remember some hammers are better suited for certain
situations. Just as a framing hammer is better for nailing together
a traditional suburban home and is not for building a log home,
certain programming techinques &amp;amp; idioms are better suited for certain problems
based on context.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Rails 3, UJS, and CSRF Meta Tags</title>
    <link href="http://www.themodestrubyist.com/2010/02/24/rails-3-ujs-and-csrf-meta-tags/" rel="alternate"/>
    <id>http://www.themodestrubyist.com/2010/02/24/rails-3-ujs-and-csrf-meta-tags/</id>
    <published>2010-02-24T00:00:00-08:00</published>
    <updated>2010-02-24T00:00:00-08:00</updated>
    <author>
      <name>Jordan Ryan West</name>
    </author>
    <summary type="html">&lt;p&gt;The past couple of days I have really had a chance to play around with
writing new apps in Rails 3 beta. One thing I am really excited about
is Unobtrusive Javascript (UJS) support in Rails 3. There is one thing
you will need to know to get up and running with UJS in Rails 3&amp;hellip;&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;The past couple of days I have really had a chance to play around with
writing new apps in Rails 3 beta. One thing I am really excited about
is Unobtrusive Javascript (UJS) support in Rails 3. There is one thing
you will need to know to get up and running with UJS in Rails 3.&lt;/p&gt;

&lt;p&gt;I was staring for almost an hour at a simple view last night that
used the standard,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; link_to "delete", model, :confirm =&amp;gt; "Are you sure?", :method =&amp;gt; :delete
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;After reading the Rails 3 Javscript event handlers and pulling my hair
out searching for if Rails 3 changed anything, the logs were still
logging a GET request. I was never seeing the confirm dialog. Well it
turns out there is &lt;a href="https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4084-ujs-silently-fails-when-csrf_meta_tag-is-not-present"&gt;a little something you need added to your layout in
the head section&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; csrf_meta_tag
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;csrf_meta_tag is a helper that returns two HTML &lt;meta&gt; tags. They
include information necessary for Rails' new, beefy XSS support with
good old protect_from_fogery. You will need these for Rails'
Javascript to work and you will need it for security.&lt;/p&gt;

&lt;p&gt;On to UJS in Rails 3. In previous version of Rails I attempted
to stay away from the usage of Javascript helpers and things like
:method =&gt; :delete because my pure JS background made me want to throw
up at the sight of those ugly onclick and onsubmit inline event
handlers. Rails 3 does away with all this and uses &lt;a href="http://ejohn.org/blog/html-5-data-attributes/"&gt;HTML5&amp;rsquo;s custom data
attributes&lt;/a&gt;. When you use :method =&gt; :delete and :confirm now you
will see two new attributes in your links: data-method and
data-confirm. Rails 3 uses these &lt;a href="http://ejohn.org/blog/html-5-data-attributes/"&gt;custom data attributes&lt;/a&gt; to
perform the same tasks as before but unobtrusively. This is one of
the most important facelifts of ActionPack.&lt;/p&gt;

&lt;p&gt;Rails 3 also changes how we use remote form and link helpers. In Rails 2
you would write:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;remote_form_for(@post)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;in Rails 3:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;form_for(@post, :remote =&amp;gt; true)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For links we now write&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;link_to "some action", my_action_path(@post), :remote =&amp;gt; true
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The above will add data-remote=&amp;ldquo;true&amp;rdquo;. If you would like to change the
HTTP method performed also pass the :method option. It will use the
sam data-method attribute used for delete links.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Rails 3 Plugins - Part 1 - The Big Picture</title>
    <link href="http://www.themodestrubyist.com/2010/03/01/rails-3-plugins---part-1---the-big-picture/" rel="alternate"/>
    <id>http://www.themodestrubyist.com/2010/03/01/rails-3-plugins---part-1---the-big-picture/</id>
    <published>2010-03-01T00:00:00-08:00</published>
    <updated>2010-03-01T00:00:00-08:00</updated>
    <author>
      <name>Jordan Ryan West</name>
    </author>
    <summary type="html">&lt;p&gt;Digging deeper into Rails 3, this week, I spent a lot of time with the
new Railtie API (Plugin API). I thought I would share some of what I
learned. There is a lot of information. In this post I will address
the big picture and things of note I found while working on a couple
plugins&amp;hellip;&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;Digging deeper into Rails 3, this week, I spent a lot of time with the
new Railtie API (Plugin API). I thought I would share some of what I
learned. There is a lot of information. In this post I will address
the big picture and things of note I found while working on a couple
plugins.&lt;/p&gt;

&lt;p&gt;The Rails Plugin API got a huge overhaul in Rails 3 (parts of it were
included in Rails 2.3 although not in such a robust fashion). While a lot of
things have changed, a lot has stayed the same as well.&lt;/p&gt;

&lt;h3&gt;vendor/plugins&lt;/h3&gt;

&lt;p&gt;Basic plugins can still live in the vendor/plugins directory just like
in previous Rails versions. The file init.rb is, also, still run when the
plugin is loaded. Many plugins can still use this approach. For
example if you simply want to extend ActiveRecord with something like
acts_as_* vendor/plugins will probably do just fine.&lt;/p&gt;

&lt;p&gt;Under the hood plugins are handled very differently. Every plugin in
the vendor/plugins directory is made into an instance of the new
Rails::Plugin class. More on this a bit later. I will mention now
though, &lt;a href="https://gist.github.com/af7e572c2dc973add221#file_3_plugin.rdoc"&gt;you should never have a need to subclass Rail::Plugin
yourself&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;The Railtie&lt;/h3&gt;

&lt;p&gt;The Railtie is where the magic really starts to happen. In Rails 3,
just like in previous versions, plugins can be distributed as
gems. When a plugin is distributed as a gem many bonus features become
available like an easy way to include rake tasks, generators and
sub-applications.&lt;/p&gt;

&lt;p&gt;In order to distribute your plugin as a gem that works with Rails you
must make it a Railtie or an Engine. To do this, include a
subclass of Rails::Railtie or its subclass Rails::Engine, respectively.
Let&amp;rsquo;s add Rails::Engine to the list of things I will talk about later.&lt;/p&gt;

&lt;p&gt;Making your plugin a Railtie allows it to hook into all parts of the
&lt;a href="http://ryanbigg.com/guides/initialization.html"&gt;initialization
process&lt;/a&gt;, setup its
own intializers and interact with the application object. The Railtie
also allows you to specify generators for the orm, testing and
templates as well as loading your own generators and rake tasks. The
Rails 3 boot processes is entirely new as well and there are many
points where your plugin can integrate and modify the application.&lt;/p&gt;

&lt;p&gt;Once you install your gem and add it to the your app&amp;rsquo;s Gemfile rake
tasks and generators will become available.&lt;/p&gt;

&lt;h3&gt;Rails::Engine (Apps inside an App)&lt;/h3&gt;

&lt;p&gt;Rails engines have been around for a while now but in Rails 3 they are
given their official place. A Rails engine allows
&amp;ldquo;sub-applications&amp;rdquo; to run inside another application. There are many examples
of this but authorization is one example commonly tossed
around. Any functionality you wish to share accross
multiple Rails applications belongs in an engine.&lt;/p&gt;

&lt;p&gt;Engines, like Railties have a special class that must be subclassed
and included in your plugin. Rails::Engine is actually a subclass of
Rails::Railtie but with a much richer feature set.&lt;/p&gt;

&lt;p&gt;Engines have both their &lt;a href="https://gist.github.com/af7e572c2dc973add221#file_2_engine.rdoc"&gt;own configuration and share configuration with
the application that includes
it&lt;/a&gt;.
For example, your engine can have completely separate view paths. You
are not forced to use an app/ directory next to lib/. If you wish you
can include controllers, views, models, and metal anywhere inside your
engine&amp;rsquo;s root directory. Middleware configuration is shared between
the application and engine, however.&lt;/p&gt;

&lt;p&gt;Rails engines are crazy powerful and probably a favorite feature for
any Rails developer. People will do really cool things with them and
even cooler things stacking them up next to each other.&lt;/p&gt;

&lt;h3&gt;A little more on Rails::Plugin and a note on Rails::Application&lt;/h3&gt;

&lt;p&gt;I said I would talk a bit more about Rails::Plugin. As I have already
mentioned, an instance of Rails::Plugin is instantiated for every
plugin in vendor/plugins. Rails::Plugin should not be subclassed like
Rails::Railtie or Rails::Engine. This is because it is loaded much
later in the load process and has considerably less access to the
application. The Rails::Plugin&lt;/p&gt;

&lt;p&gt;On to Rails::Application. Every application you create in Rails is an
instance of Rails::Application. Only one instance of a
Rails::Application can run in a single thread at any given
time. However, Rails::Application is simply a subclass of both
Rails::Railtie and its subclass Rails::Engine. Other topics related to
Rails::Application are out of the scope of this post. Read the &lt;a href="http://guides.rails.info/"&gt;edge
guides&lt;/a&gt; for more info.&lt;/p&gt;

&lt;h3&gt;Some Resources&lt;/h3&gt;

&lt;p&gt;Listed below are some resources I used while working on several Rails
3 plugins this week.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://boldr.net/upgrade-plugins-gems-rails-3"&gt;Upgrade your Plugins&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://ryanbigg.com/guides/initialization.html"&gt;Rails 3 Initializatin Process in Serious Detail&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/af7e572c2dc973add221"&gt;Gist by Jose Valim on Rails 3 Plugins&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://weblog.rubyonrails.org/2010/2/9/plugin-authors-toward-a-better-future"&gt;Rails Weblog on Plugins&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://caffeinedd.com/guides/331-making-generators-for-rails-3-with-thor"&gt;Rails 3 Generators with Thor&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://guides.rails.info/"&gt;Edge Guides&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://guides.rails.info/routing.html"&gt;Edge Guides: Routing&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://guides.rails.info/generators.html"&gt;Edge Guides: Generators&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://darwinweb.net/articles/82"&gt;Getting up to speed with Rails 3&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://strd6.com/?p=250"&gt;using unloadable in your plugins&lt;/a&gt;&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Rails 3 Plugins - Part 2 - Writing an Engine</title>
    <link href="http://www.themodestrubyist.com/2010/03/05/rails-3-plugins---part-2---writing-an-engine/" rel="alternate"/>
    <id>http://www.themodestrubyist.com/2010/03/05/rails-3-plugins---part-2---writing-an-engine/</id>
    <published>2010-03-05T00:00:00-08:00</published>
    <updated>2010-03-05T00:00:00-08:00</updated>
    <author>
      <name>Jordan Ryan West</name>
    </author>
    <summary type="html">&lt;p&gt;Last time, I gave a big-picture overview of the Rails 3 plugin
API. This time, we will dive in and write a Rails engine with a
&lt;a href="http://github.com/jrwest/authr3"&gt;simple authorization example I
wrote&lt;/a&gt;&amp;hellip;&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;Last time, I gave a big-picture overview of the Rails 3 plugin
API. This time, we will dive in and write a Rails engine with a
&lt;a href="http://github.com/jrwest/authr3"&gt;simple authorization example I
wrote&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You may be wondering why I am not talking about writing a
Rails::Railtie first. There is a great article you can read
&lt;a href="http://boldr.net/upgrade-plugins-gems-rails-3"&gt;here&lt;/a&gt; that explains
upgrading existing plugins and the Railtie in general.&lt;/p&gt;

&lt;p&gt;I will also not be addressing testing your engine because I am a die
hard rspec/cucumber fan and support in Rails 3 is in between alpha and
beta right now. I
feel nasty not writing them and have been using
ActiveSupport::TestCase in some work but not in this example. Once
support is better I will follow up and write about testing your engine
with rspec and cucumber.&lt;/p&gt;

&lt;h3&gt;So, Let&amp;rsquo;s Get Started!&lt;/h3&gt;

&lt;p&gt;You will need to setup a couple things before diving in: a directory
where your gem&amp;rsquo;s code-base will live and a playground to play with
your engine in. You will also need to be able to install the plugin as
a gem.&lt;/p&gt;

&lt;p&gt;The name of our example is &amp;ldquo;authr3&amp;rdquo; so lets create a
directory with the same name to keep our engine&amp;rsquo;s code in (you can
really name it as you please, it won&amp;rsquo;t matter). The engine directory
will also need &amp;lsquo;lib&amp;rsquo;, &amp;lsquo;app&amp;rsquo;, and &amp;lsquo;config&amp;rsquo; sub-directories. We will also
name our library &amp;lsquo;authr&amp;rsquo; not &amp;lsquo;authr3&amp;rsquo; just to show how this is handled
in a Gemfile. At the same time lets create a rails app to plug our engine into.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; mkdir -p authr3/lib/authr
&amp;gt;&amp;gt; mkdir authr3/app
&amp;gt;&amp;gt; mkdir authr3/config
&amp;gt;&amp;gt; touch authr3/lib/authr.rb 
&amp;gt;&amp;gt; rails /path/to/your/app
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Once we have our directory structure lets go ahead and go thru the
necessary steps to setup our gem although we won&amp;rsquo;t install it for a
bit. I will use &lt;a href="http://github.com/technicalpickles/jeweler"&gt;Jeweler&lt;/a&gt;
which makes it simple to build, install and publish gems using
&lt;a href="http://rake.rubyforge.org/"&gt;Rake&lt;/a&gt;. If you haven&amp;rsquo;t used Jeweler before
you can install it with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gem install jeweler
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With Jeweler you do everything in your Rakefile, which if your like me
you were probably doing anyways. Add the following to your Rakefile:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;begin
  require "jeweler"
  Jeweler::Tasks.new do |gem|
    gem.name = "authr3"
    gem.summary = "Auth Engine for Rails 3"
    gem.files = Dir["{lib}/**/*", "{app}/**/*", "{config}/**/*"]
    # other fields that would normally go in your gemspec
    # like authors, email and has_rdoc can also be included here

  end
rescue
  puts "Jeweler or one of its dependencies is not installed."
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Save your Rakefile and shuffle it a side for a bit. Your sanbox is
setup. If you want to test things simply run&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; rake install
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;from the engine&amp;rsquo;s root directory to install it as a gem. Run&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; gem uninstall authr3
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;to uninstall the gem. Thanks to khelben&amp;rsquo;s comment below you will
probably not have to do this again while playing with your engine
(Note: Originally, I said you will have to perform this process
between changes to your engine&amp;rsquo;s codebase).&lt;/p&gt;

&lt;p&gt;Lastly, you will need to add the gem to your playground&amp;rsquo;s Gemfile. It
is possible with bundler to also define a Gemfile within your
engine. Since I am no authority on bundler as it is pretty new to me
as well I will not be discussing it here. authr3 only has one
dependency, the Ruby BCrypt Library, and in this example we will just put it in the app&amp;rsquo;s Gemfile
above our gem. So add the following two lines to your playground&amp;rsquo;s
Gemfile (this article will not use bcrypt-ruby in any example
so unless you are following along with my codebase, you can skip it):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gem "bcrypt-ruby", :require =&amp;gt; "bcrypt"
gem "authr3", :require =&amp;gt; "authr", :path =&amp;gt; "/path/to/authr3"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The :require option specifies the name used to &amp;lsquo;require&amp;rsquo; the
library. If you look above we created the file authr.rb in our lib
directory. The first argument passed is the name of the gem itself
set with &amp;lsquo;gem.name&amp;rsquo; in the gemspec. Finally, again thanks to khelben&amp;rsquo;s
comment below, the path option specifies where the codebase lives
locally and will load it from there instead. The :path option makes it
so the engine does not have to be reinstalled between changes for them
to be reflected.&lt;/p&gt;

&lt;p&gt;Now that we have all the basics set up, let&amp;rsquo;s get our hands dirty with
our engine.&lt;/p&gt;

&lt;h3&gt;The Engine Class&lt;/h3&gt;

&lt;p&gt;The first real piece of code we will write is our engine class. Our
class must be a subclass of Rails::Engine, a railtie with extended
abilities like shared and self-contained configuration. Below is the
most basic class definition we can write for our authentication engine.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#lib/authr/engine.rb
module Authr
 class Engine &amp;lt; Rails::Engine
    engine_name :authr
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I am not sure if the engine name is required but it is good practice
to put it in there. I have yet to see an example in an article or in a
library that exlucded it.&lt;/p&gt;

&lt;p&gt;We need to add a few more lines to our engine.rb file before it is
operational however. Rails plugins must follow a simple rule: REQUIRE
WHAT YOU OVERRIDE. &lt;a href="http://weblog.rubyonrails.org/2010/2/9/plugin-authors-toward-a-better-future"&gt;If you extend ActionController or ActiveRecord,
require
it&lt;/a&gt;. You
should always require Rails and your own gem. My example repository
also requires &amp;lsquo;action_controller&amp;rsquo; because the full codebase extends
it. Since, I won&amp;rsquo;t be talking about that piece here I have left it out. Our engine.rb file
should look like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#lib/authr/engine.rb
require "authr"
require "rails"

module Authr
  class Engine &amp;lt; Rails::Engine
    engine_name :authr
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is the full definition of our engine. It may seem kind of
worthless but there are many things we can do
inside our engine class. So many in fact, I will address them in
another post.&lt;/p&gt;

&lt;p&gt;To get things completely wired up we must require the engine
class in our library. You can simply add a require to
lib/authr.rb. Many examples, instead, do something like this,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; require 'authr/engine' if defined?(Rails)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; module Authr
   require 'authr/engine' if defined?(Rails)
 end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can, also, explicity check that a version of Rails 3 is being used
with,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; require 'authr/engine' if defined?(Rails) &amp;amp;&amp;amp; Rails::VERSION::MAJOR == 3
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Reaping our First Reward&lt;/h3&gt;

&lt;p&gt;So we have done a whole lot without much to show for it. Let&amp;rsquo;s do
something that is tangible from within our playground
application. Lets add an Account model to our app and make sure it is defined
when running the host application.&lt;/p&gt;

&lt;p&gt;In your engine add the following model&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; #lib/app/models/account.rb
 class Account &amp;lt; ActiveRecord::Base
 end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Re-install the engine gem and boot up the console in your playground
app. If the gem is installed properly and you have added authr to your
Gemfile then you should be able to bootup &amp;lsquo;rails console&amp;rsquo; and see&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; &amp;gt; Account
 =&amp;gt; Account(Table doesn't exist)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you see&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; &amp;gt; Account
 =&amp;gt; NameError: uninitialized constant Account
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;then you have probably not added the gem to your Gemfile properly. The
&amp;lsquo;Table doesn&amp;rsquo;t exists&amp;rsquo; response is of course because we have not created
an accounts table. You can use &lt;a href="http://github.com/jrwest/authr3/blob/master/lib/generators/authr/templates/migration.rb"&gt;the migration from the authr
repository&lt;/a&gt;
to create the table. If you use the code from my authr repositiory, it
can generate the migration for your host app. To stay focused on engine
basics, I will leave generators for later in this series.&lt;/p&gt;

&lt;h3&gt;Making our engine look like a real app&lt;/h3&gt;

&lt;p&gt;So now we have our app loading classes from our engine. This is
because the paths app/ and lib/ along with several others under your
engine&amp;rsquo;s root directory are automatically added to your application&amp;rsquo;s
load path. Jose Valim&amp;rsquo;s
&lt;a href="https://gist.github.com/af7e572c2dc973add221#file_2_engine.rdoc"&gt;gist&lt;/a&gt;
on engines lists each path your engine can modify. These paths are
relative to your engine&amp;rsquo;s root not your application. Paths are not
configuration shared between app and engine. I personally leave these
alone. I intially tried doing all my work under the lib/ dir. It
seems, at least for now if you want to redfine some of the paths your
must redefine them all. For example you cannot only use,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; paths.app = "lib/app"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;to set the app, controllers, views and models paths to all live under
the lib/app directory. You must explicitly specify the path for
each.&lt;/p&gt;

&lt;p&gt;We know our controllers are automatically loaded from app/controllers
so lets add one. Let&amp;rsquo;s add an Authr::AccountsController which has a new
and create action. The new action will later have a corresponding view
which holds the new Account form.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; #app/controllers/authr/accounts_controller.rb
 module Authr
   class AccountsController &amp;lt; ApplicationController

    unloadable

    def create
      @account = Account.new(params[:account])
      if @account.save
         redirect_to '/'
      else
         render :action =&amp;gt; :new
      end
    end

    def new
     @account = Account.new
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There are a few things of note here. Like our engine, I have
namespaced the controller as well. This is not necessary outside of
your library, and infact I did not namespace the model. Its a matter
of preference and as I look back I think I may namespace the model as
well later on. This way your plugin has no chance of colliding with
classes in the host application.&lt;/p&gt;

&lt;p&gt;The second thing of note is the call to &amp;lsquo;unloadable&amp;rsquo; in the
AccountsController. This must be included in your engine
controllers. Unloadable marks your class for reloading inbetween
requests. You can read more about unloadable
&lt;a href="http://strd6.com/2009/04/cant-dup-nilclass-maybe-try-unloadable/"&gt;here&lt;/a&gt;
and &lt;a href="http://apidock.com/rails/ActiveSupport/Dependencies/Loadable/unloadable"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So we have our AccountsController, we just need a view for #new. Since this is a simple task I will let you write your own, or
just copy
&lt;a href="http://github.com/jrwest/authr3/blob/master/app/views/authr/accounts/new.html.erb"&gt;mine&lt;/a&gt;. Views
use the same naming conventions inside engines as they do
applications. This view should live in
authr3/app/views/authr/accounts/.&lt;/p&gt;

&lt;p&gt;I would like to mention, quickly, that I believe it is important to write engine
views with only the markup directly relavant to the action. Remember,
this view will be included in a layout of a host application and you do
not want your markup to be a hassle.&lt;/p&gt;

&lt;h3&gt;Routing it all together&lt;/h3&gt;

&lt;p&gt;So to review, we now have an Account model, an AccountsController and
a view, new.html.erb, so its time we see something of substance in the
browser. To do so we need one final piece, routes.&lt;/p&gt;

&lt;p&gt;Its easy to add routes to a host application from an engine. Just like
the app/ directory is automatically added to the load path so is the
config/ directory in our engine root. We will add our controller using
RESTful routes but only for the new and create actions.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; #config/routes.rb
 Rails.application.routes.draw do |map|
   resources :accounts, :controller =&amp;gt; 'authr/accounts', :only =&amp;gt; [:new, :create]
 end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Reinstall your gem one more time and then you should see the routes
appear in your host application.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; &amp;gt;&amp;gt; rake routes
 accounts    POST   /accounts(.:format)     {:action=&amp;gt;"create", :controller=&amp;gt;"authr/accounts"}
 new_account GET    /accounts/new(.:format) {:action=&amp;gt;"new", :controller=&amp;gt;"authr/accounts"}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Boot up &amp;lsquo;rails server&amp;rsquo; from your playground and point your browser at
localhost:3000/accounts/new. You should see whatever you put in
new.html.erb.&lt;/p&gt;

&lt;p&gt;So there you have it. The start of a simple Rails 3 engine. Of course
the code presented here leaves much to be desired. I will leave it up
to you to make it functional or you can look at &lt;a href="http://github.com/jrwest/authr3"&gt;my
code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In the next installment of this series I will continue to discuss
engines and there more advanced features like including rake tasks and
writing generators and initializers.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Rails 3 Plugins - Part 3 - Rake Tasks, Generators, Initializers, Oh-My!</title>
    <link href="http://www.themodestrubyist.com/2010/03/16/rails-3-plugins---part-3---rake-tasks-generators-initializers-oh-my/" rel="alternate"/>
    <id>http://www.themodestrubyist.com/2010/03/16/rails-3-plugins---part-3---rake-tasks-generators-initializers-oh-my/</id>
    <published>2010-03-16T00:00:00-07:00</published>
    <updated>2010-03-16T00:00:00-07:00</updated>
    <author>
      <name>Jordan Ryan West</name>
    </author>
    <summary type="html">&lt;p&gt;With the basics of Rails::Engine in our toolbox, in part three, I move
on to some of the more advanced features of Railties and Engines&amp;hellip;&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;With the basics of Rails::Engine in our toolbox, in part three, I move
on to some of the more advanced features of Railties and Engines.&lt;/p&gt;

&lt;p&gt;In &lt;a href="http://www.themodestrubyist.com/2010/03/05/rails-3-plugins---part-2---writing-an-engine/"&gt;part two&lt;/a&gt;,
our Rails::Engine subclass, Authr::Engine, was simply defined like
below.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# lib/authr/engine.rb
module Authr
   class Engine &amp;lt; Rails::Engine
       engine_name :authr
   end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This may seem sort of useless. As we saw it wasn&amp;rsquo;t. Simply
defining this subclass provides us with robust code-sharing in
Rails. Similarly, when subclassing Rails::Railtie, we are given early
access to the initialization process of the containg
application. This, as I discuss in detail in &lt;a href="http://www.themodestrubyist.com/2010/03/01/rails-3-plugins---part-1---the-big-picture/"&gt;part
one&lt;/a&gt;,
is what differs the Rails::Railtie from a plugin in vendor/plugins.&lt;/p&gt;

&lt;p&gt;There is, however, much more we can do within the class defintion of
our Railtie or Engine. Remember Rails::Engine is a subclass of
Rails::Railtie, thus inheriting all of its functionality. I will note
when something is only available when defining a subclass of
Rails::Engine.&lt;/p&gt;

&lt;h3&gt;Raking in the benifits&lt;/h3&gt;

&lt;p&gt;So you have some rake tasks that would be useful for a developer using
your shiny, new plugin? No problem. With a Rails::Railtie this
is easy. Toss all your rake tasks in a file inside your gem,
add a few lines to your Railtie definition and your good to go.&lt;/p&gt;

&lt;p&gt;I suggest using the convention used by Rails core and place your .rake
files in YOUR_RAILTIE_DIR/lib/railties/. You can then have your tasks
autoloaded by adding this to your Railite,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rake_tasks do
  load "your_railtie/railties/tasks.rake"
end 
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If your following from &lt;a href="http://www.themodestrubyist.com/2010/03/05/rails-3-plugins---part-2---writing-an-engine/"&gt;part
two&lt;/a&gt;,
you could add rake tasks to authr3 like this,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# lib/authr/engine.rb
module Authr
  class Engine &amp;lt; Rails::Engine
      engine_name :authr

      rake_tasks do
        load "authr/railties/tasks.rake"
      end

   end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If everything is properly hooked up you should see your tasks defined
in tasks.rake listed when you run&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; &amp;gt;&amp;gt;rake -T
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Generating on Top of Shoulders of a Giant&lt;/h3&gt;

&lt;p&gt;Thor is a good name for a giant, right? As well as a complete overhaul of plugins, Rails 3 also has an
entirely new API for writing generators built on top of &lt;a href="http://github.com/wycats/thor"&gt;Thor&lt;/a&gt;. If you are familiar
with Thor, which I really wasn&amp;rsquo;t minus scanning a few tutorials and
trying it once, &lt;a href="http://caffeinedd.com/guides/331-making-generators-for-rails-3-with-thor"&gt;then anything you would normally do with it
you can do inside Rails 3 generators&lt;/a&gt;. Rails
3 extends Thor&amp;rsquo;s actions in
&lt;a href="http://github.com/rails/rails/blob/master/railties/lib/rails/generators/actions.rb"&gt;Rails::Generators::Actions&lt;/a&gt;
and provides other needed generator functionality through
&lt;a href="http://github.com/rails/rails/blob/master/railties/lib/rails/generators/base.rb"&gt;Rails::Generators::Base&lt;/a&gt;
and several other classes and modules.&lt;/p&gt;

&lt;p&gt;In &lt;a href="http://www.themodestrubyist.com/2010/03/05/rails-3-plugins---part-2---writing-an-engine/"&gt;part
2&lt;/a&gt;,
we created an Account model to represent a user passing through our
authentication engine. This is a great example of when we could use a
generator to provide developers with a migration for creating the
accounts table. Simply dropping a template in db/migrate isn&amp;rsquo;t
enough. Just like in previous versions of Rails, the generator API
provides methods for handling special actions like creating
migrations with timestamps exactly like those generated by
ActiveRecord.&lt;/p&gt;

&lt;p&gt;First lets do a little busy work to set up our generator. We need a
folder to place all our generators in. Rails 3 looks in each plugin&amp;rsquo;s lib/generators
directory for generators. To create a generator first create a
subdirectory in lib/generators. For authr3, I used the same name as
the plugin and created the folder lib/generators/authr.&lt;/p&gt;

&lt;p&gt;Next we define our generator by subclassing
Rails::Generators::Base. In Rails 2, I believe the equivalent class is
Rails::Generator::Base (the singular use of Generator is the
difference). Following convention, our subclass should be named
something like MyGenerator and defined in my_generator.rb in a
subdirectory of lib/generators. The basic definition of a generator
for authr3 looks like this,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; # lib/generators/authr/authr_generator.rb
 require 'rails/generators'

 class AuthrGenerator &amp;lt; Rails::Generators::Base
   def self.source_root
      @source_root ||= File.join(File.dirname(__FILE__), 'templates')
   end
 end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;First, we require what we need. Next we define our generator,
AuthrGenerator, as a subclass of Rails::Generators::Base. Finally, we
add a class method to our generator, &amp;lsquo;source_root&amp;rsquo;. This class method must be
defined in your generator. Unlike Rails 2 generators, a &amp;lsquo;templates&amp;rsquo;
subdirectory is not automatically added to the load path. Instead &amp;lsquo;source_root&amp;rsquo; must return a
path to where your templates are stored. This may seem tedious at
first but it gives you flexibility and options if you are writing more
than a simple generator. You could for example use this to look up
templates in a user&amp;rsquo;s home directory or elsewhere on the system.&lt;/p&gt;

&lt;p&gt;The above is something you will need to do for new generators included
with your Railties and Engines. The built-in Rails generators (rails g[enerate])
does generate generator skeletons for you. However, it generates a
subclass of Rails::Generators::NamedBase. Rails::Generators::NamedBase
differs from Rails::Generators::Base in that it expects one argument
to be passed to the generator. I will not be discussing Rails::Generators::NamedBase in
further detail in this post. You can, of course use rails generate to
create the files you need and simply change your generator to be a
subclass of Rails::Generators::Base.&lt;/p&gt;

&lt;p&gt;So now that we have the necessary pieces in place, double-check that
the generator is listed from within the containing Rails application.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; &amp;gt;&amp;gt;rails g
 . . .
 Authr:
   authr
 . . .
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ok, good to go. Let&amp;rsquo;s generate a migration. My migration template for
authr3 looks something like below. Place the file in
lib/generators/authr/templates. If you changed this in
AuthrGenerator.source_root then place the file there.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; # lib/generators/authr/templates/migration.rb
 class CreateAccountsTable &amp;lt; ActiveRecord::Migration
   def self.up
     create_table :accounts do |t|
       t.string :uname
       t.string :hashed_password
       t.string :remember_token
       t.datetime :remember_expiry
       #Any additional fields here

       t.timestamps
    end
  end

  def self.down
    drop_table :accounts
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The details of the migration are not really relavent. I lifted it from
&lt;a href="http://github.com/jrwest/authr3"&gt;my authr3 repository&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now we must go back to our AuthrGenerator and add the necessary pieces
so it can be generated and placed in db/migrate with timestamps and a
new file name. Each public instance method defined on the generator
class is invoked when the generator is executed. We will define a
method &amp;lsquo;create_migration_file&amp;rsquo; on AuthrGenerator. You can name this
method &amp;lsquo;foo_bar_de_do_da&amp;rsquo; if you wish, however.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; # lib/generators/authr/authr_generator.rb
 require 'rails/generators'
 require 'rails/generators/migration'     

 class AuthrGenerator &amp;lt; Rails::Generators::Base
   include Rails::Generators::Migration
   ...
   # Implement the required interface for Rails::Generators::Migration.
   # taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
   def self.next_migration_number(dirname)
     if ActiveRecord::Base.timestamped_migrations
       Time.now.utc.strftime("%Y%m%d%H%M%S")
     else
       "%.3d" % (current_migration_number(dirname) + 1)
     end
   end

   def create_migration_file
     migration_template 'migration.rb', 'db/migrate/create_accounts_table.rb'
   end
 end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The first thing we add is the inclusion of
Rails::Generators::Migration defined in
rails/generators/migration. This is necessary in order to use the
&amp;lsquo;migration_template&amp;rsquo; action. The &amp;lsquo;migration_template&amp;rsquo; action works
pretty similar to its Rails 2 counterpart: expecting two strings a
source template and a destination file. It will automatically prepend the proper
timestamp to the filename when the generator is invoked. Unlike Rails
2, however, simply calling &amp;lsquo;migration_template&amp;rsquo; is not enough. We must
also define AuthrGenerator.next_migration_number in order for it to
work. &lt;a href="http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/a507ce419076cda2"&gt;Jose Valim has mentioned the
possibility&lt;/a&gt;
ActiveRecord::Generators::Base.next_migration_number will be exposed
as a class method, which you can then use explicitly in your
generators. As of now, I do not believe this is possible, and you are
stuck defining it on your own.&lt;/p&gt;

&lt;p&gt;There is a way to get around the nusance of defining
&amp;lsquo;next_migration_number&amp;rsquo; and shorten up our generator&amp;rsquo;s code. This
option is only available if your migration template is static. Instead
of having a template at all we can simply invoke the built-in
migration generator. Here is an example Jose Valim presented in a
&lt;a href="http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/a507ce419076cda2"&gt;discussion on Google
Groups&lt;/a&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  class ActsAsTaggableOnMigrationGenerator &amp;lt; Rails::Generators::Base
    invoke "migration", %(add_fields_to_tags name:string label:string)
  end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Notice invoke is called on the generator class itself  not from inside
one of its instance methods. The first argument passed is the name of
the generator. The second arguments is an array of arguments exactly
like the ones you would pass to the command-line rails generate
script.&lt;/p&gt;

&lt;p&gt;Generators, like other APIs in Rails, are extremely flexible and easy
to use. There are many other things you can do with your generators
like define command-line arguments a generator can take. Since this
post is getting long enough, I will leave some of the Generators API
more advanced features for later in this series.&lt;/p&gt;

&lt;h3&gt;Intialization in your Plugin and Your App&lt;/h3&gt;

&lt;p&gt;The main difference between Railties and a classic plugin living in
vendor/plugins is when they are loaded in the app boot up process and
their access to an application&amp;rsquo;s initialization.&lt;/p&gt;

&lt;p&gt;First, say you have some things that need to be setup inside your
plugin when the server boots up the application. This is what simple
initializers are for. Here is how we can define an initializer in our
Raltie.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; class MyRailtie &amp;lt; Rails::Railtie
   ...
   initializer "my_plugin.some_init_task" do |app|
     # app variable represents application object
     # for containing app
   end
 end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The Rails::Railtie.initializer class method is what lets us define our
initializer. It takes a string, the name of the initializer and a
block. It also excepts some options but more on that in a second. The
block is passed one parameter, a reference to the containing
application object, Rails.application. Any action you would perform on
Rails.application you should be able to perform on the parameter
passed to the block.&lt;/p&gt;

&lt;p&gt;As I mentioned we can also pass &amp;lsquo;initializer&amp;rsquo; some options. The
:before and :after options allows us to hook into the application&amp;rsquo;s init process and
specify when our initializer will be called.  Ryan Bigg does a great
job of &lt;a href="http://ryanbigg.com/guides/initialization.html"&gt;detailing all of the available initialization
points&lt;/a&gt; our plugin can
hook into. Here is a quick example of setting our initializer to run
after load paths have been set in the application.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; initializer "my_plugin.another_task", :after =&amp;gt; :set_load_path do |app|
    #init stuff here
 end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When doing this, it is important to require what you need and include
whatever file in the rails codebase that defines the initializer you
are using. In the case of set_load_path we would require
&amp;lsquo;rails/engine&amp;rsquo;. Look to &lt;a href="http://ryanbigg.com/guides/initialization.html"&gt;Ryan Bigg&amp;rsquo;s
guides&lt;/a&gt; for what
initializer is defined where.&lt;/p&gt;

&lt;p&gt;That&amp;rsquo;s it for part three. Hope this series continues to be helpful for
people. I am pretty sure I will be writing one final part, addressing
some of more advanced points and nuances.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Rails 3 Plugins - Part 4 - More on Generators</title>
    <link href="http://www.themodestrubyist.com/2010/03/22/rails-3-plugins---part-4---more-on-generators/" rel="alternate"/>
    <id>http://www.themodestrubyist.com/2010/03/22/rails-3-plugins---part-4---more-on-generators/</id>
    <published>2010-03-22T00:00:00-07:00</published>
    <updated>2010-03-22T00:00:00-07:00</updated>
    <author>
      <name>Jordan Ryan West</name>
    </author>
    <summary type="html">&lt;p&gt;So, you have written a basic generator for your Rails 3 plugin but it
needs to do more. In this part I discuss the generators API in more detail&amp;hellip;&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;So, you have written a basic generator for your Rails 3 plugin but it
needs to do more. In this part I discuss the generators API in more detail.&lt;/p&gt;

&lt;h3&gt;Adding a Descritiption and Usage Info&lt;/h3&gt;

&lt;p&gt;People using your plugin will want to know what your generator
does. You can add a simple description to your generator like so,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class MyGenerator &amp;lt; Rails::Generators::Base
  desc "run this generator to create something"
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &amp;lsquo;desc&amp;rsquo; macro takes a string, which should describe the generators
purpose.&lt;/p&gt;

&lt;p&gt;In the same directory as the file which contains your generator class
you can place a file named &amp;lsquo;USAGE&amp;rsquo;. This file should contain a more
detailed description and optionally an example of how to use your
generator. Here is the USAGE file from the &lt;a href="http://github.com/rails/rails/tree/master/railties/lib/generators/rails/app"&gt;rails core application
generator&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Description:
  The 'rails' command creates a new Rails application with a default
  directory structure and configuration at the path you specify.  

Example:
  rails ~/Code/Ruby/weblog

  This generates a skeletal Rails installation in ~/Code/Ruby/weblog.
  See the README in the newly created application to get going.
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Taking Command-Line Options and Arguments&lt;/h3&gt;

&lt;p&gt;If you need to provide control to the user upon invocation
you can define some options for it. These options are exposed all the
way to the command-line. Defining these options is not
actually something specific to the Rails 3 Generators API. Instead,
its inherited from Thor, which the API is based upon.&lt;/p&gt;

&lt;p&gt;To define a single option we can use the &amp;lsquo;class_option&amp;rsquo; macro. To
define multiple we use &amp;lsquo;class_options&amp;rsquo;. Here is how we would define a
boolean option for some generator&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class_option :my_option, :type =&amp;gt; :boolean, :desc =&amp;gt; "some desc"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&amp;lsquo;class_option&amp;rsquo; takes a name and several options. The following are a
list of options you can pass to class_option:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:desc =&amp;gt; string, description of option
:required =&amp;gt; true/false, whether or not the argument is required
:default =&amp;gt; default value if argument is not given
:group =&amp;gt; group this option belongs to. 
          Used for orginization of options at different levels
:type =&amp;gt; :string/:hash/:array/:numeric/:boolean
:banner =&amp;gt; string, shown in usage notes
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Options are accessed from within a generator&amp;rsquo;s steps (or instance
methods defined in the Generator class) using the &amp;lsquo;options&amp;rsquo; hash. We
can access our previously defined option like so&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; options[:my_option] 
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Since it is a boolean option we would most likely use it like&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; def some_step
   do_something unless options[:my_option]
 end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There also exists the &amp;lsquo;method_option(s)&amp;rsquo; and the &amp;lsquo;argument&amp;rsquo;
class macros. The former is more useful in Thor specific contexts and I will
not discuss it here. The latter is a way to take command-line
arguments instead of options. The difference between arguments and
options is subtle with respect to nomenclature but great in
practice. Whereas options are provided regardless of order
using the&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  --option=VALUE
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;syntax, arguments are taken off the command-line in order. Inside your
code, options are accessed using self.options. Arguments are accessed
using accessors, like self.some_argument, and cannot be of type
:boolean. Also, any optional arguments, created with :required =&gt; false
or :optional =&gt; true, must be specified after all the required
arguments. Restated, all required arguments must precede any optional
ones. This is of course due to the order sensitive nature of
arguments. Here is how we could define a couple arguments for our generator&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  argument :my_arg, :type =&amp;gt; :numeric, :required =&amp;gt; true, :desc =&amp;gt; "required"
  argument :some_arg, :type =&amp;gt; :string, :required =&amp;gt; false, :desc =&amp;gt; "optional"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We could then access these arguments in our generator class like in
this rudimentiery and mostly useless example,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; def some_task
    if self.my_arg &amp;lt; some_value
      do_something_with self.some_arg
    end
 end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note, unlike &amp;lsquo;class_option&amp;rsquo; and &amp;lsquo;class_options&amp;rsquo; , an &amp;lsquo;arguments&amp;rsquo;
macro does not exist for creating multiple arguments at once. Instead
&amp;lsquo;arguments&amp;rsquo; returns the arguments defined for a class, or if none
exist, its superclass. You can read more on these in the &lt;a href="http://textmate.rubyforge.org/thor/Thor.html"&gt;Thor
docs&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;Rails Specific Actions&lt;/h3&gt;

&lt;p&gt;Thor provides several useful methods or actions that can be used
within steps to perform common tasks. These are defined in
&lt;a href="http://textmate.rubyforge.org/thor/Thor/Actions.html"&gt;Thor::Actions&lt;/a&gt;. Rails
also defines some more actions, primarily, in
&lt;a href="http://github.com/rails/rails/blob/master/railties/lib/rails/generators/actions.rb"&gt;Rails::Generators::Actions&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Here are some useful ones and how to use them:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# lib(filename, data=nil, &amp;amp;block)
lib("somefile.rb") do 
  "this string will be the content of the newly created lib/somefile.rb"
end  

lib("anotherfile.rb", "you can also pass a string to lib")

# rake(command, options={})
rake("db:migrate") #run any defined Rake command
rake("db:migrate", :env =&amp;gt; :development)

# route(routing_code)
route("resources :accounts") # adds route to TOP of routes definition
                             # you must write valid routing code

# git(command = {})
git(:init) #run any git command
git(:add =&amp;gt; "somefile.ext", :rm =&amp;gt; "somedir/anotherfile.ext")
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;All of these are automatically available in any of your generators.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Redis + CompSci 101 (Redis::Stack)</title>
    <link href="http://www.themodestrubyist.com/2010/03/27/redis--compsci-101-redisstack/" rel="alternate"/>
    <id>http://www.themodestrubyist.com/2010/03/27/redis--compsci-101-redisstack/</id>
    <published>2010-03-27T00:00:00-07:00</published>
    <updated>2010-03-27T00:00:00-07:00</updated>
    <author>
      <name>Jordan Ryan West</name>
    </author>
    <summary type="html">&lt;p&gt;Recently I got introduced to &lt;a href="http://code.google.com/p/redis/"&gt;Redis&lt;/a&gt;,
a super-cool, im-memory (but less-volitale), key-value store. As usual, I was
late to the party.&lt;/p&gt;

&lt;p&gt;Redis, is several things, including a sort of data structures server. So
to flash back to CompSci 101, in this article I build a stack on top
of the Redis Ruby client, which is one of the first things I did with
it&amp;hellip;&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;Recently I got introduced to &lt;a href="http://code.google.com/p/redis/"&gt;Redis&lt;/a&gt;,
a super-cool, im-memory (but less-volitale), key-value store. As usual, I was
late to the party.&lt;/p&gt;

&lt;p&gt;Redis, is several things, including a sort of data structures server. So
to flash back to CompSci 101, in this article I build a stack on top
of the Redis Ruby client, which is one of the first things I did with
it.&lt;/p&gt;

&lt;h3&gt;Redis Resources&lt;/h3&gt;

&lt;p&gt;Before we get started here are some Redis resources I used to get up
and running:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://redis-db.com/"&gt;Redis Homepage&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://code.google.com/p/redis/wiki/index?tm=6"&gt;Redis Wiki&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.engineyard.com/blog/2009/key-value-stores-for-ruby-part-4-to-redis-or-not-to-redis/"&gt;Engine Yard Article&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://github.com/ezmobius/redis-rb"&gt;Redis Ruby client&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Redis::Stack&lt;/h3&gt;

&lt;p&gt;Our Redis::Stack class will implement 4 operations: push, pop,
peek, to_a, and size. Redis provides us with a list data structure, implemented as
a Linked List, which we will use to store our stack. The operations that
act on lists we will concern ourself with are below. The commands used (passed to the Redis client
object) are not specific to the Redis Ruby client but as this is a
Ruby blog, I express them in Ruby code.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;redis_client = Redis.new
redis_client.lpush 'stack', 'member' # push element onto top of stack
redis_client.lpop 'stack'            # pop an element off the stack
redis_client.lrange, 'stack', 0, 0   # peek at the top element of 
                                     # the stack. lrange takes a
                                     # key, a starting index in the
                                     # list, and an ending index
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The Ruby Redis client allows commands to be called in two ways. The
way you will probably want to use it is by calling the command as an
instance method of the client. Redis-rb does a little method missing
magic with that and calls the instance method #call_command, passing it
an array representing the command and its arguments.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#the following will produce the same result
redis_client.call_command ['lpush', 'stack', 'member']
redis_client.lpush 'stack', 'member'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With that, lets implement our Stack. It will take a hash of options
when initalized. The options should include a :key to store our stack in and a :redis
instance. Each of the commands will only operate on the given :key
using the given :redis client.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Redis::Stack
  attr_reader :key

  def initialize(options)
    @redis = options[:redis]
    @key = options[:key]
  end 

  def push(member)
    @redis.lpush key, member
  end 

  def pop
    @redis.lpop
  end

  def peek
    (@redis.lrange key, 0, 0).first # lrange returns an array
                                    # peek should return a member
  end 

  def to_a
    @redis.lrange key, 0, -1
  end 

  def size
    to_a.size
  end

end

stack = Stack.new(:redis =&amp;gt; Redis.new, :key =&amp;gt; 'my_stack')
stack.size # 0
stack.to_a # []
stack.peek # nil
stack.pop  # nil

stack.push 1
stack.push 2
stack.size # 2
stack.to_a # [2, 1]
stack.peek # 2
stack.pop  # 2
stack.peek # 1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There you have it. A dirt simple stack using Redis. Of course, there
are some edge cases to handle before using the class but this is more
about using Redis so I have ignored those factors here. You will also,
of course, need an instance of redis-server running.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Why I Ruby, Maybe Why You Should Too</title>
    <link href="http://www.themodestrubyist.com/2010/04/14/why-i-ruby-maybe-why-you-should-too/" rel="alternate"/>
    <id>http://www.themodestrubyist.com/2010/04/14/why-i-ruby-maybe-why-you-should-too/</id>
    <published>2010-04-14T00:00:00-07:00</published>
    <updated>2010-04-14T00:00:00-07:00</updated>
    <author>
      <name>Jordan Ryan West</name>
    </author>
    <summary type="html">&lt;p&gt;Its been a little longer than I had hoped since my last post. Since
I have been on vacation, working little with even less to write about,
I thought I would do a little ditty on why Ruby has become my primary
programming/scripting language and why I like it so much&amp;hellip;&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;Its been a little longer than I had hoped since my last post. Since
I have been on vacation, working little with even less to write about,
I thought I would do a little ditty on why Ruby has become my primary
programming/scripting language and why I like it so much.&lt;/p&gt;

&lt;h3&gt;The Simple Answer: Its Concise Syntax&lt;/h3&gt;

&lt;p&gt;Before programming in Ruby I programmed or had programmed in C, PHP,
ASP(.NET), Javascript, Perl, Java and Scheme. Each of them has their
strengths and their quirks. I could never put my finger on what
bothered me about each of &amp;lsquo;em. I still work in several of
them when appropriate but when I first started toying with Ruby that
feeling of 'something is missing,&amp;rsquo; never came.&lt;/p&gt;

&lt;p&gt;Ruby&amp;rsquo;s syntax is as close to reading plain English as it gets in my
experience. Concise, easy to read code is self-documenting and
requires less comments. It, also, makes code written by others (even
without comments) easier to pickup. A developers style, especially one
very different from yours, can make it difficult to read their
code. In my opinion this is much less the case with Ruby. There is
something so straight-forward about Ruby that when you pick up a
foreign codebase for some light reading (&amp;lsquo;cause we all have that extra
time) it does not take much effort to figure out what is where and how
it is all put together. At the same time there are many, many ways to
skin your cat (mine&amp;rsquo;s polka-dotted).&lt;/p&gt;

&lt;p&gt;When I was first picking up Ruby I came across a script that counted
character frequencies in a file, a good example. Since I was new to
the language I first rewrote the script in C. I, then, following the
book containing the example, copied the script in Ruby. I now keep
both those scripts printed out side-by-side above my desk as an
example of how beautiful Ruby can be and how far programming has
become.&lt;/p&gt;

&lt;h3&gt;Great Community, Great Documentation, Great Reading&lt;/h3&gt;

&lt;p&gt;Learning a new programming language is not always easy. Properietary
languages like those developed by Microsoft do not always have the
most accessible documentation. For old, time tested languages like C &amp;amp; PHP
there is great documentation out there but there is even more awful or
outdated content. Corporations seeking to churn out reading for
developers quickly put together books without the best practices in
mind, dirtying the pile. All this together makes finding examples of
common solutions all the more difficult to follow and learn from. I
don&amp;rsquo;t mean to say that there does not exist great reading on other
languages, there does. However, just like finding a great app on the
AppStore is getting more and more difficult so does finding good
content the older a programming language gets.&lt;/p&gt;

&lt;p&gt;With Ruby this is not the case and because of the community I believe
it will not occur, at least, not for a while. Rubyists seem to put much
more work into documentation or at least writing clear, concise
examples. When googling for an answer you will typically find an RDoc
right away or a well-written article. Searching for answers to a
question about PHP was never as easy for me. Documentation of Ruby
libraries is usually long-winded, so much so that at first glance it
may seem like overkill. Then you need an answer to your &amp;lsquo;obscure case&amp;rsquo;
and instead of digging for hours and hours to find a credible answer
you will only need seconds to find it right in the documentation or a
few results down in a great article.&lt;/p&gt;

&lt;p&gt;Some, including me, originally get thrown off by the Rails instilled
motto of &amp;lsquo;convention over configuration&amp;rsquo; that has spread to most of
the Ruby ecosystem. In fact, its spread is one of the reasons Ruby is
so great. When getting down and dirty with a new library or framework,
conventions have made finding similar answers possible in similar
ways. Yesterday, I needed to quickly view some metadata from my F-Spot
database via HTTP. I take any opportunity I can to write a little
Ruby, improve my chops, but I didn&amp;rsquo;t feel like setting up a Rails app
for the few times I will ever need this functionality. I had never
used &lt;a href="http://www.sinatrarb.com/"&gt;Sinatra&lt;/a&gt; or
&lt;a href="http://datamapper.org/"&gt;DataMapper&lt;/a&gt; before but had read up on both. I
figured this would be a good time to do some self-education on some
great Ruby tools. So I set out to write my little application. With
two quick searches for &amp;lsquo;datamapper sqlite3&amp;rsquo; and &amp;lsquo;sinatra ruby&amp;rsquo; I was
ready to go. Within two hours I had learned two great libraries, new
to me, and solved my problem. The similarties between DataMapper &amp;amp;
ActiveRecord and Sinatra &amp;amp; Rack(/Rails) made the process all the
easier. The great documentation filled in any blanks I needed along
the way.&lt;/p&gt;

&lt;p&gt;If your a book reader like me there are many great Ruby books and some
of them are even free, online. If your already into Ruby you can
download &lt;a href="http://blog.rubybestpractices.com/posts/gregory/022-rbp-now-open.html"&gt;Ruby Best Practices for
free&lt;/a&gt;. Other
great starters include &lt;a href="http://www.amazon.com/Ruby-Way-Second-Techniques-Programming/dp/06723288440"&gt;The Ruby
Way&lt;/a&gt;
and the ever popular &lt;a href="http://pragprog.com/titles/rails3/agile-web-development-with-rails-third-edition"&gt;Agile Web Development with
Rails&lt;/a&gt;
(&lt;a href="http://pragprog.com/titles/rails4/agile-web-development-with-rails-4th-edition"&gt;4th edition now in
beta&lt;/a&gt;). Good
Ruby books are much more common than bad ones, which is normally not
the case in my experience. Simply put, Ruby is one of the easiest
programming languages to learn.&lt;/p&gt;

&lt;h3&gt;Have it Your Way&lt;/h3&gt;

&lt;p&gt;When I was a student in high school I had a chemistry intstructor who
would start class by telling us wether or not it was going to be a
Burger King Day: a day where we could work on a project of our
choosing or, &amp;lsquo;Have it Your Way.&amp;rsquo; We called the not fun days McDonalds
days. Ironically, I prefer the golden arches when poisoning my body
but I digress.&lt;/p&gt;

&lt;p&gt;Rubyists have adopted the have it your way model and even more so
recently. The redesign of Rails 3 makes the already great framework
100% ORM, test, client-side, and templating framework
agnostic. ActiveRecord can be used apart from Rails, say in your
Sinatra application or, like me, you can use DataMapper instead. I
could have easily used another ORM as well. Nothing sums up this idea
better than &lt;a href="http://gist.github.com/54177"&gt;Ryan Tomayko&amp;rsquo;s &amp;ldquo;No require &amp;lsquo;rubygems&amp;rsquo;&amp;rdquo;
doctrine&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;Not a Language of the Web, but Perfect Enough&lt;/h3&gt;

&lt;p&gt;Unlike PHP, Ruby is not a programming language primarily interacted
with via a web server. Sure, PHP CLI exists and is probably used, but
most PHP code is fired via Apache, IIS, Nginx, etc. Ruby is a
scripting language whose first uses were not for web development. Ruby
does include WEBrick as part of its Standard Library and several other
servers are distributed as gems. Its because of Rails and other Ruby
web frameworks that take advantage of this that Ruby has also become a
language of the web.&lt;/p&gt;

&lt;p&gt;When developing multiple PHP applications on the same workstation each
new application sandbox requires cumbersome editing of server
configuration files. With Rack, Rails or any other Ruby web framework
simply calling a command-line script in an extra terminal window is
all that is needed to get going. To run multiple applications at the
same time just change the port number when running the server
script. The barrier to the web is much thinner with Ruby. Setting up a
new machine to continue developing all your existing PHP applications
is much more time consuming and painful than it is for Ruby code and
applications. Tools like Ruby Gems and Bundler make this even easier.&lt;/p&gt;

&lt;p&gt;In gaining popularity, Ruby has been used in applications like
MacRuby, JRuby and IronRuby to facilitate the writing of Mac, Java and
.NET applications respectively. Such endorsement is a huge nod to
Ruby&amp;rsquo;s strengths. It shows how versatile a programming language Ruby
really is. Ruby has uses for many types of problems from those solved
by Apache and PHP to ones solved by a simple bash script.&lt;/p&gt;

&lt;p&gt;Ruby changed the way I write code. Its community and tools like RSpec
and Cucumber have really instilled the idea of Behavior-Driven
Development in me along with many other good programming
practices. The language made me a better programmer no matter which
one I write in. Alan Skorkin wrote &lt;a href="http://www.skorks.com/2010/03/the-difference-between-a-developer-a-programmer-and-a-computer-scientist/"&gt;a great
article&lt;/a&gt;
about the differences between the Computer Scientist, the Programmer
and the Developer. Ruby really is the language of the Programmer.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Aruba - Cucumber Goodness for the Command-Line</title>
    <link href="http://www.themodestrubyist.com/2010/04/22/aruba---cucumber-goodness-for-the-command-line/" rel="alternate"/>
    <id>http://www.themodestrubyist.com/2010/04/22/aruba---cucumber-goodness-for-the-command-line/</id>
    <published>2010-04-22T00:00:00-07:00</published>
    <updated>2010-04-22T00:00:00-07:00</updated>
    <author>
      <name>Jordan Ryan West</name>
    </author>
    <summary type="html">&lt;p&gt;Recently, I have been spending some of my extra time writing a little
command-line application for interacting with the &lt;a href="http://bookworm.oreilly.com"&gt;O'Reilly
Bookworm&lt;/a&gt; Public API. I remembered
reading about &lt;a href="http://github.com/aslakhellesoy/aruba"&gt;Aruba&lt;/a&gt;, a
&lt;a href="http://cukes.info"&gt;Cucumber&lt;/a&gt; &amp;lsquo;add-on&amp;rsquo; similar to
&lt;a href="http://github.com/aslakhellesoy/cucumber-rails"&gt;cucumber-rails&lt;/a&gt; but
for the command-line, on
&lt;a href="http://thechangelog.com/post/388235527/aruby-cucumber-from-the-command-line"&gt;TheChangeLog&lt;/a&gt;. Aruba&amp;rsquo;s
own step definitions are self-documented in its own features and
scenarios but I will cover some of the basics here. I will also cover
some useful parts of the API defined by Aruba that you can use to
write your own steps&amp;hellip;&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;Recently, I have been spending some of my extra time writing a little
command-line application for interacting with the &lt;a href="http://bookworm.oreilly.com"&gt;O'Reilly
Bookworm&lt;/a&gt; Public API. I remembered
reading about &lt;a href="http://github.com/aslakhellesoy/aruba"&gt;Aruba&lt;/a&gt;, a
&lt;a href="http://cukes.info"&gt;Cucumber&lt;/a&gt; &amp;lsquo;add-on&amp;rsquo; similar to
&lt;a href="http://github.com/aslakhellesoy/cucumber-rails"&gt;cucumber-rails&lt;/a&gt; but
for the command-line, on
&lt;a href="http://thechangelog.com/post/388235527/aruby-cucumber-from-the-command-line"&gt;TheChangeLog&lt;/a&gt;. Aruba&amp;rsquo;s
own step definitions are self-documented in its own features and
scenarios but I will cover some of the basics here. I will also cover
some useful parts of the API defined by Aruba that you can use to
write your own steps.&lt;/p&gt;

&lt;p&gt;Simply put, Aruba is a gem that defines steps and helpers you can use
to drive out your command-line application. You can use Aruba to test
not only applications written in Ruby but also C, Perl or any other
script that you can run from your command prompt. For Ruby
applications Aruba also supports &lt;a href="http://rvm.beginrescueend.com/"&gt;RVM&lt;/a&gt;
so you can test your applications in specific environments. Use Aruba
to control not only the Ruby version but also what gems are available
to the system. If your developing a command-line app and use Cucumber,
Aruba is for you.&lt;/p&gt;

&lt;p&gt;If you would like to get started with Aruba install it with&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt; gem install aruba
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You will need Cucumber installed as well to get going.&lt;/p&gt;

&lt;h3&gt;Starting your Aruba Vacation (Setting up the Environment)&lt;/h3&gt;

&lt;p&gt;Although you can use Aruba to test your C or Java application, this is a Ruby blog
so I will discuss how to use it while writing an executable contained
in a Ruby gem. I setup my gem directory like so:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;- some_gem/
  - bin/
    -executable
  - lib/
    - some_gem.rb
    - some_gem/
  - features/
    - support/
      - env.rb
    - step_definitions/
      - some_steps.rb
      - more_stems.rb
    - feature1.feature
    - feature2.feature
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;By convention, I use the file &lt;em&gt;features/support/env.rb&lt;/em&gt; to get Aruba up
and running in our Cucumber environment. The first and only thing we
need to do, for now, is include the library. Add&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require 'aruba'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;to &lt;em&gt;env.rb&lt;/em&gt;. Make sure everything is ok by running cucumber on the empty
features directory.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt; cucumber 
0 scenarios
0 steps
0m0.000s
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Running Executables&lt;/h3&gt;

&lt;p&gt;For the purposes of this post lets continue to work with the previous
example and assume we are to drive out the functionality of the
application bin/executable. When we write our scenarios we would like
to address the running of this application, possibly with various
options, as we would from the command-line. We would, also, like to
test the output our application writes to STDOUT.  The first few steps
you need to know about are defined as,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;When /^I run "(.*)"$/ do |cmd|
  run(unescape(cmd))
end

Then /^I should see "([^\"]*)"$/ do |partial_output|
  combined_output.should =~ compile_and_escape(partial_output)
end

Then /^I should see:$/ do |partial_output|
  combined_output.should =~ compile_and_escape(partial_output)
end

Then /^I should not see "([^\"]*)"$/ do |partial_output|
  combined_output.should_not =~ compile_and_escape(partial_output)
end

Then /^I should not see:$/ do |partial_output|
  combined_output.should_not =~ compile_and_escape(partial_output)
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For our purposes we would write something like&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Scenario: Running our Executable
  When I run "executable --someoption"
  Then I should see:
    """
    Hello World!
    This is my executable
    """
  And I should not see "something I don't wanted printed"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you run this scenario you will probably see a similar error to this
one in the output, even if you have created the file bin/executable.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; Then I should see:                 # 1.7/lib/aruba/cucumber.rb:66
   """
   Hello World!
   This is my executable
   """
  expected: [removed this for presentation purposes],
       got: "\n------------------------------------------\
    ----------------------------\nsh: executable: not found\n" \
    (using =~)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Aruba allows you to play with files as well. For that reason it must
do things in a temporary place. As a result the load path isn&amp;rsquo;t as
straight forward as you might or I did assume. Changing&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; When I run "executable --someoption"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;to&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;When I run "bin/executable --someoption"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;will not work either. You could install the gem each time but that
would be a hassle. Instead, we need to rewrite one of Aruba&amp;rsquo;s helper
methods to work with our executable. I stole the following idea from
&lt;a href="http://github.com/rspec/rspec-core/blob/master/features/support/env.rb"&gt;RSpec-2&amp;rsquo;s aruba
setup&lt;/a&gt;. For
our example we need to add this so we can start going from red to
green.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# features/support/env.rb
module ArubaOverrides
  def detect_ruby_script(cmd)
    if cmd =~ /^executable /
      "ruby -I../../lib -S ../../bin/#{cmd}"
    else
      super(cmd)
    end
  end
end

World(ArubaOverrides)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This code is pretty straight forward. We define a module
ArubaOverrides; you could have named it anything. Aruba defines a
helper, &lt;em&gt;detect_ruby_script&lt;/em&gt;, which we override. This method is called
by another helper, run (see the step definitions above). If the
command we run starts with &amp;lsquo;executable&amp;rsquo;, in this case, we run it
explicitly using the -S option and add our library to the load path
with -I. Finally, we use World(ArubaOverrides) to include the helper
in the Cucumber World.&lt;/p&gt;

&lt;p&gt;Now if we write the following little script,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# bin/executable
#!/usr/bin/ruby
puts "Hello World!"
puts "This is my executable"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and do a little permission modification,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt; chmod +x bin/executable
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;we should be in the green when we run our scenario again.&lt;/p&gt;

&lt;h3&gt;Aruba Tags&lt;/h3&gt;

&lt;p&gt;Aruba uses four special tags to make running scenarios a bit more
detailed or to help with debugging. The tags and their side-effects
are,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@announce-cmd    # prints a command called by 'run'
@announce-stdout # prints the final output written to STDOUT
@announce-stderr # prints the final output written to STDERR
@announce        # all of the above
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Adding the tag @announce-cmd to our scenario would result in
the following output when run.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;When I run "executable --someoption"
$ /usr/bin/ruby1.8 -I../../lib -S ../../bin/executable --someoption
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Some more useful steps&lt;/h3&gt;

&lt;p&gt;If you would like to include steps about your application&amp;rsquo;s exit status
Aruba makes it easy. Here are some scenario&amp;rsquo;s from Aruba&amp;rsquo;s own
features:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Scenario: exit status of 0
  When I run "ruby -h"
  Then the exit status should be 0

Scenario: non-zero exit status
  When I run "ruby -e 'exit 56'"
  Then the exit status should be 56
  And the exit status should not be 0
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There is also a step definition that helps test an expected successful
exit. The first step will pass and the second will fail.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Scenario: Success
  When I successfully run "ruby -e 'exit 0'"
  And I successfully run "ruby -e 'exit 10'"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Some other useful exit status related steps are defined as,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Then /^the exit status should be (\d+)$/ do |exit_status|
  @last_exit_status.should == exit_status.to_i
end

Then /^the exit status should not be (\d+)$/ do |exit_status|
  @last_exit_status.should_not == exit_status.to_i
end

Then /^it should (pass|fail) with:$/ do |pass_fail, partial_output|
  Then "I should see:", partial_output
  if pass_fail == 'pass'
    @last_exit_status.should == 0
  else
    @last_exit_status.should_not == 0
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you would like to test the output written to standard out you can
use the &lt;em&gt;I should (not) see&lt;/em&gt; steps we used above. You can also test
STDOUT and STDERR with,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Then /^the stderr should contain "([^\"]*)"$/

Then /^the stderr should not contain "([^\"]*)"$/

Then /^the stdout should contain "([^\"]*)"$/

Then /^the stdout should not contain "([^\"]*)"$/
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I mentioned above that Aruba lets you work with temporary files. Here
are some more examples right from Aruba&amp;rsquo;s own feature set.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Scenario: create a dir
  Given a directory named "foo/bar"
  When I run "ruby -e \"puts test ?d, 'foo'\""
  Then the stdout should contain "true"

Scenario: append to a file
  Given a file named "foo/bar/example.rb" with:
    """
    puts "hello world"
    """
  When I append to "foo/bar/example.rb" with:
    """
    puts "this was appended"
    """
  When I run "ruby foo/bar/example.rb"
  Then I should see "hello world"
    And I should see "this was appended"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I mentioned Aruba has support for RVM and control over gems
installed when testing your script. The &lt;a href="http://github.com/aslakhellesoy/aruba/blob/master/README.rdoc"&gt;Aruba
README&lt;/a&gt;
has some great examples, so I point you there. You can also check out
all of the defined steps
&lt;a href="http://github.com/aslakhellesoy/aruba/blob/master/lib/aruba/cucumber.rb"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;Writing your own steps with the Aruba API&lt;/h3&gt;

&lt;p&gt;If you need something more than the steps defined for you by Aruba you
can use its API to write your own. You have already seen how Aruba
uses its helpers in its own steps, above. I will address some of the
useful ones.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;run(cmd)&lt;/em&gt; helper runs a command passed to it. This string should
be the same as if you were running it from your command prompt. &lt;em&gt;run&lt;/em&gt;
uses IO.popen to run your command as a sub-process, storing the results
of STDOUT and STDERR. You can check these variables in your
steps. They are named &lt;em&gt;@last_stdout&lt;/em&gt; and &lt;em&gt;@last_stderr&lt;/em&gt;,
respectively. &lt;em&gt;@last_exit_status&lt;/em&gt;, also, gets defined in this helper.&lt;/p&gt;

&lt;p&gt;You can move around directories with the helper &lt;em&gt;cd(dir)&lt;/em&gt;. This
simulates calling &amp;lsquo;cd&amp;rsquo; from a prompt. You can create a directory, with
&lt;em&gt;create_dir(dir_name)&lt;/em&gt;, before changing into it. If you would like to
create or append to a file use the helpers &lt;em&gt;create_file(filename,
file_content)&lt;/em&gt; or &lt;em&gt;append_to_file(filename, file_content)&lt;/em&gt;. Don&amp;rsquo;t feel
the need to create the entire path to a temporary file,
however. &lt;em&gt;create_file&lt;/em&gt; does this for you with
&lt;em&gt;FileUtils#mkdir_p&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;check_file_presence(paths, expect_presence)&lt;/em&gt; is a useful one for your
&amp;lsquo;Then&amp;rsquo; steps. It loops through the array of given paths. If
&lt;em&gt;expect_presence&lt;/em&gt; takes on a true value, it will pass if each path
given points to a file. If &lt;em&gt;expect_presence&lt;/em&gt; is false, it will pass,
otherwise.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;create_dir('some/dir') cd('some/dir')
create_file(File.expand_path('~/.dotfile', 'someprop: some_val\n')
check_file_presence(['file1', 'dir/file2'], true)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;One final useful helper you can use in your tests is
&lt;em&gt;check_file_content(file, partial_content, expect_match)&lt;/em&gt;. This helper
passes if the contents of &lt;em&gt;file&lt;/em&gt;, a path, matches (using regular
expressions) the given &lt;em&gt;partial_content&lt;/em&gt; when &lt;em&gt;expect_match&lt;/em&gt; is
true. If &lt;em&gt;expect_match&lt;/em&gt; takes on a false value, the helper passes when
the &lt;em&gt;partial_content&lt;/em&gt; is not contained in &lt;em&gt;file&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Well, I hope you enjoyed our little vacation to Aruba as much as I
did. If you already use Cucumber for your code/gems or Rails
applications, using Aruba is a nice addition to the toolbox. There are
some things to note, however. Remember, Aruba runs your code in a
sub-process. This changes a few things. For example, even though you
should not be stubbing in Cucumber, if you need to, don&amp;rsquo;t be surprised
when a stub doesn&amp;rsquo;t work when a method is called inside your
executable. This is because your code is run in an entirely separate
context from the Cucumber runner. It is important to take this into
consideration when writing your own acceptance tests.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Introducing Mongomatic</title>
    <link href="http://www.themodestrubyist.com/2010/08/15/introducing-mongomatic/" rel="alternate"/>
    <id>http://www.themodestrubyist.com/2010/08/15/introducing-mongomatic/</id>
    <published>2010-08-15T00:00:00-07:00</published>
    <updated>2010-08-15T00:00:00-07:00</updated>
    <author>
      <name>Jordan Ryan West</name>
    </author>
    <summary type="html">&lt;p&gt;&lt;a href="http://mongomatic.com"&gt;Mongomatic&lt;/a&gt; is a Ruby object mapper for &lt;a href="http://www.mongodb.org/"&gt;MongoDB&lt;/a&gt; that aims to be simple, have minimal dependencies and adhere to native Mongo conventions. Mongomatic brings you just close enough to the &lt;a href="http://github.com/mongodb/mongo-ruby-driver"&gt;Ruby mongo driver&lt;/a&gt; wherever possible. It uses Ruby&amp;rsquo;s hash syntax to access data stored in the document including sub-documents, which are plain nested hashes. Documents are queried using the native query syntax. Mongomatic also supports validation and callbacks with a much simpler API than other Mongo object mappers modeled after ActiveRecord&amp;hellip;&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;&lt;a href="http://mongomatic.com"&gt;Mongomatic&lt;/a&gt; is a Ruby object mapper for &lt;a href="http://www.mongodb.org/"&gt;MongoDB&lt;/a&gt; that aims to be simple, have minimal dependencies and adhere to native Mongo conventions. Mongomatic brings you just close enough to the &lt;a href="http://github.com/mongodb/mongo-ruby-driver"&gt;Ruby mongo driver&lt;/a&gt; wherever possible. It uses Ruby&amp;rsquo;s hash syntax to access data stored in the document including sub-documents, which are plain nested hashes. Documents are queried using the native query syntax. Mongomatic also supports validation and callbacks with a much simpler API than other Mongo object mappers modeled after ActiveRecord.&lt;/p&gt;

&lt;h3&gt;Jumping Right In&lt;/h3&gt;

&lt;p&gt;You can install Mongmatic through &lt;a href="http://rubygems.org"&gt;RubyGems&lt;/a&gt; or by cloning the &lt;a href="http://github.com/benmyles/mongomatic"&gt;GitHub repository&lt;/a&gt; and building it yourself. To install with RubyGems run:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gem install mongomatic
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Since connection details are a bit mundane lets start with a little example first. All Mongomatic models inherit from Mongomatic::Base.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Post &amp;lt; Mongomatic::Base
  include Mongomatic::Expectations::Helper

  private 

  def validate
    expectations do 
      be_present self['title'], "Title must not be empty"

      not_be_a_match self['alias'], 
                     "Alias must not contain uppercase characters", :with =&amp;gt; /[A-Z]/

      be_of_length self['authors'], "Must have alteast one author", :minimum =&amp;gt; 1
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The above example defines a class called Post. Each document will be stored in the Post collection. I will discuss the validation portion of the code further on.&lt;/p&gt;

&lt;p&gt;We can now use our class like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# instantiate a new document
post = Post.new(:title =&amp;gt; 'Post Title')
post['alias'] = 'A'
p.new? =&amp;gt; true

# insert invalid document
post.valid? =&amp;gt; false
post.insert =&amp;gt; false
post.errors.full_messages =&amp;gt; ["Alias must not contain uppercase characters", ...]

# insert valid document
post['alias'] = 'abc'
post['authors'] = ['Jordan West']
post.insert =&amp;gt; BSON::ObjectID
post.new? =&amp;gt; false
post.insert =&amp;gt; false # can only insert new records

# update document
post['title'] = 'New Post Title'
post.update

# iterate through docs with a cursor
posts = Post.find(:alias =&amp;gt; 'abc') =&amp;gt; Mongomatic::Cursor
posts.each do |post|
  # code here
end

# find single document
Post.find_one(:title =&amp;gt; 'New Post Title') =&amp;gt; Post
Post.find_one(post['_id']) =&amp;gt; Post

# remove document
post.remove
post.removed? =&amp;gt; true

Post.find_one(:title =&amp;gt; 'New Post Title') =&amp;gt; nil
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Getting Connected&lt;/h3&gt;

&lt;p&gt;Mongomatic supports global and per-model connections. This means that each model can represent collections on different servers, clusters, or databases. The connection is established using the Mongo Ruby driver and then provided to Mongomatic. Below we define a global connection object.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Mongomatic.db = Mongo::Connection.new.db("modest_rubyist")
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Connections are defined in the same way for a single class.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Post.db = Mongo::Connection.new.db("another_mr_db")
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Validation&lt;/h3&gt;

&lt;p&gt;Validation can be done in one of two ways in Mongomatic. Both ways share some common elements. All validation is done inside the class' validate method. Validity is determined by the number of errors generated on an instance. The first validation method is to generate the errors manually. Error messages are pushed onto the instance&amp;rsquo;s errors array if an error condition is met. We could rewrite part of our first example like this,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Post &amp;lt; Mongomatic:Base
  private

  def validate
    errors &amp;lt;&amp;lt; ['name', 'cannot be blank'] if self['name'].blank?
  end
end

p = Post.new
p.insert =&amp;gt; false
p.errors.full_messages =&amp;gt; ['name cannot be blank']
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The second method is to include the RSpec-inspiried Mongomatic::Expectations::Helper module like we did earlier,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Post &amp;lt; Mongomatic::Base
  include Mongomatic::Expectations::Helper

  private 

  def validate
    expectations do 
      be_present self['title'], "Title must not be empty"
      not_be_a_match self['alias'], 
                     "Alias must not contain uppercase characters", :with =&amp;gt; /[A-Z]/
      be_of_length self['authors'], "Must have alteast one author", :minimum =&amp;gt; 1
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Expectations can only be used inside the of expectations block helper. Each expectation takes a value, an error message and possibly some options. There are several other expectations you can use. Checkout the &lt;a href="http://mongomatic.com/rdoc/README_rdoc.html"&gt;README&lt;/a&gt; for more info.&lt;/p&gt;

&lt;h3&gt;Callbacks&lt;/h3&gt;

&lt;p&gt;Mongomatic supports simple callbacks. Callbacks are defined as instance methods much like validation is. The following callbacks can be defined on our Post model:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Post &amp;lt; Mongomatic::Base
  private

  def before_validate
  end

  def after_validate
  end

  def before_insert
  end

  def before_insert_or_update
  end

  def after_insert_or_update
  end

  def after_insert
  end

  def before_update
  end

  def after_update 
  end

  def before_remove
  end

  def after_remove
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;The Indexes Convention&lt;/h3&gt;

&lt;p&gt;Although Mongomatic does not provide an API for creating indexes, it is recommended to use this convention&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Post &amp;lt; Mongomatic::Base
  def self.create_indexes
    collection.create_index('title', :unique =&amp;gt; true)
    collection.create_index('alias')
  end

  def self.drop_indexes
    collection.drop_indexes
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Define two class methods, one to create the indexes and the other to drop them. Indexes are created and dropped using the Mongo Ruby driver&amp;rsquo;s create_index and drop_indexes methods. Refer to the &lt;a href="http://github.com/mongodb/mongo-ruby-driver"&gt;Ruby mongo driver&lt;/a&gt; docs for more index options.&lt;/p&gt;

&lt;h3&gt;Safe Methods and Unique Values&lt;/h3&gt;

&lt;p&gt;You can use unique indexes and Mongomatic&amp;rsquo;s safe inserts to detect when duplicate values are stored in the same field. Taking the example from the section before we could check for duplicate post titles.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Post.new(:title =&amp;gt; "Dont Duplicate").insert!
Post.new(:title =&amp;gt; "Dont Duplicate").insert! # raises Mongo::OperationFailure
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Each operation has a corresponding safe, bang-method. Safe methods pass the :safe =&gt; true option to the Mongo ruby driver when running operations and raise a Mongo::OperationFailure exception if something went wrong.&lt;/p&gt;

&lt;h3&gt;More Resources&lt;/h3&gt;

&lt;p&gt;If you like Mongomatic check out &lt;a href="http://mongomatic.com"&gt;it&amp;rsquo;s shiny new website&lt;/a&gt;, designed by &lt;a href="http://christopherhein.com/"&gt;Christopher Hein&lt;/a&gt;, who did an awesome job. You can also visit the &lt;a href="http://github.com/benmyles/mongomatic"&gt;GitHub repository&lt;/a&gt; or the &lt;a href="http://mongomatic.com/rdoc/"&gt;docs&lt;/a&gt;. We will be adding to the &lt;a href="http://github.com/benmyles/mongomatic/wiki"&gt;Wiki&lt;/a&gt; soon. Also, check back soon because I will have another post on working with sub-documents, relationships, arrays, counters and writing your own expectations.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>On Mongomatic and Sinatra, Padrino and Rails 3 Support</title>
    <link href="http://www.themodestrubyist.com/2010/08/26/on-mongomatic-and-sinatra-padrino-and-rails-3-support/" rel="alternate"/>
    <id>http://www.themodestrubyist.com/2010/08/26/on-mongomatic-and-sinatra-padrino-and-rails-3-support/</id>
    <published>2010-08-26T00:00:00-07:00</published>
    <updated>2010-08-26T00:00:00-07:00</updated>
    <author>
      <name>Jordan Ryan West</name>
    </author>
    <summary type="html">&lt;p&gt;If you have played with Mongomatic, by now you&amp;rsquo;ve probably started using it with your favorite Ruby web framework. Recently, I released &lt;a href="http://github.com/jrwest/sinatra-mongomatic"&gt;sinatra-mongomatic&lt;/a&gt; and &lt;a href="http://github.com/jrwest/mongomatic-rails3"&gt;mongomatic-rails3&lt;/a&gt;, gems adding support for Sinatra and Rails, respectively.  &lt;a href="http://twitter.com/lusis"&gt;John Vincent&lt;/a&gt;, also, added Mongomatic support to &lt;a href="http://github.com/padrino/padrino-framework"&gt;Padrino&lt;/a&gt; and has, generously, contributed the related section in this post&amp;hellip;&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;If you have played with Mongomatic, by now you&amp;rsquo;ve probably started using it with your favorite Ruby web framework. Recently, I released &lt;a href="http://github.com/jrwest/sinatra-mongomatic"&gt;sinatra-mongomatic&lt;/a&gt; and &lt;a href="http://github.com/jrwest/mongomatic-rails3"&gt;mongomatic-rails3&lt;/a&gt;, gems adding support for Sinatra and Rails, respectively.  &lt;a href="http://twitter.com/lusis"&gt;John Vincent&lt;/a&gt;, also, added Mongomatic support to &lt;a href="http://github.com/padrino/padrino-framework"&gt;Padrino&lt;/a&gt; and has, generously, contributed the related section in this post.&lt;/p&gt;

&lt;h2&gt;Singing Mongomatic with Sinatra&lt;/h2&gt;

&lt;p&gt;Requiring &lt;a href="http://github.com/jrwest/sinatra-mongomatic"&gt;sinatra-mongomatic&lt;/a&gt; in your Sinatra application automatically requires everything you need to use Mongomatic. In addition, it adds the &lt;em&gt;mongomatic&lt;/em&gt; helper method to configure your connection details. sinatra-mongomatic can be used with both classic and modular Sinatra styles.&lt;/p&gt;

&lt;p&gt;Here is an example in the classic style:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require 'sinatra'
require 'sinatra/mongomatic'

# Usually in another file. Included here for brevity
class User &amp;lt; Mongomatic::Base
end

mongomatic Mongo::Connection.new.db("test2")

get '/create/:name' do
  User.new(:name =&amp;gt; params[:name]).insert
  "ok"
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Checkout the &lt;a href="http://github.com/jrwest/sinatra-mongomatic/tree/master/examples"&gt;examples&lt;/a&gt; for more help.&lt;/p&gt;

&lt;h2&gt;Mongmatic and the Godfather&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Written by &lt;a href="http://twitter.com/lusis"&gt;John Vincent&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In a nutshell, &lt;a href="http://padrinorb.com"&gt;Padrino&lt;/a&gt; is a Ruby framework (similar to Rails in concept) built upon the Sinatra Microframework.
Using Mongomatic in Padrino currently requires the version from &lt;a href="http://github.com/padrino/padrino-framework"&gt;Padrino&amp;rsquo;s github repository&lt;/a&gt;. The code has been accepted and will be in the upcoming 0.9.15 release.
Don&amp;rsquo;t worry, the github master branch is very stable and is unit tested on each commit via Hudson. I&amp;rsquo;m going to assume you&amp;rsquo;re using RVM and have a gemset created for testing.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git clone http://github.com/padrino/padrino-framework.git
gem install bundler --pre
cd padrino-framework
bundle install
bundle check
bundle exec rake test # If you're paranoid
bundle exec rake package
bundle exec rake install
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You should now be running the master branch. As with any other ORM supported in Padrino, usage is as simple as this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;padrino g project pblog -d mongomatic
cd pblog
bundle install
padrino g model post name:string body:text
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Your database configuration will be in &lt;em&gt;config/database.rb&lt;/em&gt;.Your generated model file will be in &lt;em&gt;app/models/post.rb&lt;/em&gt;. The model file is heavily commented so take a look if you have any questions. Being true to the nature of MongoDB, Mongomatic doesn&amp;rsquo;t require you to define any model attributes for use. In the case of the Padrino driver, we&amp;rsquo;ve made some assumptions:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;* Defining a column during model generation makes that a required value via 
  Mongomatic's Expectations Helper.
* Any attribute defined as an integer will additionally use Expectations to 
  ensure that data added is an integer.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can interact with your model via the padrino console:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ padrino console
=&amp;gt; Loading development console (Padrino v.0.9.14)
=&amp;gt; Located unlocked Gemfile for development
=&amp;gt; Loading Application Pblog
ruby-1.9.1-p378 &amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;At this point you can interact with your model using normal Mongomatic model methods:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ruby-1.9.1-p378 &amp;gt; Post.empty?
=&amp;gt; true 
ruby-1.9.1-p378 &amp;gt; p = Post.new(:name =&amp;gt; 'My first blog post')
=&amp;gt; #&amp;lt;Post:0x00000003bc7e30 @doc={"name"=&amp;gt;"My first blog post"}, @removed=false, ...&amp;gt; 
ruby-1.9.1-p378 &amp;gt; p.valid?
=&amp;gt; false 
ruby-1.9.1-p378 &amp;gt; p.errors
=&amp;gt; [["body cannot be blank"]] 
ruby-1.9.1-p378 &amp;gt; p["body"] = &amp;lt;&amp;lt;EOT
ruby-1.9.1-p378"&amp;gt; Today I learned how to use Mongomatic with Padrino. 
ruby-1.9.1-p378"&amp;gt; I'm much cooler than the other kids on the block now!
ruby-1.9.1-p378"&amp;gt; EOT
=&amp;gt; "Today I learned how to use Mongomatic with Padrino. I'm much cooler than...
ruby-1.9.1-p378 &amp;gt; p.valid?
=&amp;gt; true 
ruby-1.9.1-p378 &amp;gt; p.insert
=&amp;gt; BSON::ObjectID('4c6fdf1579af411f57000001') 
ruby-1.9.1-p378 &amp;gt; Post.empty?
=&amp;gt; false 
ruby-1.9.1-p378 &amp;gt; Post.count
=&amp;gt; 1 
ruby-1.9.1-p378 &amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Staying true to the adhoc nature of MongoDB, I could add a new field to my record. The model is, however, unaware of this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ruby-1.9.1-p378 &amp;gt; p["author"] = "padrinouser@example.com"
=&amp;gt; "padrinouser@example.com" 
ruby-1.9.1-p378 &amp;gt; p.valid?
=&amp;gt; true 
ruby-1.9.1-p378 &amp;gt; p.update
=&amp;gt; 273 
ruby-1.9.1-p378 &amp;gt; p
=&amp;gt; #&amp;lt;Post:0x00000003bc7e30 @doc={"name"=&amp;gt;"My first blog post", "body"=&amp;gt;"Today I 
     learned how to use Mongomatic with Padrino. I'm much cooler than the other kids       
     on the block now!\n", "_id"=&amp;gt;BSON::ObjectID('4c6fdf1579af411f57000001'), 
     "author"=&amp;gt;"padrinouser@example.com"}, @removed=false, @is_new=false, @errors=[]&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;h4&gt;Current limitations:&lt;/h4&gt;

&lt;p&gt;One nicety that Padrino provides is an &amp;lsquo;admin&amp;rsquo; interface similar in concept to Django. Due to the design of Mongomatic, admin support is not available using the Mongomatic driver. Admin support is &lt;em&gt;NOT&lt;/em&gt; required for any Padrino application so this limitation may not be an issue in your use case.&lt;/p&gt;

&lt;h4&gt;Future enhancements:&lt;/h4&gt;

&lt;p&gt;I&amp;rsquo;d like to add similar validation for each datatype of which the Padrino generator is aware as done with &amp;ldquo;integer&amp;rdquo;.
Mongomatic work is being done in a &lt;a href="http://github.com/lusis/padrino-framework/tree/mongomatic"&gt;branch of the lusis fork&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For more information check out &lt;a href="http://github.com/lusis/padrino-mongomatic-example"&gt;John&amp;rsquo;s example application&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Riding Mongomatic with Rails 3&lt;/h2&gt;

&lt;p&gt;Mongomatic can be used, as provided from the core gem, with Rails 2 &amp;amp; 3. I, myself, and Ben Myles, Mongomatic&amp;rsquo;s creator, use only the Mongomatic gem in both Rails 2 &amp;amp; 3 applications in production. This is what we recommend. However, some Rails developers may be more accustomed to some of Rails' niceties (I&amp;rsquo;m thinking helpers mostly here). Although the core of Mongomatic can still be used with many of these helpers, if you know what to give them, the &lt;a href="http://github.com/jrwest/mongomatic-rails3"&gt;mongomatic-rails3&lt;/a&gt; gem makes it a bit easier for those so inclined and who are also using Rails 3. It also provides you with generators and config file support.&lt;/p&gt;

&lt;p&gt;You can install Mongomatic and mongomatic-rails3 by simply adding the latter to your Gemfile:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gem 'mongomatic-rails3', :require =&amp;gt; 'rails/mongomatic'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and running &lt;em&gt;bundle install&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;After installing you can generate your configuration file with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rails g mongomatic:config [DB]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For more information on this generator you can run it with the &lt;em&gt;&amp;mdash;help&lt;/em&gt; option.&lt;/p&gt;

&lt;p&gt;The config file is somewhat atypical. Its one commonality is that you still define connections for each environment. However, instead of having a verbose YAML file with many keys, you simply provide each environment with an instance of Mongo::DB. You can use ERB to make sharing common connection options simpler. The example below is what is generated if no database is given when you run the config generator.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;development: Mongo::Connection.new.db("test")
test: Mongo::Connection.new.db("test")
production: Mongo::Connection.new.db("test")
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;mongomatic-rails3 fully implements the ActiveModel generator and sets it self as the application ORM. This means that when you run &lt;em&gt;rails g model&lt;/em&gt; a subclass of &lt;em&gt;Mongomatic::Base&lt;/em&gt; is generated. The generator currently doesn&amp;rsquo;t support the auto-generation of validation along with the model like Padrino. However, because I like what John has done support for this will come in a future version.&lt;/p&gt;

&lt;h4&gt;A Note on Scaffolds&lt;/h4&gt;

&lt;p&gt;mongomatic-rails3 adds support for Scaffold generation, however, scaffold support is currently limited. This library does not yet support form_for, due to its hash-only data access syntax (you should instead use form_tag), which scaffolds generate in their views (yet will most likely mean a special form helper for Mongomatic). You can, however, still use scaffolds as a starting point.&lt;/p&gt;
</content>
  </entry>
</feed>
