Module: CallChain
- Defined in:
- lib/callchain.rb
Class Method Summary collapse
Instance Method Summary collapse
- #call(thing) ⇒ Object
- #chain ⇒ Object
-
#use(*args) ⇒ Object
CallChain implementations.
Class Method Details
.[](key) ⇒ Object
39 40 41 |
# File 'lib/callchain.rb', line 39 def self.[](key) self.bind(key) end |
.bind(method_name, *args) ⇒ Object
43 44 45 |
# File 'lib/callchain.rb', line 43 def self.bind(method_name, *args) lambda { |thing| thing.send(method_name, *args) } end |
Instance Method Details
#call(thing) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/callchain.rb', line 30 def call(thing) return thing unless chain chain.each do |filter| thing = filter.call(thing) yield thing, filter if block_given? end thing end |
#chain ⇒ Object
26 27 28 |
# File 'lib/callchain.rb', line 26 def chain @chain end |
#use(*args) ⇒ Object
CallChain implementations
Individual filters do work with ::call(object)
Compose a CallChain with the ::use method
Compose a CallChain of call chains because it exports ::call(object)
Example:
Pricer = Class.new(CallChain)
class AppTierPricer < Pricer
use PlanScoper, Quantizer, AppGrouper
use UserGrouper
end
21 22 23 24 |
# File 'lib/callchain.rb', line 21 def use(*args) @chain ||= [] @chain.push(*args) end |