Module: ProcessChain::InstanceMethods

Defined in:
lib/process_chain.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#if_fail { ... } ⇒ Object

Execute passed code block if chain is not success

Yields:

  • code block to be executed on fail


45
46
47
48
# File 'lib/process_chain.rb', line 45

def if_fail
  return self if success?
  yield
end

#if_success { ... } ⇒ Object

Execute passed code block if chain is success

Yields:

  • code block to be executed on success


38
39
40
41
# File 'lib/process_chain.rb', line 38

def if_success
  return self unless success?
  yield if success?
end

#initialize(input: {}, success: true) ⇒ Object

Constructor

Parameters:

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

    input data for process

  • success (true false) (defaults to: true)

    result of latest process

Returns:

  • a new instance of class

Raises:

  • (ArgumentError)

    when input is not a Hash


25
26
27
28
29
# File 'lib/process_chain.rb', line 25

def initialize(input: {}, success: true)
  raise ArgumentError, '"input" should be a Hash' unless input.is_a? Hash
  @success = success
  @results = Hashie::Mash.new input
end

#return_fail(results = nil) ⇒ Object

Returns a new fail instace of chain

Parameters:

  • results (Hash) (defaults to: nil)

    a new input for next process


58
59
60
# File 'lib/process_chain.rb', line 58

def return_fail(results = nil)
  self.class.new input: build_new_result(results), success: false
end

#return_success(results = nil) ⇒ Object

Returns a new success instace of chain

Parameters:

  • results (Hash) (defaults to: nil)

    a new input for next process


52
53
54
# File 'lib/process_chain.rb', line 52

def return_success(results = nil)
  self.class.new input: build_new_result(results)
end

#success?true false

Returns:

  • (true false)

32
33
34
# File 'lib/process_chain.rb', line 32

def success?
  @success
end