class Micro::Service::Pipeline::Reducer
Attributes
services[R]
Public Class Methods
build(args)
click to toggle source
# 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)
click to toggle source
# 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
new(services)
click to toggle source
# File lib/micro/service/pipeline/reducer.rb, line 22 def initialize(services) @services = services end
Public Instance Methods
&(arg)
click to toggle source
# 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)
click to toggle source
# File lib/micro/service/pipeline/reducer.rb, line 33 def >>(arg) self.class.build(services + self.class.map_services(arg)) end
call(arg = {})
click to toggle source
# 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()
click to toggle source
# File lib/micro/service/pipeline/reducer.rb, line 41 def to_proc Proc.new { |arg| call(arg) } end
Private Instance Methods
arg_to_call?(arg)
click to toggle source
# File lib/micro/service/pipeline/reducer.rb, line 54 def arg_to_call?(arg) return true if arg.is_a?(Micro::Service::Base) || arg.is_a?(Reducer) return true if arg.is_a?(Class) && (arg < Micro::Service::Base || arg < Micro::Service::Pipeline) return false end
initial_result(arg)
click to toggle source
# File lib/micro/service/pipeline/reducer.rb, line 47 def initial_result(arg) return arg.call if arg_to_call?(arg) return arg if arg.is_a?(Micro::Service::Result) result = Micro::Service::Result.new result.__set__(true, arg, :ok, nil) end