Class: Pipes::Context
- Inherits:
-
Object
- Object
- Pipes::Context
- Defined in:
- lib/codequest_pipes/context.rb
Overview
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.
Defined Under Namespace
Classes: ExecutionTerminated, Override
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
Instance Method Summary collapse
-
#add(values) ⇒ Object
Method
addallows adding new properties (as a Hash) to the Context. -
#failure? ⇒ Boolean
Check if the Context failed.
-
#initialize(values = {}) ⇒ Context
constructor
Context constructor.
-
#success? ⇒ Boolean
Check if the Context finished successfully.
-
#terminate(error) ⇒ Object
Explicitly fail the pipe, allowing the error to be saved and accessed from the Context.
Constructor Details
#initialize(values = {}) ⇒ Context
Context constructor.
19 20 21 22 |
# File 'lib/codequest_pipes/context.rb', line 19 def initialize(values = {}) add(values) @error = nil end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
6 7 8 |
# File 'lib/codequest_pipes/context.rb', line 6 def error @error end |
Instance Method Details
#add(values) ⇒ Object
Method add allows adding new properties (as a Hash) to the Context.
27 28 29 30 31 32 33 |
# 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 |
#failure? ⇒ Boolean
Check if the Context failed.
57 58 59 |
# File 'lib/codequest_pipes/context.rb', line 57 def failure? !success? end |
#success? ⇒ Boolean
Check if the Context finished successfully. This method smells of :reek:NilCheck
50 51 52 |
# File 'lib/codequest_pipes/context.rb', line 50 def success? @error.nil? end |
#terminate(error) ⇒ Object
Explicitly fail the pipe, allowing the error to be saved and accessed from the Context.
41 42 43 44 |
# File 'lib/codequest_pipes/context.rb', line 41 def terminate(error) @error = error fail ExecutionTerminated, error end |