class Pipes::Context
Context
is an object used to pass data between Pipes
. It behaves like an OpenStruct except you can write a value only once - this way we prevent context keys from being overwritten.
Attributes
Public Class Methods
Context
constructor.
@param values [Hash]
# File lib/codequest_pipes/context.rb, line 19 def initialize(values = {}) add(values) @error = nil end
Public Instance Methods
Method `add` allows adding new properties (as a Hash) to the Context
.
@param values [Hash]
# File lib/codequest_pipes/context.rb, line 27 def add(values) values.each do |key, val| k_sym = key.to_sym fail Override, "Property :#{key} already present" if respond_to?(k_sym) define_singleton_method(k_sym) { val } end end
Check if the Context
failed.
@return [Boolean] Failure status.
# File lib/codequest_pipes/context.rb, line 65 def failure? !success? end
Quietly fail the pipe, allowing the error to be saved and accessed from the Context
.
@param error [Any] Error to be set.
# File lib/codequest_pipes/context.rb, line 39 def halt(error = 'Execution stopped') @error = error end
Printable string representation of the context object_id_hex explained: stackoverflow.com/a/2818916/3526316
@return [String]
# File lib/codequest_pipes/context.rb, line 73 def inspect keys = methods - Object.methods - Pipes::Context.instance_methods fields = keys.map { |key| "#{key}=#{public_send(key).inspect}" } fields << "@error=#{@error.inspect}" object_id_hex = '%x' % (object_id << 1) "#<Pipes::Context:0x00#{object_id_hex} #{fields.join(', ')}>" end
Check if the Context
finished successfully. This method smells of :reek:NilCheck
@return [Boolean] Success status.
# File lib/codequest_pipes/context.rb, line 58 def success? @error.nil? end
Explicitly fail the pipe, allowing the error to be saved and accessed from the Context
.
@param error [Any] Error to be set.
@raise [ExecutionTerminated]
# File lib/codequest_pipes/context.rb, line 49 def terminate(error) halt(error) fail ExecutionTerminated, error end