Sha256: d3b64a274566ffa91d5664ea6ec86c62289db9f8702c5d59536f8743587a3c9c

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

HashToObject
============

HashToObject is a mixin for converting hashes into ruby objects.  For instance, if you wanted an Order class that could be instantiated from a hash, you could define it as such:

    class Order
      include HashToObject

      def initialize(options = {})
        objectify(options)
      end
    end

And you have a hash like such:
 
    hash_to_object = {:amount => 25, :type => "credit", :admin => false}

Then you can call `Order.new(hash_to_object)` and get an `Order` object with instance variables `@amount`, `@type`, `@admin`.

Nesting
=======
HashToObject also supports nesting of object creation.  It defines the nested objects' class based on the name of the parent class and the singularized key name of the nested value.

Example:

    Order.new({:item => {:name => "foo"}, :transactions => [{:id => "bar"},{:id => "baz"}])

This would create an object of type `Order`, with instance variables `@item` and `@transactions` linking to objects of type `Order::Item` (with instance variable `@name`), and `Order::Transaction` (with instance variable `@id`).  

The only caveat is that you need to have `Order::Item` and `Order::Transaction` defined, and mix-in HashToObject similarly to `Order` above.

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hash_to_object-0.1.0 README.md