Class: ChainIt

Inherits:
Object
  • Object
show all
Defined in:
lib/chain_it.rb,
lib/chain_it/version.rb

Constant Summary collapse

DEFAULT_SETTINGS =
{ auto_exception_handling: false }.freeze
INVALID_RESULT_MSG =
"ChainIt#chain block must return both #value and #failure? aware object.\n Check documentation: https://github.com/spark-solutions/chain_it#usage"
VERSION =
'1.2.0'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) ⇒ ChainIt

Returns a new instance of ChainIt.



5
6
7
# File 'lib/chain_it.rb', line 5

def initialize(settings = {})
  @settings = DEFAULT_SETTINGS.merge(settings)
end

Instance Method Details

#chainObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/chain_it.rb', line 9

def chain
  if @skip_next
    @skip_next = false
    return self
  end

  return self if @skip
  @skip_next = false

  @result = yield result_value
  @skip = true if result_failure?
  self
rescue StandardError => e
  raise e unless @settings[:auto_exception_handling]

  e.define_singleton_method(:value) { e }
  @result = e

  @skip = true
  self
end

#on_error {|result_value| ... } ⇒ Object

Yields:

  • (result_value)


37
38
39
40
41
42
# File 'lib/chain_it.rb', line 37

def on_error
  return self unless @skip
  yield result_value
  @skip = false
  self
end

#resultObject



44
45
46
# File 'lib/chain_it.rb', line 44

def result
  @result
end

#skip_nextObject



31
32
33
34
35
# File 'lib/chain_it.rb', line 31

def skip_next
  return self if @skip
  @skip_next = yield result_value
  self
end