Class: HatiOperation::Base
- Inherits:
-
Object
- Object
- HatiOperation::Base
- Includes:
- HatiCommand::Cmd
- Defined in:
- lib/hati_operation/base.rb
Class Method Summary collapse
- .call(*args, **kwargs, &block) ⇒ Object
- .on_failure(command) ⇒ Object
- .on_success(command) ⇒ Object
- .operation_config ⇒ Object
- .params(command, err: nil) ⇒ Object
-
.step(**kwargs) ⇒ Object
TODO: validate type.
Instance Method Summary collapse
- #__step_block_call!(err: nil) ⇒ Object
-
#step(result = nil, err: nil, &block) ⇒ Object
unpack result wraps implicitly.
- #step_configs ⇒ Object
-
#step_exec_stack ⇒ Object
keep track of step macro calls.
Class Method Details
.call(*args, **kwargs, &block) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/hati_operation/base.rb', line 63 def call(*args, **kwargs, &block) reciever = nil injected_params = nil if block_given? reciever = new container = StepConfigContainer.new container.instance_eval(&block) # WIP: work on defaults for DSL reciever.step_configs.merge!(container.configurations) injected_params = reciever.step_configs[:params] end params_modifier = injected_params || operation_config[:params] # TODO: naming if params_modifier unless kwargs[:params] raise 'If operation config :params is set, caller method must have :params keyword argument' end params_rez = params_modifier.call(kwargs[:params]) reciever_configs = reciever&.step_configs || {} params_err = reciever_configs[:params_err] || operation_config[:params_err] if params_rez.failure? # WIP: override or nest ??? params_rez.err = params_err if params_err return params_rez end kwargs[:params] = params_rez.value end result = super(*args, __command_reciever: reciever, **kwargs) # Wrap for implicit rez = result.respond_to?(:success?) ? result : HatiCommand::Success.new(result) # TODO: extract success_wrap = operation_config[:on_success] failure_wrap = operation_config[:on_failure] return success_wrap&.call(rez) if success_wrap && rez.success? return failure_wrap&.call(rez) if failure_wrap && rez.failure? rez end |
.on_failure(command) ⇒ Object
35 36 37 |
# File 'lib/hati_operation/base.rb', line 35 def on_failure(command) operation_config[:on_failure] = command end |
.on_success(command) ⇒ Object
31 32 33 |
# File 'lib/hati_operation/base.rb', line 31 def on_success(command) operation_config[:on_success] = command end |
.operation_config ⇒ Object
22 23 24 |
# File 'lib/hati_operation/base.rb', line 22 def operation_config @operation_config ||= {} end |
.params(command, err: nil) ⇒ Object
26 27 28 29 |
# File 'lib/hati_operation/base.rb', line 26 def params(command, err: nil) operation_config[:params] = command operation_config[:params_err] = err end |
.step(**kwargs) ⇒ Object
TODO: validate type
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/hati_operation/base.rb', line 40 def step(**kwargs) # TODO: add specific error raise 'Invalid Step type. Expected HatiCommand::Cmd' unless included_modules.include?(HatiCommand::Cmd) name, command = kwargs.first if kwargs[:err] error_name = "#{name}_error".to_sym operation_config[error_name] = kwargs[:err] end # WIP: restructure operation_config[name] = command define_method(name) do configs = self.class.operation_config step_exec_stack.append({ step: name, err: configs[error_name], done: false }) step_configs[name] || configs[name] end end |
Instance Method Details
#__step_block_call!(err: nil) ⇒ Object
145 146 147 148 149 |
# File 'lib/hati_operation/base.rb', line 145 def __step_block_call!(err: nil) yield rescue StandardError => e err ? Failure!(e, err: err) : Failure!(e) end |
#step(result = nil, err: nil, &block) ⇒ Object
unpack result wraps implicitly
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/hati_operation/base.rb', line 124 def step(result = nil, err: nil, &block) return __step_block_call!(err: err, &block) if block_given? last_step = step_exec_stack.last err ||= last_step[:err] if last_step if result.is_a?(HatiCommand::Result) Failure!(result, err: err || result.error) if result.failure? step_exec_stack.last[:done] = true if last_step return result.value end Failure!(result, err: err) if err && result.nil? step_exec_stack.last[:done] = true if last_step result end |
#step_configs ⇒ Object
113 114 115 |
# File 'lib/hati_operation/base.rb', line 113 def step_configs @step_configs ||= {} end |
#step_exec_stack ⇒ Object
keep track of step macro calls
118 119 120 |
# File 'lib/hati_operation/base.rb', line 118 def step_exec_stack @step_exec_stack ||= [] end |