lib/eco/api/usecases/use_case.rb in eco-helpers-0.9.2 vs lib/eco/api/usecases/use_case.rb in eco-helpers-0.9.3

- old
+ new

@@ -1,64 +1,51 @@ module Eco module API - module UseCases - class UseCase - TYPES = [:import, :filter, :transform, :sync, :export] - ALL_PARAMS = [:input, :people, :session, :options] - TYPE_PARAMS = { - import: [:input, :session], - filter: [:people, :session, :options], - transform: [:people, :session], - export: [:people, :session, :options] - } + class UseCases + class UseCase < BaseCase + @types = [:import, :filter, :transform, :sync, :export] - class << self - def valid_type?(type) - TYPES.include?(type) - end - - def type_params(type) - raise "Invalid type '#{type.to_s}'" if !valid_type?(type) - TYPE_PARAMS[type] - end - end - attr_reader :name, :type, :times_launched attr_reader :options def initialize(name, type:, root:, &block) - raise "Undefined usecase type #{type}, when creating '#{name}'. Please, use any of #{TYPES}" unless self.class.valid_type?(type) + raise InvalidType.new("Invalid type for '#{name}'.", type: type, types: self.class.types) unless self.class.valid_type?(type) self.root = root @callback = block @name = name @type = type @times_launched = 0 end def chainer - # TODO: root is a UseGroup that will not point to this new case. + # TODO: root is a Eco::API::UseCases that will not point to this new case. # => Moreover, the name and type will be the same as self Eco::API::UseCases::UseCaseChain.new(usecase: self, root: @root) end def root=(value) - raise "Root should be a UseGroup. Given: #{value}" if !value.is_a?(UseGroup) + raise "Root should be a Eco::API::UseCases. Given: #{value.class}" if !value.is_a?(Eco::API::UseCases) @root = value end - def launch(input: nil, people: nil, session:, options: {}) - @options = options - data = UseCaseIO.new(usecase: self, input: input, people: people, session: session, options: options) + # Actual launch of the usecase + # @param io [Eco::API::UseCases::BaseIO] an input/output helper with the necessary parameters. + # @param kargs [Hash] hash with symbol keys. + # @option kargs [Eco::API::Common::People::Entries, Eco::API::Organization::People] :input the input data of reference. + # @option kargs [Eco::API::Organization::People] :people object. + # @option kargs [Eco::API:Session] :session + # @option kargs [Hash] :options hash with symbol keys (i.e. behaviour modifiers, cli trackers, filters, etc.) + def launch(io: nil, **kargs) + params = io&.params(keyed: true) || {} + kargs = params.merge(kargs).merge(usecase: self) - data.output = @callback.call(data.params) - @times_launched += 1 - - data_model = { - self => { - io: data - } - } + UseCaseIO.new(**kargs).tap do |uio| + @options = uio.options + uio.session.logger.debug("#{self.class}: going to process '#{name}'") + uio.output = @callback.call(*uio.params) + @times_launched += 1 + end end protected def callback