Class: Checklist

Inherits:
Object
  • Object
show all
Defined in:
lib/checklist.rb,
lib/checklist/ui.rb,
lib/checklist/step.rb,
lib/checklist/version.rb,
lib/checklist/wrapper.rb

Defined Under Namespace

Classes: Step, UI

Constant Summary collapse

VERSION =
"0.1.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Checklist

Returns a new instance of Checklist.



11
12
13
14
15
16
17
18
# File 'lib/checklist.rb', line 11

def initialize(name, options={})
  @steps = []
  @remaining_steps = nil
  @completed_steps = nil
  @current = nil
  @name = name
  @ui = options[:ui] || Checklist::UI.new
end

Instance Attribute Details

#currentObject

Returns the value of attribute current.



9
10
11
# File 'lib/checklist.rb', line 9

def current
  @current
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/checklist.rb', line 9

def name
  @name
end

#stepsObject (readonly)

Returns the value of attribute steps.



9
10
11
# File 'lib/checklist.rb', line 9

def steps
  @steps
end

#uiObject (readonly)

Returns the value of attribute ui.



9
10
11
# File 'lib/checklist.rb', line 9

def ui
  @ui
end

Class Method Details

.checklist(*args) {|cl| ... } ⇒ Object

Yields a new checklist to block and runs it

Yields:

  • (cl)

Raises:

  • (ArgumentError)


3
4
5
6
7
8
# File 'lib/checklist/wrapper.rb', line 3

def self.checklist(*args)
  raise ArgumentError, 'need a block' unless block_given?
  cl = self.new(*args)
  yield cl
  cl.run!
end

.step(challenge, response, description = nil, &block) ⇒ Object

Create a new Step instance

Raises:

  • (ArgumentError)


16
17
18
19
# File 'lib/checklist/step.rb', line 16

def self.step(challenge, response, description=nil, &block)
  raise ArgumentError, 'need a block' unless block_given?
  Step.new(challenge, response, description, block)
end

Instance Method Details

#<<(step) ⇒ Object

appendd a Checklist::Step to the checklist

Raises:

  • (RuntimeError)


21
22
23
24
25
# File 'lib/checklist.rb', line 21

def <<(step)
  raise RuntimeError, 'List is open' if open?
  step.must_be_a(Step)
  steps << step
end

#close!Object

Finish the checklist, print and return outstanding steps

Raises:

  • (RuntimeError)


77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/checklist.rb', line 77

def close!
  raise RuntimeError, 'Checklist is not open' unless open?
  if completed?
    ui.complete(self)
  else
    ui.incomplete(self, remaining_steps)
  end
  rv = remaining_steps
  self.current = 
    self.remaining_steps = 
    self.completed_steps =
    nil
  rv
end

#completedObject

Number of completed steps or nil if list is not open



53
54
55
# File 'lib/checklist.rb', line 53

def completed
  completed_steps and completed_steps.length
end

#completed?Boolean

true if checklist is started and all steps are completed_steps

Returns:

  • (Boolean)


38
39
40
# File 'lib/checklist.rb', line 38

def completed?
  remaining_steps && remaining_steps.empty?
end

#lengthObject

Number of defined steps



43
44
45
# File 'lib/checklist.rb', line 43

def length
  steps.length
end

#open!Object

Raises:

  • (RuntimeError)


57
58
59
60
61
62
63
# File 'lib/checklist.rb', line 57

def open!
  raise RuntimeError, "Checklist is already open" if open?
  ui.header(self)
  self.remaining_steps = steps.clone
  self.completed_steps = []
  self
end

#open?Boolean

true if checklist is started

Returns:

  • (Boolean)


33
34
35
# File 'lib/checklist.rb', line 33

def open?
  !!remaining_steps
end

#remainingObject

Number of remaining steps or nil if list is not open



48
49
50
# File 'lib/checklist.rb', line 48

def remaining
  remaining_steps and remaining_steps.length
end

#run!Object

Run the whole thing



93
94
95
96
97
98
99
# File 'lib/checklist.rb', line 93

def run!
  open!
  step! until completed?
  self
ensure
  close!
end

#step(challenge, response, description = nil, &code) ⇒ Object

create new Checklist::Step and add it to the checklist



28
29
30
# File 'lib/checklist.rb', line 28

def step(challenge, response, description=nil, &code)
  self << Checklist::step(challenge, response, description, &code)
end

#step!Object

Execute one step of the checklist

Raises:

  • (RuntimeError)


66
67
68
69
70
71
72
73
74
# File 'lib/checklist.rb', line 66

def step!
  raise RuntimeError, 'Checklist is not open' unless open?
  raise RuntimeError, 'Checklist is completed' if completed?
  self.current = remaining_steps.first
  ui.start(current)
  current.run!
  ui.finish(current)
  completed_steps << remaining_steps.shift
end