lib/micro/service/pipeline.rb in u-service-0.6.0 vs lib/micro/service/pipeline.rb in u-service-0.7.0

- old
+ new

@@ -1,40 +1,44 @@ # frozen_string_literal: true module Micro module Service - class Pipeline - INVALID_COLLECTION = - 'argument must be a collection of `Micro::Service::Base` classes'.freeze + module Pipeline + class Reducer + def initialize(services) + @services = services + end - def self.[](*services) - new(services) - end + def call(arg={}) + @services.reduce(initial_result(arg)) do |result, service| + break result if result.failure? + service.call(result.value) + end + end - def initialize(services) - @services = validate!(services) + private + + def initial_result(arg) + return arg if arg.is_a?(Micro::Service::Result) + Micro::Service::Result::Success(value: arg) + end end - def call(arg={}) - @services.reduce(initial_result(arg)) do |result, service| - break result if result.failure? - service.call(result.value) - end + private_constant :Reducer + + INVALID_SERVICES = + 'argument must be a collection of `Micro::Service::Base` classes'.freeze + + def self.[](*args) + self.new(args) end - private + def self.new(args) + services = Array(args) - def validate!(services) - Array(services).tap do |collection| - if collection.any? { |klass| !(klass < ::Micro::Service::Base) } - raise ArgumentError, INVALID_COLLECTION - end - end - end + raise ArgumentError, INVALID_SERVICES if services.any? { |klass| !(klass < ::Micro::Service::Base) } - def initial_result(arg) - return arg if arg.is_a?(Micro::Service::Result) - Micro::Service::Result::Success(value: arg) - end + Reducer.new(services) + end end end end