Skip to main content

Mobomo webinars-now on demand! | learn more.

Hashie, Intridea’s Hash Toolkit, is today with version 0.2.0 gaining a new member: the Clash.

A Clash is a “Chainable Lazy Hash” that allows you to construct a hash using method syntax. It is meant to be used in a similar way to the way that Rails 2.x’s named_scope or Arel’s query building work. Let’s start with an example:

require 'hashie/clash'  c = Hashie::Clash.new c.where(:abc => 'def').order(:created_at)  c == {:where => {:abc => 'def'}, :order => :created_at}

Pretty neat, right? But you can go beyond that. Clash also allows you to use bang method notation to create, dive into, and return from sub-hashes. Let me show you what I mean:

c = Hashie::Clash.new c.where!.abc('def').ghi(123)._end!.order(:created_at)  c == {:where => {:abc => 'def', :ghi => 123}, :order => :created_at}

By using a bang method, you automatically create a subhash that is then the subject of the chain, allowing you to create more keys on that subhash. Then, if you want to jump back up to the top-level hash, you simply call the _end! method to return up one level in the stack (thanks to jQuery for that particular inspiration).

While Clash is a very simple tool, we hope that it could eventually make its way into some of the new ORMs that are cropping up around NoSQL systems (such as MongoMapper) to provide the same kind of effortless chaining that has made ActiveRecord so easy to work with.

Categories
Author

One of my earliest gems was Mash, a useful tool for creating mocking objects as a Hash. One of the most common problems people had with Mash was a simple one: it conflicted with another class of the same name in extlib! To address this problem as well as give the project some room to grow, Mash is now part of a new toolkit called Hashie. Hashie is available now via Gemcutter and the source, as always, is available on GitHub. To install:

gem install hashie

Hello, Hashie

Hashie is, right now, simply the former Mash code along with a new extended Hash called a Dash. A Dash is a “discrete hash” that has pre-defined properties. It can be used as a dead-simple data object when even something like DataMapper is too heavy, but a Struct is too light (Dash gives you the ability to set per-property defaults as well as initialize from an attributes Hash). For example:

class Person < Hashie::Dash   property :name   property :email   property :occupation, :default => 'Rubyist' end  p = Person.new p.name # => nil p.occupation # => 'Rubyist' p.email = 'abc@def.com' p.email # => 'abc@def.com' p['awesome'] # => NoMethodError  p = Person.new(:name => "Awesome Guy") p.name # => "Awesome Guy" p.occupation # => "Rubyist"

The other advantage Hashie has over Mash is that it’s built from the ground up to avoid conflicts. Instead of adding stringify_keys methods to the Hash class, it’s instead added to a Hashie::Hash subclass. You can, however, get Hashie’s few Hash extensions in the Hash class by including the HashExtensions:

Hash.send :include, Hashie::HashExtensions

Hopefully the move will make it easier for everyone to use it in their projects without fear of running into conflicts, and hopefully you’ll also find the Dash useful. Over time the functionality of Hashie may grow to encompass additional simple and useful extensions of Hash. So install Hashie, your friendly neighborhood Hash toolkit, today!

Categories
Author
1
Subscribe to Hashie