Class: Pipes::Context

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(values = {}) ⇒ Context

Context constructor.

Parameters:

  • values (Hash) (defaults to: {})


19
20
21
22
# File 'lib/codequest_pipes/context.rb', line 19

def initialize(values = {})
  add(values)
  @error = nil
end

Instance Attribute Details

#errorObject (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.

Parameters:

  • values (Hash)


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.

Returns:

  • (Boolean)

    Failure status.



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

Returns:

  • (Boolean)

    Success status.



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.

Parameters:

  • error (Any)

    Error to be set.

Raises:



41
42
43
44
# File 'lib/codequest_pipes/context.rb', line 41

def terminate(error)
  @error = error
  fail ExecutionTerminated, error
end