0.0
No commit activity in last 3 years
No release in over 3 years
Converts hashes into ruby objects.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 2.3
 Project Readme

HashToObject

HashToObject is a mixin for converting hashes into ruby objects. For instance, if you wanted to build Foo objects, you could define it as such:

class Foo
  include HashToObject

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

You can now build new Foo objects with arbitrary key value pairs and have access to them.

foo = Foo.new('bar' => 'baz', 'qux' => [1,2,3])

foo.bar
=> "baz"
foo.bar = "garply"
=> "garply"

quux = Foo.new('corge' => 'grault')

quux.bar
=> NoMethodError: undefined method 'bar'
quux.corge
=> "grault"

Recursion and Nesting

HashToObject also supports nesting of object creation. Note that you will need classes that correspond to the hash structure.

class Foo::Item
  include HashToObject

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

class Foo::Lolipop
  include HashToObject

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

  def waldo
    "Where in the world is #{@id}?"
  end
end

foo = Foo.new('item' => {'name' => 'foo'}, 'lolipops' => [{'id'=> 'bar'}, {'id'=> 'baz'}])

foo.item
=> #<Foo::Item:0x007fbf40ee1e78 @name="foo">
foo.lolipops
=> [#<Foo::Lolipop:0x007fbf429d4780 @id="bar">, #<Foo::Lolipop:0x007fbf42bc1a48 @id="baz">]
foo.lolipops.first.waldo
=> "Where in the world is bar?"