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

Inherits:
BaseCase show all
Includes:
Chainer
Defined in:
lib/eco/api/usecases/use_case.rb,
lib/eco/api/usecases/use_case/chainer.rb

Direct Known Subclasses

Error::Handler, Policies::Policy, UseCaseChain

Defined Under Namespace

Modules: Chainer

Constant Summary

Constants included from BaseCase::Model

BaseCase::Model::MODELS

Constants included from BaseCase::Type

BaseCase::Type::TYPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Chainer

#chainer

Methods included from Language::Klass::InheritableClassVars

#inheritable_attrs, #inheritable_class_vars, #inherited

Methods included from Language::Klass::Naming

#instance_variable_name, #to_constant

Methods included from Language::Klass::Hierarchy

#descendants, #descendants?

Methods included from Language::Klass::Builder

#new_class

Methods included from Language::Klass::Uid

#uid

Methods included from Language::Klass::Resolver

#class_resolver, #resolve_class

Methods included from Language::Klass::Const

#if_const, #redef_without_warning

Constructor Details

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

Returns a new instance of UseCase.



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

def initialize(name, type:, root:, model: nil, &block)
  validate_type!(type, msg: "Invalid type for '#{name}'.")

  model ||= self.class.type_to_default_model(type)
  validate_model!(model, msg: "Invalid model for '#{name}'.")

  super()

  self.root       = root
  @callback       = block
  @name           = name
  @type           = type
  @model          = model
  @times_launched = 0
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#times_launchedObject (readonly)

Returns the value of attribute times_launched.



10
11
12
# File 'lib/eco/api/usecases/use_case.rb', line 10

def times_launched
  @times_launched
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

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

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

Returns:



71
72
73
# File 'lib/eco/api/usecases/use_case.rb', line 71

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

  • :input (Eco::API::Common::People::Entries, Eco::API::Organization::People)

    the input data of reference.

  • :data (Variant)

    object. @option kargs [Eco::API::Organization::People] :people object. @option kargs [Eco::API::Organization::Contractors] :contractors object.

  • :session (Eco::API:Session)
  • :options (Hash)

    hash with symbol keys (i.e. behaviour modifiers, cli trackers, filters, etc.)

Returns:



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

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)


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

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



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

def source_object
  return unless callback_from_loader?

  callback_self
end