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

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

Direct Known Subclasses

SafeReducer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#servicesObject (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

.map_services(arg) ⇒ Object



8
9
10
11
12
# File 'lib/micro/service/pipeline/reducer.rb', line 8

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

Raises:

  • (NoMethodError)


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_procObject



41
42
43
# File 'lib/micro/service/pipeline/reducer.rb', line 41

def to_proc
  Proc.new { |arg| call(arg) }
end