Module: Methodical::DSL

Defined in:
lib/methodical/dsl.rb

Instance Method Summary collapse

Instance Method Details

#action(title, &block) ⇒ Object



6
7
8
# File 'lib/methodical/dsl.rb', line 6

def action(title, &block)
  SimpleActionItem.new(title, &block)
end

#filter(&block) ⇒ Object

Filter and optionally modify step disposition



69
70
71
72
73
# File 'lib/methodical/dsl.rb', line 69

def filter(&block)
  Modifier.new("Filter disposition") do |action_item, baton|
    block.call(action_item.execute!(baton))
  end
end

#handle_error(error_type, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/methodical/dsl.rb', line 42

def handle_error(error_type, &block)
  Modifier.new("Handle error #{error_type}") do |action_item, baton|
    begin
      action_item.execute!(baton, true)
    rescue error_type => error
      block.call(baton, action_item, error)
    end
  end
end

#ignoreObject



61
62
63
64
65
66
# File 'lib/methodical/dsl.rb', line 61

def ignore
  Modifier.new("Ignore failures") do |action_item, baton|
    action_item.ignored=true
    action_item.call(baton, action_item)
  end
end

#recover_failureObject



52
53
54
55
56
57
58
59
# File 'lib/methodical/dsl.rb', line 52

def recover_failure
  Modifier.new("Recover from failure") do |action_item, baton|
    disposition = action_item.execute!(baton)
    if disposition.status == :failed
      yield(baton, action_item, disposition)
    end
  end
end

#requisiteObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/methodical/dsl.rb', line 21

def requisite
  Modifier.new("Requisite") do |action_item, baton|
    disposition = action_item.execute!(baton)
    if(disposition.failed?)
      disposition.merge(:status => :abort)
    else
      disposition
    end
  end
end

#retry_on_failure(times_to_retry = 1, time_limit_in_seconds = :none, options = {}) ⇒ Object

TODO Factor this out into its own class, it’s a bit big TODO we may want to roll this functionality into the core Checklist. It would be nice if a retried action would actually show up in the log, e.g.:

  1. Do some work (Failed; Retrying)

  2. Do some work (Succeeded)



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/methodical/dsl.rb', line 80

def retry_on_failure(
    times_to_retry=1, time_limit_in_seconds=:none, options={})
  max_tries = times_to_retry.to_i + 1
  clock = options.fetch(:clock) { Time }
  cutoff_time = if time_limit_in_seconds == :none 
                  :none
                else
                  clock.now + time_limit_in_seconds
                end
  description = 
    "Retry #{times_to_retry} times"
  unless time_limit_in_seconds == :none
    description << " or #{time_limit_in_seconds} seconds"
  end
  Modifier.new(description) do 
    |action_item, baton|
    tries = 0
    begin
      disposition = action_item.execute!(baton)
      tries += 1
      if disposition.failed? && 
          (cutoff_time != :none) && 
          (clock.now >= cutoff_time)
        break
      end
    end until disposition.succeeded? || tries >= max_tries
    disposition
  end
end

#skip_if(reason, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/methodical/dsl.rb', line 32

def skip_if(reason, &block)
  Modifier.new("Skip if #{reason}") do |action_item, baton|
    if block.call(baton, action_item)
      action_item.skip!(reason)
    else
      action_item.call(baton, action_item)
    end
  end
end

#sufficientObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/methodical/dsl.rb', line 10

def sufficient
  Modifier.new("Sufficient") do |action_item, baton|
    disposition = action_item.execute!(baton)
    if(disposition.status == :succeeded)
      disposition.merge(:status => :sufficient)
    else
      disposition
    end
  end
end