Combined reader/writer accessors in Ruby, ideal for chaining or DSLs.
Installing
You can install this gem via RubyGems:
gem install attr_combined_accessor
Using
Sometimes you really need to be able to set and get without the limitations of Ruby's default accessors and the equals sign. This is where attr_combined_accessor comes in!
require 'attr_combined_accessor'
class MyAmazingClass
attr_combined_accessor :foo, :bar
end
And now you can use them:
my_amazing_class = MyAmazingClass.new
my_amazing_class.foo # => nil
my_amazing_class.foo('foo')
my_amazing_class.foo # => 'foo'
The attr_combined_accessor gem returns self
from the accessor, which makes chaining setters possible:
my_amazing_class = MyAmazingClass.new
my_amazing_class.foo('foo').bar('bar')
my_amazing_class.foo # => 'foo'
my_amazing_class.bar # => 'bar'
It also creates the standard attr_writer
for the places where you want readability and don't need the DSL or chaining style:
my_amazing_class = MyAmazingClass.new
my_amazing_class.foo = 'foo'
my_amazing_class.foo # => 'foo'
my_amazing_class.foo('notfoo!')
my_amazing_class.foo # => 'notfoo!'
License
This project is licensed via MIT license.