PoroPlus
Friendly methods intended to make POROs more useful. Also useful for creating bounded context / domain models (for example, in services) as first-class objects with custom getters and handy JSON serialization!
Installation
Add this line to your application's Gemfile:
gem 'poro_plus'
And then execute:
$ bundle
Or install it yourself as:
$ gem install poro_plus
Usage
Simply include PoroPlus into your Ruby class:
class MyThing
include PoroPlus
attr_reader :foo, :bar, :tres
end
Then you can:
> thing_1 = MyThing.new(:foo => 'Raven', :bar => 'Writing Desk', :tres => nil)
=> #<MyThing:0x007ff4866ea398 @foo="Raven", @bar="Writing Desk", @tres=nil>
> thing_1.to_hash
=> {:foo=>"Raven", :bar=>"Writing Desk", :tres=>nil}
> thing_1.to_json(:skip_nils => true)
=> "{\"foo\":\"Raven\",\"bar\":\"Writing Desk\"}"
If you want the JSON to include methods that return values:
class MyThing
include PoroPlus
attr_reader :foo, :bar, :tres
def tres
"tres leches con #{self.bar}"
end
end
Then, passing a nil to create a placeholder for that JSON attribute, you can:
> thing_2 = MyThing.new(:foo => 'Taco', :bar => 'cinnammon', :tres => nil) # nil placeholder
=> #<MyThing:0x007ff4866ea398 @foo="Taco", @bar="cinnammon", @tres=nil>
> thing_2.to_json(:skip_nils => true)
=> "{\"foo\":\"Taco\",\"bar\":\"cinnammon\",\"tres\":\"tres leches con cinnammon\"}"
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request