Class: Doable::Step
- Inherits:
-
Object
- Object
- Doable::Step
- Defined in:
- lib/doable/step.rb
Overview
This class contains the actual work to be done
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#success ⇒ Object
readonly
Returns the value of attribute success.
-
#task ⇒ Object
readonly
Returns the value of attribute task.
Instance Method Summary collapse
-
#call(*args) ⇒ Object
Call our step, passing any arguments to the block (task) itself.
-
#handled ⇒ Object
Set the Step’s status to ‘handled’.
-
#initialize(context, options = {}, &block) ⇒ Step
constructor
A new instance of Step.
-
#retry(*args) ⇒ Object
Wrapper around call used for retrying a step.
-
#rollback(&block) ⇒ Boolean
Sets up a block to call when rolling back this step.
-
#rollback! ⇒ Object
Actually rollback this step.
-
#rollbackable? ⇒ Boolean
Query a step to see if it can be rolled back.
-
#skip ⇒ Object
allow skipping steps (needs to be called externally).
-
#successful? ⇒ Boolean
Boolean way of requesting success.
Constructor Details
#initialize(context, options = {}, &block) ⇒ Step
Returns a new instance of Step.
8 9 10 11 12 13 14 15 |
# File 'lib/doable/step.rb', line 8 def initialize(context, = {}, &block) @context = context @success = false @status = :unattempted @task = block @name = .key?(:name) ? [:key] : block.to_s @rollback = nil end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/doable/step.rb', line 4 def name @name end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
4 5 6 |
# File 'lib/doable/step.rb', line 4 def status @status end |
#success ⇒ Object (readonly)
Returns the value of attribute success.
4 5 6 |
# File 'lib/doable/step.rb', line 4 def success @success end |
#task ⇒ Object (readonly)
Returns the value of attribute task.
4 5 6 |
# File 'lib/doable/step.rb', line 4 def task @task end |
Instance Method Details
#call(*args) ⇒ Object
Call our step, passing any arguments to the block (task) itself
18 19 20 21 22 23 24 |
# File 'lib/doable/step.rb', line 18 def call(*args) @status = :failed # first assume our attempt fails @context.instance_exec(*args, &@task) # try to execute the Step's task @success = true # we'll only get here if no exceptions were raised @status = :completed # if we're here, the Step is complete return self end |
#handled ⇒ Object
Set the Step’s status to ‘handled’
27 28 29 30 |
# File 'lib/doable/step.rb', line 27 def handled @status = :handled @success = true # Might want to change this to false... end |
#retry(*args) ⇒ Object
Wrapper around call used for retrying a step. Recommended approach to retrying as this may be enhanced in the future.
40 41 42 |
# File 'lib/doable/step.rb', line 40 def retry(*args) call(*args) end |
#rollback(&block) ⇒ Boolean
Sets up a block to call when rolling back this step
52 53 54 55 |
# File 'lib/doable/step.rb', line 52 def rollback(&block) @rollback = block return true end |
#rollback! ⇒ Object
Actually rollback this step
68 69 70 71 72 |
# File 'lib/doable/step.rb', line 68 def rollback! @context.instance_exec(&@rollback) @status = :rolledback @success = false end |
#rollbackable? ⇒ Boolean
Query a step to see if it can be rolled back
59 60 61 62 63 64 65 |
# File 'lib/doable/step.rb', line 59 def rollbackable? if @rollback and [:completed, :handled, :failed].include?(@status) return true else return false end end |
#skip ⇒ Object
allow skipping steps (needs to be called externally)
45 46 47 48 |
# File 'lib/doable/step.rb', line 45 def skip @status = :skipped @success = false end |
#successful? ⇒ Boolean
Boolean way of requesting success
34 35 36 |
# File 'lib/doable/step.rb', line 34 def successful? return @success end |