Class: UseCases::StepAdapters::Abstract

Inherits:
Object
  • Object
show all
Includes:
Dry::Monads
Defined in:
lib/use_cases/step_adapters/abstract.rb

Direct Known Subclasses

Check, Map, Step, Tee, Try

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *args) ⇒ Abstract

Returns a new instance of Abstract.



13
14
15
16
# File 'lib/use_cases/step_adapters/abstract.rb', line 13

def initialize(name, *args)
  @name = name
  @object, @options = args
end

Instance Attribute Details

#failureObject (readonly)

Returns the value of attribute failure.



11
12
13
# File 'lib/use_cases/step_adapters/abstract.rb', line 11

def failure
  @failure
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/use_cases/step_adapters/abstract.rb', line 11

def name
  @name
end

#objectObject (readonly)

Returns the value of attribute object.



11
12
13
# File 'lib/use_cases/step_adapters/abstract.rb', line 11

def object
  @object
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/use_cases/step_adapters/abstract.rb', line 11

def options
  @options
end

Instance Method Details

#bind(object) ⇒ Object



31
32
33
# File 'lib/use_cases/step_adapters/abstract.rb', line 31

def bind(object)
  self.class.new(name, object, options)
end

#callObject



22
23
24
# File 'lib/use_cases/step_adapters/abstract.rb', line 22

def call(...)
  UseCases::Result.new(self, do_call(...))
end

#callable_args(args) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/use_cases/step_adapters/abstract.rb', line 35

def callable_args(args)
  if callable_args_count > args.count
    raise StepArgumentError,
          "##{step.name} expects #{expected_args_count} arguments it only received #{step_args.count}, make sure your previous step Success() statement has a payload."
  end
  return args.first(callable_args_count) unless external? && selects_external_args?

  hashed_args(args).values
end

#callable_args_countObject



93
94
95
# File 'lib/use_cases/step_adapters/abstract.rb', line 93

def callable_args_count
  callable_proc.parameters.count
end

#callable_methodObject



66
67
68
69
70
71
# File 'lib/use_cases/step_adapters/abstract.rb', line 66

def callable_method
  case with_option
  when NilClass, FalseClass then name
  else                           :call
  end
end

#callable_objectObject



57
58
59
60
61
62
63
64
# File 'lib/use_cases/step_adapters/abstract.rb', line 57

def callable_object
  case with_option
  when NilClass, FalseClass then object
  when Symbol               then object.send(with_option)
  when String               then UseCases.config.container[with_option]
  else                           with_option
  end
end

#callable_procObject



53
54
55
# File 'lib/use_cases/step_adapters/abstract.rb', line 53

def callable_proc
  callable_object.method(callable_method).to_proc
end

#do_call(*initial_args) ⇒ Object



26
27
28
29
# File 'lib/use_cases/step_adapters/abstract.rb', line 26

def do_call(*initial_args)
  args = callable_args(initial_args)
  callable_proc.call(*args)
end

#external?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/use_cases/step_adapters/abstract.rb', line 85

def external?
  !with_option.nil?
end

#hashed_args(prev_result, params, current_user) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/use_cases/step_adapters/abstract.rb', line 45

def hashed_args((prev_result, params, current_user))
  {
    previous_step_result: prev_result,
    params: params,
    current_user: current_user
  }.slice(*pass_option)
end

#missing?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/use_cases/step_adapters/abstract.rb', line 97

def missing?
  !callable_object.respond_to?(callable_method, true)
end

#pass_optionObject



77
78
79
# File 'lib/use_cases/step_adapters/abstract.rb', line 77

def pass_option
  options[:pass]
end

#previous_input_param_nameObject



81
82
83
# File 'lib/use_cases/step_adapters/abstract.rb', line 81

def previous_input_param_name
  options[:merge_input_as] || :input
end

#previous_step_resultObject



18
19
20
# File 'lib/use_cases/step_adapters/abstract.rb', line 18

def previous_step_result
  object.stack.prev_step_result
end

#selects_external_args?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/use_cases/step_adapters/abstract.rb', line 89

def selects_external_args?
  !pass_option.nil?
end

#with_optionObject



73
74
75
# File 'lib/use_cases/step_adapters/abstract.rb', line 73

def with_option
  options[:with]
end