Class: PriceHubble::Utils::Decision::Runtime
- Inherits:
-
Object
- Object
- PriceHubble::Utils::Decision::Runtime
- Defined in:
- lib/price_hubble/utils/decision.rb
Overview
An inline runtime class to abstract the decision making.
Instance Attribute Summary collapse
-
#bang_proc ⇒ Object
readonly
Generate getters for the runtime settings.
-
#fail_proc ⇒ Object
readonly
Generate getters for the runtime settings.
-
#good_proc ⇒ Object
readonly
Generate getters for the runtime settings.
-
#on_error ⇒ Object
readonly
Generate getters for the runtime settings.
Instance Method Summary collapse
-
#bang(&block) ⇒ Object
Register a new error (bang) way.
-
#error_proc ⇒ Proc
Returns the prefered error method block, based on the
on_error
setting. -
#evaluate { ... } ⇒ Mixed
Evaluate the decision.
-
#fail(&block) ⇒ Object
Register a new error (fail) way.
-
#good(&block) ⇒ Object
Register a new success (good) way.
-
#initialize(on_error: :fail) ⇒ Runtime
constructor
Create a new decision runtime object which collect all the result paths, and then evaluates the decision.
Constructor Details
#initialize(on_error: :fail) ⇒ Runtime
Create a new decision runtime object which collect all the result paths, and then evaluates the decision.
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_proc ⇒ Object (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_proc ⇒ Object (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_proc ⇒ Object (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_error ⇒ Object (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.
58 59 60 |
# File 'lib/price_hubble/utils/decision.rb', line 58 def bang(&block) @bang_proc = block end |
#error_proc ⇒ Proc
Returns the prefered error method block, based on the on_error
setting.
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.
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.
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.
72 73 74 |
# File 'lib/price_hubble/utils/decision.rb', line 72 def good(&block) @good_proc = block end |