Class: Eco::API::UseCases::UseCase

Inherits:
BaseCase
  • Object
show all
Defined in:
lib/eco/api/usecases/use_case.rb

Direct Known Subclasses

Error::Handler, Policies::Policy, UseCaseChain

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseCase

valid_type?, validate_type

Methods included from Common::ClassHelpers

#class_resolver, #descendants, #descendants?, #inheritable_attrs, #inheritable_class_vars, #inherited, #instance_variable_name, #new_class, #resolve_class, #to_constant

Constructor Details

#initialize(name, type:, root:, &block) ⇒ UseCase

Returns a new instance of UseCase.

Raises:



10
11
12
13
14
15
16
17
18
# File 'lib/eco/api/usecases/use_case.rb', line 10

def initialize(name, type:, root:, &block)
  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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/eco/api/usecases/use_case.rb', line 7

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/eco/api/usecases/use_case.rb', line 8

def options
  @options
end

#times_launchedObject (readonly)

Returns the value of attribute times_launched.



7
8
9
# File 'lib/eco/api/usecases/use_case.rb', line 7

def times_launched
  @times_launched
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/eco/api/usecases/use_case.rb', line 7

def type
  @type
end

Instance Method Details

#chainerObject



20
21
22
23
24
# File 'lib/eco/api/usecases/use_case.rb', line 20

def chainer
  # 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

#launch(io: nil, **kargs) ⇒ Object

Actual launch of the usecase

Parameters:

  • io (Eco::API::UseCases::BaseIO) (defaults to: nil)

    an input/output helper with the necessary parameters.

  • kargs (Hash)

    hash with symbol keys.

Options Hash (**kargs):



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/eco/api/usecases/use_case.rb', line 38

def launch(io: nil, **kargs)
  params = io&.params(keyed: true, all: true) || {}
  kargs  = params.merge(kargs).merge(usecase: self)

  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

#root=(value) ⇒ Object



26
27
28
29
# File 'lib/eco/api/usecases/use_case.rb', line 26

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