lib/micro/service/pipeline.rb in u-service-0.11.0 vs lib/micro/service/pipeline.rb in u-service-0.12.0

- old
+ new

@@ -4,23 +4,24 @@ module Service module Pipeline class Reducer attr_reader :services - INVALID_SERVICES = - 'argument must be a collection of `Micro::Service::Base` classes'.freeze + InvalidServices = ArgumentError.new('argument must be a collection of `Micro::Service::Base` classes'.freeze) + private_constant :InvalidServices + 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 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) } + raise InvalidServices if services.any? { |klass| !(klass < ::Micro::Service::Base) } new(services) end def initialize(services) @@ -28,11 +29,11 @@ end def call(arg={}) @services.reduce(initial_result(arg)) do |result, service| break result if result.failure? - service.call(result.value) + service.__new__(result, result.value).call end end def >>(arg) Reducer.build(services + self.class.map_services(arg)) @@ -41,12 +42,12 @@ private def initial_result(arg) return arg.call if arg_to_call?(arg) return arg if arg.is_a?(Micro::Service::Result) - - Micro::Service::Result::Success[value: arg] + result = Micro::Service::Result.new + result.__set__(true, arg, nil) end 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) @@ -72,20 +73,20 @@ def self.[](*args) Reducer.build(args) end - UNDEFINED_PIPELINE = "This class hasn't declared its pipeline. Please, use the `pipeline()` macro to define one.".freeze + UndefinedPipeline = ArgumentError.new("This class hasn't declared its pipeline. Please, use the `pipeline()` macro to define one.".freeze) - private_constant :UNDEFINED_PIPELINE + private_constant :UndefinedPipeline def self.included(base) base.extend(ClassMethods) base.class_eval(<<-RUBY) def initialize(options) @options = options pipeline = self.class.__pipeline__ - raise ArgumentError, UNDEFINED_PIPELINE unless pipeline + raise UndefinedPipeline unless pipeline end RUBY end def call