module Eco module API class UseCases # InputOutput class for usecases. # @note Same as Eco::API::UseCases::BaseIO but: # - includes `type` of usecase # - provides a helper to `chain` InputOutput between usecases # @attr_reader usecase [Eco::API::UseCases::UseCase] the usecase this InputOuput is linked to class UseCaseIO < BaseIO @types = BaseIO.types attr_reader :usecase # @param usecase [Eco::API::UseCases::UseCase] the target `usecase` # @param job [Eco::API::Session::Batch::Job] the default `job` of the usecase # @see Eco::API::UseCases::BaseIO#initialize def initialize(usecase:, job: nil, **kargs) self.usecase = usecase @job = job super(**kargs) end # @see Eco::API::UseCases::BaseIO#new # @param usecase [Eco::API::UseCases::UseCase] target usecase # @return [Eco::API::UseCases::UseCaseIO] def new(usecase:, **kargs) default = { usecase: usecase, job: @job } super(**default.merge(kargs)) end # @param value [Eco::API::UseCases::UseCase] the usecase this InputOuput should be linked to def usecase=(value) raise "It should be a Eco::API::UseCases::UseCase. Given: #{value}" if !value.is_a?(Eco::API::UseCases::UseCase) @usecase = value end # @return [Symbol] the `type` of the UseCase this InputOuput is linked to def type @usecase.type end def type=(value) raise "Can't modify type depends on the usecase linked to this IO object" end # @see Eco::API::UseCases::BaseIO#params # Same as its superclass but adding `usecase` parameter def params(keyed: false) super(keyed: keyed).tap do |res| if keyed res.merge!({ usecase: usecase, job: @job }) else res.push(usecase).push(@job) end end end def chain(usecase:) raise "It should be a UseCase. Given: #{usecase}" if !usecase.is_a?(UseCase) aux_io = self.class.new(input: input, people: people, session: session, options: options, usecase: usecase) kargs = aux_io.params(keyed: true) case self.type when :import kargs[:input] = output when :filter kargs[:people] = output when :transform, :sync, :export, :error_handler # no redirections => should it redirect the input? end self.class.new(kargs) end end end end end