Sha256: 18449bdb5ca7c6f105ef8112f603ca03c8219c2146a61b5cdc209cb3096668d6

Contents?: true

Size: 1.65 KB

Versions: 2

Compression:

Stored size: 1.65 KB

Contents

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]
        }

        attr_reader :name, :type, :times_launched

        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

        def initialize(name, type:, root:, options: {}, &block)
          raise "Undefine usecase type #{type}. Please, use any of #{TYPES}" unless self.class.valid_type?(type)

          self.root = root
          @case     = block
          @name     = name
          @type     = type
          @options  = options
          @times_launched = 0
        end

        def root=(value)
          raise "Root should be a UseGroup. Given: #{value}" if !value.is_a?(UseGroup)
          @root = value
        end

        def launch(input: nil, people: nil, session:, options: {})
          data = UseCaseIO.new(usecase: self, input: input, people: people, session: session, options: options)

          data.output = @case.call(data.params)
          @times_launched += 1

          data_model = {
            self => {
              io: data
            }
          }
        end

      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
eco-helpers-0.7.2 lib/eco/api/usecases/use_case.rb
eco-helpers-0.7.1 lib/eco/api/usecases/use_case.rb