Class: Eco::API::Common::Loaders::UseCase

Inherits:
CaseBase show all
Defined in:
lib/eco/api/common/loaders/use_case.rb

Instance Attribute Summary

Attributes included from Language::AuxiliarLogger

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CaseBase

#name, name_only_once!

Methods inherited from Base

<=>, created_at, set_created_at!

Methods included from ClassHelpers

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

Methods included from Language::AuxiliarLogger

#log

Constructor Details

#initialize(usecases) ⇒ UseCase

rubocop:disable Lint/MissingSuper



44
45
46
47
48
49
# File 'lib/eco/api/common/loaders/use_case.rb', line 44

def initialize(usecases) # rubocop:disable Lint/MissingSuper
  msg = "Expected Eco::API::UseCases. Given #{usecases.class}"
  raise msg unless usecases.is_a?(Eco::API::UseCases)

  usecases.define(name, type: type, &method(:main))
end

Class Method Details

.cli(cli_class = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/eco/api/common/loaders/use_case.rb', line 21

def cli(cli_class = nil)
  if cli_class.is_a?(Class)
    msg = "cli_class should inherit from Eco::API::UseCases::Cli"
    raise ArgumentError, msg unless cli_class < Eco::API::UseCases::Cli

    @cli = cli_class
  elsif cli_class.nil?
    return @cli if instance_variable_defined?(:@cli) && !@cli.nil?
    # try to see if it's namespaced after the use case it provisions cli integration
    begin
      try_class = [to_s, 'Cli'].join('::')
      @cli = Kernel.const_get(try_class)
    rescue NameError
      nil
    end
  else
    raise ArgumentError, "Expecting a class. Given: #{cli_class.class} object"
  end
end

.cli!Object



17
18
19
# File 'lib/eco/api/common/loaders/use_case.rb', line 17

def cli!
  cli&.apply!
end

.type(value = nil) ⇒ Symbol

Returns the type of usecase (i.e. :sync, :transform, :import, :other).

Returns:

  • (Symbol)

    the type of usecase (i.e. :sync, :transform, :import, :other)



8
9
10
11
12
13
14
15
# File 'lib/eco/api/common/loaders/use_case.rb', line 8

def type(value = nil)
  unless value
    msg  = "You should specify a type of case "
    msg << "[:sync, :transform, :import, :other] for #{self}"
    return @type || (raise msg)
  end
  @type = value
end

Instance Method Details

#cli_apply!Object



65
66
67
# File 'lib/eco/api/common/loaders/use_case.rb', line 65

def cli_apply!
  self.class.cli!
end

#main(entries, people, session, options, usecase) ⇒ Object

The parameters of this method will depend on the type of usecase.

Parameters:



57
58
59
# File 'lib/eco/api/common/loaders/use_case.rb', line 57

def main(entries, people, session, options, usecase) # rubocop:disable Lint/UnusedMethodArgument
  raise "You should implement this method"
end

#typeObject



61
62
63
# File 'lib/eco/api/common/loaders/use_case.rb', line 61

def type
  self.class.type
end