AttrHashAccessor provides Hash based attribute accessor macro.
Add this line to your application's Gemfile:
gem 'attr_hash_accessor'
And then execute:
$ bundle
Or install it yourself as:
$ gem install attr_hash_accessor
class MyClass
include AttrHashAccessor
attr_hash_writer :bar
attr_hash_reader :foo, :bar
attr_hash_accessor :baz
end
obj = MyClass.new(foo: 'FOO', bar: 'BAR', baz: 'BAZ')
obj.foo # => 'FOO'
obj.bar = 'BARBAR'
obj.bar # => 'BARBAR'
obj.baz # => 'BAZ'
obj.baz = 'BAZBAZ'
obj.baz # => 'BAZBAZ'
class MyClass
include AttrHashAccessor
attr_hash_writer :foo
attr_hash_reader :foo, &->(val) { Array(val) }
attr_hash_accessor :bar, &->(val) { val || {} }
end
obj = MyClass.new
obj.foo # => []
obj.bar # => {}
obj.foo = [1, 2, 3]
obj.foo # => [1, 2, 3]
obj.bar = {lorem: 'ipsum'}
obj.bar # => {:lorem=>"ipsum"}
class MyClass
include AttrHashAccessor
attr_hash_reader :bar
def self.default
{bar: 'BAR'}
end
end
obj = MyClass.new
obj.bar # => 'BAR'
obj = MyClass.new(bar: 'baz')
obj.bar # => 'baz'
- 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