Project

chainy

0.0
No commit activity in last 3 years
No release in over 3 years
easy method chaining
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Chainy

Chainy makes method chaining easier.

Usage

Use .chain_attr_accessor to tell Chainy which methods should be chainable:

class YourClass
  chain_attr_accessor :strategy, :timeout
end

.chain_attr_accessor creates getter, setter, but also chain setter:

instance = YourClass.new

# getter:
instance.strategy               # => nil

# setter: 
instance.strategy = :password   # => :password

# chain setters
instance.with_strategy(:mock).with_timeout(30)
instance.strategy               # => :mock
instance.timeout                # => 30

Options

1. hash: true

If hash option is set to true, Chainy sets default getter value to empty Hash and chain setter adds new values to this Hash:

class YourClass
  chain_attr_accessor :options, hash: true
end

instance.options  #=> {}
instance.with_options(option1: value1).with_options(option2: value2).with_options(option3: value3)
instance.options  #=> {option1: value1, option2: value2, option3: value3}

Also, method for removing options is created:

instance.without_options(:option1, :option2)
instance.options  #=> {option3: value3}

2. prefix: 'your_prefix'

Default prefix for chain setters is with_, but you can overwrite it with prefix option:

class YourClass
  chain_attr_accessor :strategy, :timeout, prefix: 'add'
end

You can also change default prefix:

Chainy.configure do |config|
  config.prefix = 'add'
end

Installation

Install gem

gem install chainy

and use it

require 'chainy'

class YourClass
  chain_attr_accessor :strategy, :timeout, prefix: 'add'
end

instance = YourClass.new
instance.add_strategy(:mock).add_timeout(30)

or simply add it to your Gemfile:

gem 'chainy'