Use passive resource to create on instances from hashes especially from third party apis. Allows for the ability place hash manipulation logic in one neat place
Examples:
used with a hash or hash like object > require ‘passive_resource’
> true
> class NewClass < PassiveResource::Base > def whole_name; “#first_name #last_name” end > end
> nil
> instance = NewClass.new(:first_name => ‘grady’, :last_name => ‘griffin’)
> “last_name”=>“griffin”
> instance.first_name
> “grady”
> instance.whole_name
> “grady griffin”
Used as with an api returning json
> class FacebookProfile < PassiveResource::Base > end
> nil
> facebook = FacebookProfile.new(‘graph.facebook.com/19292868552’)
> “name”=>“Facebook Platform”, “picture”=>“”, “link”=>“www.facebook.com/platform”, “likes”=>3923792, “category”=>“Product/service”, “website”=>“developers.facebook.com”, “username”=>“platform”, “founded”=>“2007”, “company_overview”=>“Facebook Platform enables anyone to build social apps on Facebook and the web.”, “mission”=>“To make the web more open and social.”, “about”=>“We’re building the social web. Get the latest here: developers.facebook.com ”, “talking_about_count”=>74536
> facebook.talking_about_count
> 74536
> facebook.founded
> “2007”
Works with code that was previously using hashes
> 74536
> “2007”
Objects are nested automatically > instance = NewClass.new(:locations => [=> ‘Orlando’, :state => “FL”, => ‘Greenwood’, :state => “SC”])
> “state”=>“FL”, “state”=>“SC”]}
> instance.locations
> [“state”=>“FL”, “state”=>“SC”]
> instance.locations.first.city
> “Orlando”
the many method can be use for collections
> array = NewClass.many([=> ‘Orlando’, :state => “FL”, => ‘Greenwood’, :state => “SC”])
> [“state”=>“FL”, “state”=>“SC”]
> array.first.city
> “Orlando”
The key/value interface can be used to add methods > empty = NewClass.new({})
> {}
> empty.some_method NoMethodError: undefined method ‘some_method’ for {}:NewClass
from /usr/local/rvm/gems/ruby-1.9.3-p0/gems/passive_resource-0.0.1/lib/passive_resource/base.rb:55:in `method_missing'
from (irb):6
from /usr/local/rvm/rubies/ruby-1.9.3-p0/bin/irb:16:in `<main>'
> empty = 56
> 56
> empty.some_method