Class: Micro::Service::Pipeline::Reducer
- Inherits:
-
Object
- Object
- Micro::Service::Pipeline::Reducer
- Defined in:
- lib/micro/service/pipeline/reducer.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#services ⇒ Object
readonly
Returns the value of attribute services.
Class Method Summary collapse
Instance Method Summary collapse
- #&(arg) ⇒ Object
- #>>(arg) ⇒ Object
- #call(arg = {}) ⇒ Object
-
#initialize(services) ⇒ Reducer
constructor
A new instance of Reducer.
- #to_proc ⇒ Object
Constructor Details
#initialize(services) ⇒ Reducer
Returns a new instance of Reducer.
22 23 24 |
# File 'lib/micro/service/pipeline/reducer.rb', line 22 def initialize(services) @services = services end |
Instance Attribute Details
#services ⇒ Object (readonly)
Returns the value of attribute services.
7 8 9 |
# File 'lib/micro/service/pipeline/reducer.rb', line 7 def services @services end |
Class Method Details
.build(args) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/micro/service/pipeline/reducer.rb', line 14 def self.build(args) services = Array(args).flat_map { |arg| map_services(arg) } raise Error::InvalidServices if services.any? { |klass| !(klass < ::Micro::Service::Base) } new(services) end |
Instance Method Details
#&(arg) ⇒ Object
37 38 39 |
# File 'lib/micro/service/pipeline/reducer.rb', line 37 def &(arg) raise NoMethodError, "undefined method `&' for #{self.inspect}. Please, use the method `>>' to avoid this error." end |
#>>(arg) ⇒ Object
33 34 35 |
# File 'lib/micro/service/pipeline/reducer.rb', line 33 def >>(arg) self.class.build(services + self.class.map_services(arg)) end |
#call(arg = {}) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/micro/service/pipeline/reducer.rb', line 26 def call(arg = {}) @services.reduce(initial_result(arg)) do |result, service| break result if result.failure? service.__new__(result, result.value).call end end |
#to_proc ⇒ Object
41 42 43 |
# File 'lib/micro/service/pipeline/reducer.rb', line 41 def to_proc Proc.new { |arg| call(arg) } end |