Class: Eco::API::UseCases::BaseIO

Inherits:
BaseCase show all
Includes:
Language::AuxiliarLogger, Language::Methods::DslAble
Defined in:
lib/eco/api/usecases/base_io.rb

Overview

Basic class to manage InputOuput for usecases

Direct Known Subclasses

UseCaseIO

Defined Under Namespace

Classes: MissingParameter

Instance Attribute Summary collapse

Attributes included from Language::AuxiliarLogger

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Language::AuxiliarLogger

#log

Methods included from Language::Methods::DslAble

#evaluate, #method_missing, #respond_to_missing?

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(session:, type: nil, input: nil, people: nil, options: {}, output: nil, validate: true) ⇒ BaseIO

Returns a new instance of BaseIO.

Parameters:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/eco/api/usecases/base_io.rb', line 43

def initialize( # rubocop:disable Lint/MissingSuper
  session:,
  type:    nil, input:  nil, people: nil,
  options: {},  output: nil, validate: true
)
  self.type = type if type

  if self.type && validate # rubocop:disable Style/IfUnlessModifier
    validate_args(input: input, people: people, session: session, options: options)
  end

  @output      = output
  @input       = input
  @people      = people
  @session     = session
  @options     = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Eco::Language::Methods::DslAble

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



34
35
36
# File 'lib/eco/api/usecases/base_io.rb', line 34

def input
  @input
end

#optionsObject (readonly)

Returns the value of attribute options.



34
35
36
# File 'lib/eco/api/usecases/base_io.rb', line 34

def options
  @options
end

#outputObject

Returns the value of attribute output.



36
37
38
# File 'lib/eco/api/usecases/base_io.rb', line 36

def output
  @output
end

#peopleObject (readonly)

Returns the value of attribute people.



34
35
36
# File 'lib/eco/api/usecases/base_io.rb', line 34

def people
  @people
end

#sessionObject (readonly)

Returns the value of attribute session.



34
35
36
# File 'lib/eco/api/usecases/base_io.rb', line 34

def session
  @session
end

#typeObject

Returns the value of attribute type.



35
36
37
# File 'lib/eco/api/usecases/base_io.rb', line 35

def type
  @type
end

Class Method Details

.input_required?(type) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/eco/api/usecases/base_io.rb', line 25

def input_required?(type)
  !valid_type?(type) || %i[import sync].include?(type)
end

.people_required?(type) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/eco/api/usecases/base_io.rb', line 29

def people_required?(type)
  !valid_type?(type) || %i[filter transform sync error_handler export].include?(type)
end

Instance Method Details

#baseEco::API::UseCases::BaseIO

Helper to obtain an BaseIO object from any child class.



69
70
71
72
73
74
75
76
# File 'lib/eco/api/usecases/base_io.rb', line 69

def base
  kargs = params(keyed: true).merge({
    type:   type,
    output: output
  }).slice(:type, :input, :people, :session, :options, :output) # :validate <- ?

  Eco::API::UseCases::BaseIO.new(**kargs)
end

#chained(as: type) ⇒ Eco::API::UseCases::BaseIO

Note:

if there isn't output it doesn't do the switch, provided that it preserves the target parameter (otherise it would blank it)

Does the switch from output to result in a new IO object.



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/eco/api/usecases/base_io.rb', line 122

def chained(as: type) # rubocop:disable Naming/MethodParameterName
  base.tap do |io_base|
    next unless io_base.output

    case as
    when :import
      io_base.output_be_input!
    when :filter
      io_base.output_be_people!
      # when :transform, :sync, :export, :error_handler, :other
    end
  end
end

#new(**kargs) ⇒ Eco::API::UseCases::BaseIO



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/eco/api/usecases/base_io.rb', line 80

def new(**kargs)
  default = {
    type:     type,
    input:    input,
    people:   people,
    session:  session,
    options:  options,
    output:   output,
    validate: true
  }
  self.class.new(**default.merge(kargs))
end

#params(keyed: false, all: false) ⇒ Object

Helper to build a Hash of symbol keys or Array with params to do callbacks.



94
95
96
97
98
99
100
101
# File 'lib/eco/api/usecases/base_io.rb', line 94

def params(keyed: false, all: false)
  kargs = {}
  kargs.merge!(input:   input)   if input_required?  || all
  kargs.merge!(people:  people)  if people_required? || all
  kargs.merge!(session: session, options: options)

  keyed ? kargs : kargs.values
end

#process_case(case_name, case_type, **params) ⇒ Eco::API::UseCases::BaseIO

Note:
  1. It swaps output to input or people depending on self.type
  2. does this, before the target case_name launch and **after

Shortcut to run a usecase passing this io as parameter



108
109
110
111
112
113
114
115
# File 'lib/eco/api/usecases/base_io.rb', line 108

def process_case(case_name, case_type, **params)
  session.process_case(
    case_name,
    io:   chained,
    type: case_type,
    **params
  ).chained
end