Class: Doable::Step

Inherits:
Object
  • Object
show all
Defined in:
lib/doable/step.rb

Overview

This class contains the actual work to be done

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, options = {}, &block) ⇒ Step

Returns a new instance of Step.

Parameters:

  • options (Hash) (defaults to: {})
  • block (Proc)


8
9
10
11
12
13
14
15
# File 'lib/doable/step.rb', line 8

def initialize(context, options = {}, &block)
  @context  = context
  @success  = false
  @status   = :unattempted
  @task     = block
  @name     = options.key?(:name) ? options[:key] : block.to_s
  @rollback = nil
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/doable/step.rb', line 4

def name
  @name
end

#statusObject (readonly)

Returns the value of attribute status.



4
5
6
# File 'lib/doable/step.rb', line 4

def status
  @status
end

#successObject (readonly)

Returns the value of attribute success.



4
5
6
# File 'lib/doable/step.rb', line 4

def success
  @success
end

#taskObject (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

#handledObject

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

#skipObject

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

Returns:

  • (Boolean)


34
35
36
# File 'lib/doable/step.rb', line 34

def successful?
  return @success
end