Adds class method fluent_accessor
which will create several methods to enable fluent API access to the instance variables.
it requires Ruby 2, since it uses keyword arguments
Add this line to your application's Gemfile:
gem 'fluent_accessors'
And then execute:
$ bundle
Or install it yourself as:
$ gem install fluent_accessors
class TestKlass
extend FluentAccessors
fluent_accessor :something
end
x = TestKlass.new
# normal setter
x.something = 1
# normal getter
x.something # => 1
# fluent setter method
x.set_something 2 # returns self
x.something # => 2
# using getter with argument => fluent method
x.something 3 # returns self
x.something # => 3
the Fluent method (getter with an argument) will:
- always return
self
- it will not set the value directly:
- if the object responds to a
set_myproperty
method, it will call that and assume that it will - if the object does not respond to a
set_myproperty
method, it will call the normal settermyproperty=
- if the object does not respond to a
set_myproperty
normyproperty=
methods, it will set the property directly.
if you don't want the set_something
method, you can specify not to create it.
class TestKlass
extend FluentAccessors
fluent_accessor :something, set_method: false
end
x = TestKlass.new
s.respond_to? :set_method # => false
if you don't want the something=
method, you can specify not to create it.
class TestKlass
extend FluentAccessors
fluent_accessor :something, writer_method: false
end
x = TestKlass.new
s.respond_to? :something= # => false
- Fork it ( https://github.com/eturino/fluent_accessors/fork )
- 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 a new Pull Request