Class: Micro::Service::Pipeline::Reducer

Inherits:
Object
  • Object
show all
Defined in:
lib/micro/service/pipeline.rb

Constant Summary collapse

INVALID_SERVICES =
'argument must be a collection of `Micro::Service::Base` classes'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(services) ⇒ Reducer

Returns a new instance of Reducer.



26
27
28
# File 'lib/micro/service/pipeline.rb', line 26

def initialize(services)
  @services = services
end

Instance Attribute Details

#servicesObject (readonly)

Returns the value of attribute services.



7
8
9
# File 'lib/micro/service/pipeline.rb', line 7

def services
  @services
end

Class Method Details

.build(args) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
# File 'lib/micro/service/pipeline.rb', line 18

def self.build(args)
  services = Array(args).flat_map { |arg| map_services(arg) }

  raise ArgumentError, INVALID_SERVICES if services.any? { |klass| !(klass < ::Micro::Service::Base) }

  new(services)
end

.map_services(arg) ⇒ Object



12
13
14
15
16
# File 'lib/micro/service/pipeline.rb', line 12

def self.map_services(arg)
  return arg.services if arg.is_a?(Reducer)
  return arg.__pipeline__.services if arg.is_a?(Class) && arg < Micro::Service::Pipeline
  Array(arg)
end

Instance Method Details

#>>(arg) ⇒ Object



37
38
39
# File 'lib/micro/service/pipeline.rb', line 37

def >>(arg)
  Reducer.build(services + self.class.map_services(arg))
end

#call(arg = {}) ⇒ Object



30
31
32
33
34
35
# File 'lib/micro/service/pipeline.rb', line 30

def call(arg={})
  @services.reduce(initial_result(arg)) do |result, service|
    break result if result.failure?
    service.call(result.value)
  end
end