Class: PriceHubble::Utils::Decision::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/price_hubble/utils/decision.rb

Overview

An inline runtime class to abstract the decision making.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(on_error: :fail) ⇒ Runtime

Create a new decision runtime object which collect all the result paths, and then evaluates the decision.

Parameters:

  • on_error (Symbol) (defaults to: :fail)

    the error way



49
50
51
52
53
# File 'lib/price_hubble/utils/decision.rb', line 49

def initialize(on_error: :fail)
  @on_error = on_error
  @bang_proc = -> { StandardError.new }
  @fail_proc = @good_proc = -> {}
end

Instance Attribute Details

#bang_procObject (readonly)

Generate getters for the runtime settings



43
44
45
# File 'lib/price_hubble/utils/decision.rb', line 43

def bang_proc
  @bang_proc
end

#fail_procObject (readonly)

Generate getters for the runtime settings



43
44
45
# File 'lib/price_hubble/utils/decision.rb', line 43

def fail_proc
  @fail_proc
end

#good_procObject (readonly)

Generate getters for the runtime settings



43
44
45
# File 'lib/price_hubble/utils/decision.rb', line 43

def good_proc
  @good_proc
end

#on_errorObject (readonly)

Generate getters for the runtime settings



43
44
45
# File 'lib/price_hubble/utils/decision.rb', line 43

def on_error
  @on_error
end

Instance Method Details

#bang(&block) ⇒ Object

Register a new error (bang) way. Requires a block.

Parameters:

  • block (Proc)

    the block to run in case of errors



58
59
60
# File 'lib/price_hubble/utils/decision.rb', line 58

def bang(&block)
  @bang_proc = block
end

#error_procProc

Returns the prefered error method block, based on the on_error setting.

Returns:

  • (Proc)

    the error block we should use



80
81
82
83
84
# File 'lib/price_hubble/utils/decision.rb', line 80

def error_proc
  return fail_proc if on_error == :fail

  -> { raise bang_proc.call }
end

#evaluate { ... } ⇒ Mixed

Evaluate the decision.

Yields:

  • Runtime to collect the settings and the result

Returns:

  • (Mixed)

    the result of the decision (good|fail|bang) block



90
91
92
93
# File 'lib/price_hubble/utils/decision.rb', line 90

def evaluate
  result = yield(self)
  result ? good_proc.call : error_proc.call
end

#fail(&block) ⇒ Object

Register a new error (fail) way. Requires a block.

Parameters:

  • block (Proc)

    the block to run in case of errors



65
66
67
# File 'lib/price_hubble/utils/decision.rb', line 65

def fail(&block)
  @fail_proc = block
end

#good(&block) ⇒ Object

Register a new success (good) way. Requires a block.

Parameters:

  • block (Proc)

    the block to run in case of success



72
73
74
# File 'lib/price_hubble/utils/decision.rb', line 72

def good(&block)
  @good_proc = block
end