FluentAccessors
Adds class method fluent_accessor which will create several methods to enable fluent API access to the instance variables.
Dependencies
it requires Ruby 2, since it uses keyword arguments
Installation
Add this line to your application's Gemfile:
gem 'fluent_accessors'
And then execute:
$ bundle
Or install it yourself as:
$ gem install fluent_accessors
Usage
Basic usage
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 # => 3fluent method
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_mypropertymethod, it will call that and assume that it will - if the object does not respond to a
set_mypropertymethod, it will call the normal settermyproperty= - if the object does not respond to a
set_mypropertynormyproperty=methods, it will set the property directly.
avoiding set_something method
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 # => falseavoiding creating the writer method
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= # => falseContributing
- 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