Class: Cuprum::Function
- Inherits:
-
Object
- Object
- Cuprum::Function
- Defined in:
- lib/cuprum/function.rb
Overview
Functional object that encapsulates a business logic operation with a consistent interface and tracking of result value and status.
A Function can be defined either by passing a block to the constructor, or by defining a subclass of Function and implementing the #process method.
Direct Known Subclasses
Defined Under Namespace
Classes: NotImplementedError
Instance Method Summary collapse
-
#call(*arguments, **keywords) { ... } ⇒ Cuprum::Result
Executes the logic encoded in the constructor block, or the #process method if no block was passed to the constructor.
-
#chain(function = nil, on: nil, &block) ⇒ Cuprum::Function
Registers a function or block to run after the current function, or after the last chained function if the current function already has one or more chained function(s).
-
#else(function = nil, &block) ⇒ Cuprum::Function
Shorthand for function.chain(:on => :failure).
-
#initialize {|*arguments, **keywords, &block| ... } ⇒ Function
constructor
Returns a new instance of Cuprum::Function.
-
#then(function = nil, &block) ⇒ Cuprum::Function
Shorthand for function.chain(:on => :success).
Constructor Details
#initialize {|*arguments, **keywords, &block| ... } ⇒ Function
Returns a new instance of Cuprum::Function.
125 126 127 |
# File 'lib/cuprum/function.rb', line 125 def initialize &implementation define_singleton_method :process, &implementation if implementation end |
Instance Method Details
#call(*arguments, **keywords) { ... } ⇒ Cuprum::Result
145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/cuprum/function.rb', line 145 def call *args, &block call_chained_functions do Cuprum::Result.new.tap do |result| @errors = result.errors result.value = process(*args, &block) @errors = nil end # tap end # call_chained_functions end |
#chain(function, on: nil) ⇒ Cuprum::Function #chain(on: :nil) {|result| ... } ⇒ Cuprum::Function
Registers a function or block to run after the current function, or after the last chained function if the current function already has one or more chained function(s). This creates and modifies a copy of the current function.
192 193 194 195 196 |
# File 'lib/cuprum/function.rb', line 192 def chain function = nil, on: nil, &block proc = convert_function_or_proc_to_proc(block || function) chain_function(proc, :on => on) end |
#else(function) ⇒ Cuprum::Function #else {|result| ... } ⇒ Cuprum::Function
Shorthand for function.chain(:on => :failure). Registers a function or block to run after the current function. The chained function will only run if the previous function was unsuccessfully run.
215 216 217 218 219 |
# File 'lib/cuprum/function.rb', line 215 def else function = nil, &block proc = convert_function_or_proc_to_proc(block || function) chain_function(proc, :on => :failure) end |
#then(function) ⇒ Cuprum::Function #then {|result| ... } ⇒ Cuprum::Function
Shorthand for function.chain(:on => :success). Registers a function or block to run after the current function. The chained function will only run if the previous function was successfully run.
238 239 240 241 242 |
# File 'lib/cuprum/function.rb', line 238 def then function = nil, &block proc = convert_function_or_proc_to_proc(block || function) chain_function(proc, :on => :success) end |