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

Inherits:
BaseCase 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.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/eco/api/usecases/use_case.rb', line 10

def initialize(name, type:, root:, &block)
  invalid_type_err = InvalidType.new(
    "Invalid type for '#{name}'.",
    type:  type,
    types: self.class.types
  )
  raise invalid_type_err unless self.class.valid_type?(type)

  super()

  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



33
34
35
36
37
# File 'lib/eco/api/usecases/use_case.rb', line 33

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

#classed_definitionEco::API::Common::Loaders::Base, NilClass

When it was defined from a Loader class it retrieves the object.

Returns:



74
75
76
# File 'lib/eco/api/usecases/use_case.rb', line 74

def classed_definition
  callback_self if callback_from_loader?
end

#launch(io: nil, **kargs) ⇒ Eco::API::UseCases::UseCaseIO

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):

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/eco/api/usecases/use_case.rb', line 55

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.log(:debug) {
      "#{self.class}: going to process '#{name}'"
    }

    set_session_n_options(uio) if callback_from_loader?

    uio.output       = callback.call(*uio.params)
    @times_launched += 1
  end
end

#root=(value) ⇒ Object

Raises:

  • (ArgumentError)


39
40
41
42
43
44
# File 'lib/eco/api/usecases/use_case.rb', line 39

def root=(value)
  msg = "Root should be a Eco::API::UseCases. Given: #{value.class}"
  raise ArgumentError, msg unless value.is_a?(Eco::API::UseCases)

  @root = value
end

#source_objectObject



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

def source_object
  return nil unless callback_from_loader?

  callback_self
end