Class: NxtPipeline::Pipeline
- Inherits:
-
Object
- Object
- NxtPipeline::Pipeline
- Defined in:
- lib/nxt_pipeline/pipeline.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Class Method Summary collapse
Instance Method Summary collapse
- #after_execution(&block) ⇒ Object
- #after_step(&block) ⇒ Object
- #around_execution(&block) ⇒ Object
- #around_step(&block) ⇒ Object
- #before_execution(&block) ⇒ Object
- #before_step(&block) ⇒ Object
- #call(acc, &block) ⇒ Object
- #constructor(name, default: false, &constructor) ⇒ Object
- #constructor_resolver(&block) ⇒ Object
- #handle_step_error(error) ⇒ Object
-
#initialize(configuration: nil, resolvers: [], &block) ⇒ Pipeline
constructor
A new instance of Pipeline.
- #on_errors(*errors, halt_on_error: true, &callback) ⇒ Object (also: #on_error)
- #raise_duplicate_default_constructor ⇒ Object
- #set_default_constructor(default_constructor) ⇒ Object
- #step(argument, constructor: nil, **opts, &block) ⇒ Object
-
#steps(steps = []) ⇒ Object
Overwrite reader to also define steps.
-
#steps=(steps = []) ⇒ Object
Allow to force steps with setter.
Constructor Details
#initialize(configuration: nil, resolvers: [], &block) ⇒ Pipeline
Returns a new instance of Pipeline.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/nxt_pipeline/pipeline.rb', line 7 def initialize(configuration: nil, resolvers: [], &block) @steps = [] @error_callbacks = [] @logger = Logger.new @current_step = nil @current_arg = nil @default_constructor_name = nil @constructors = {} @constructor_resolvers = resolvers @result = nil if configuration.present? config = ::NxtPipeline.configuration(configuration) configure(&config) end configure(&block) if block_given? end |
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
28 29 30 |
# File 'lib/nxt_pipeline/pipeline.rb', line 28 def logger @logger end |
#result ⇒ Object
Returns the value of attribute result.
29 30 31 |
# File 'lib/nxt_pipeline/pipeline.rb', line 29 def result @result end |
Class Method Details
.call(acc, configuration: nil, resolvers: [], &block) ⇒ Object
3 4 5 |
# File 'lib/nxt_pipeline/pipeline.rb', line 3 def self.call(acc, configuration: nil, resolvers: [], &block) new(configuration: configuration, resolvers: resolvers, &block).call(acc) end |
Instance Method Details
#after_execution(&block) ⇒ Object
200 201 202 |
# File 'lib/nxt_pipeline/pipeline.rb', line 200 def after_execution(&block) callbacks.register([:after, :execution], block) end |
#after_step(&block) ⇒ Object
188 189 190 |
# File 'lib/nxt_pipeline/pipeline.rb', line 188 def after_step(&block) callbacks.register([:after, :step], block) end |
#around_execution(&block) ⇒ Object
204 205 206 |
# File 'lib/nxt_pipeline/pipeline.rb', line 204 def around_execution(&block) callbacks.register([:around, :execution], block) end |
#around_step(&block) ⇒ Object
192 193 194 |
# File 'lib/nxt_pipeline/pipeline.rb', line 192 def around_step(&block) callbacks.register([:around, :step], block) end |
#before_execution(&block) ⇒ Object
196 197 198 |
# File 'lib/nxt_pipeline/pipeline.rb', line 196 def before_execution(&block) callbacks.register([:before, :execution], block) end |
#before_step(&block) ⇒ Object
184 185 186 |
# File 'lib/nxt_pipeline/pipeline.rb', line 184 def before_step(&block) callbacks.register([:before, :step], block) end |
#call(acc, &block) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/nxt_pipeline/pipeline.rb', line 147 def call(acc, &block) reset configure(&block) if block_given? callbacks.run(:before, :execution, acc) self.result = callbacks.around :execution, acc do steps.inject(acc) do |changes, step| self.result = call_step(step, changes) rescue StandardError => error decorate_error_with_details(error, changes, step, logger) handle_error_of_step(step, error) result end end # callbacks.run(:after, :execution, result) TODO: Better pass result to callback? self.result = callbacks.run(:after, :execution, acc) || result rescue StandardError => error handle_step_error(error) end |
#constructor(name, default: false, &constructor) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/nxt_pipeline/pipeline.rb', line 31 def constructor(name, default: false, &constructor) name = name.to_sym raise StandardError, "Already registered step :#{name}" if constructors[name] constructors[name] = constructor return unless default set_default_constructor(name) end |
#constructor_resolver(&block) ⇒ Object
41 42 43 |
# File 'lib/nxt_pipeline/pipeline.rb', line 41 def constructor_resolver(&block) constructor_resolvers << block end |
#handle_step_error(error) ⇒ Object
169 170 171 172 173 174 175 176 |
# File 'lib/nxt_pipeline/pipeline.rb', line 169 def handle_step_error(error) log_step(current_step) callback = find_error_callback(error) raise unless callback callback.call(error, current_arg, current_step) end |
#on_errors(*errors, halt_on_error: true, &callback) ⇒ Object Also known as: on_error
178 179 180 |
# File 'lib/nxt_pipeline/pipeline.rb', line 178 def on_errors(*errors, halt_on_error: true, &callback) error_callbacks << ErrorCallback.new(errors, halt_on_error, &callback) end |
#raise_duplicate_default_constructor ⇒ Object
50 51 52 |
# File 'lib/nxt_pipeline/pipeline.rb', line 50 def raise_duplicate_default_constructor raise ArgumentError, 'Default step already defined' end |
#set_default_constructor(default_constructor) ⇒ Object
45 46 47 48 |
# File 'lib/nxt_pipeline/pipeline.rb', line 45 def set_default_constructor(default_constructor) raise_duplicate_default_constructor if default_constructor_name.present? self.default_constructor_name = default_constructor end |
#step(argument, constructor: nil, **opts, &block) ⇒ Object
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/nxt_pipeline/pipeline.rb', line 77 def step(argument, constructor: nil, **opts, &block) if constructor.present? and block_given? msg = "Either specify a block or a constructor but not both at the same time!" raise ArgumentError, msg end to_s = if opts[:to_s].present? opts[:to_s] = opts[:to_s].to_s else if argument.is_a?(Proc) || argument.is_a?(Method) steps.count.to_s else argument.to_s end end opts.reverse_merge!(to_s: to_s) if constructor.present? # p.step Service, constructor: ->(step, **changes) { ... } if constructor.respond_to?(:call) resolved_constructor = constructor else # p.step Service, constructor: :service resolved_constructor = constructors.fetch(constructor) { ::NxtPipeline.constructor(constructor) || (raise ArgumentError, "No constructor defined for #{constructor}") } end elsif block_given? # p.step :inline do ... end resolved_constructor = block else # If no constructor was given try to resolve one resolvers = constructor_resolvers.any? ? constructor_resolvers : [] constructor_from_resolvers = resolvers.map do |resolver| resolver.call(argument, **opts) end.find(&:itself) # resolved constructor is a proc if constructor_from_resolvers.is_a?(Proc) resolved_constructor = constructor_from_resolvers elsif constructor_from_resolvers.present? resolved_constructor = constructors[constructor_from_resolvers] else # try to resolve constructor by argument --> #TODO: Is this a good idea? resolved_constructor = constructors[argument] end # if still no constructor resolved unless resolved_constructor.present? if argument.is_a?(NxtPipeline::Pipeline) pipeline_constructor = ->(changes) { argument.call(changes) } resolved_constructor = pipeline_constructor # last chance: default constructor elsif default_constructor.present? resolved_constructor = default_constructor # now we really give up :-( else raise ArgumentError, "Could neither find nor resolve any constructor for #{argument}, #{opts}" end end end register_step(argument, resolved_constructor, callbacks, **opts) end |
#steps(steps = []) ⇒ Object
Overwrite reader to also define steps
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/nxt_pipeline/pipeline.rb', line 55 def steps(steps = []) return @steps unless steps.any? steps.each do |args| arguments = Array(args) if arguments.size == 1 step(arguments.first) elsif arguments.size == 2 step(arguments.first, **arguments.second) else raise ArgumentError, "Either pass a single argument or an argument and options" end end end |
#steps=(steps = []) ⇒ Object
Allow to force steps with setter
71 72 73 74 75 |
# File 'lib/nxt_pipeline/pipeline.rb', line 71 def steps=(steps = []) # reset steps to be zero @steps = [] steps(steps) end |