Skip to content

eturino/fluent_accessors

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FluentAccessors

Adds class method fluent_accessor which will create several methods to enable fluent API access to the instance variables.

Gem Version Build Status Code Climate Code Climate Coverage

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 # => 3

fluent method

the Fluent method (getter with an argument) will:

  1. always return self
  2. it will not set the value directly:
  3. if the object responds to a set_myproperty method, it will call that and assume that it will
  4. if the object does not respond to a set_myproperty method, it will call the normal setter myproperty=
  5. if the object does not respond to a set_myproperty nor myproperty= 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 # => false

avoiding 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= # => false

Contributing

  1. Fork it ( https://github.com/eturino/fluent_accessors/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

About

Gem that provides a macro for creating fluent accessors for instance variables, working similar to attr_accessor

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages