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

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

Instance Attribute Summary

Attributes inherited from CaseBase

#usecase

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

Returns a new instance of UseCase.



40
41
42
43
# File 'lib/eco/api/common/loaders/use_case.rb', line 40

def initialize(usecases)
  raise "Expected Eco::API::UseCases. Given #{usecases.class}" unless usecases.is_a?(Eco::API::UseCases)
  usecases.define(self.name, type: self.type, &self.method(:main))
end

Class Method Details

.cli(cli_class = nil) ⇒ Object



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

def cli(cli_class = nil)
  if cli_class.is_a?(Class)
    raise ArgumentError, "cli_class should inherit from Eco::API::UseCases::Cli" 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 = [self.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



15
16
17
# File 'lib/eco/api/common/loaders/use_case.rb', line 15

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
# File 'lib/eco/api/common/loaders/use_case.rb', line 8

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

Instance Method Details

#cli_apply!Object



59
60
61
# File 'lib/eco/api/common/loaders/use_case.rb', line 59

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:



51
52
53
# File 'lib/eco/api/common/loaders/use_case.rb', line 51

def main(entries, people, session, options, usecase)
  raise "You should implement this method"
end

#typeObject



55
56
57
# File 'lib/eco/api/common/loaders/use_case.rb', line 55

def type
  self.class.type
end