Module: HatTrick::WizardSteps

Includes:
Enumerable
Included in:
Wizard, WizardDefinition
Defined in:
lib/hat_trick/wizard_steps.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#stepsObject (readonly) Also known as: to_ary

Returns the value of attribute steps.



6
7
8
# File 'lib/hat_trick/wizard_steps.rb', line 6

def steps
  @steps
end

Instance Method Details

#add_step(step, args = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hat_trick/wizard_steps.rb', line 52

def add_step(step, args={})
  if step.is_a?(HatTrick::Step) || step.is_a?(HatTrick::StepDefinition)
    new_step = step
  else
    step_args = args.merge(:name => step, :wizard => self)
    new_step = HatTrick::StepDefinition.new(step_args)
  end

  if steps.count == 0
    new_step.first = true
  end

  steps << new_step

  new_step
end

#delete_step(_step) ⇒ Object



69
70
71
72
# File 'lib/hat_trick/wizard_steps.rb', line 69

def delete_step(_step)
  step = find_step(_step)
  steps.delete(step)
end

#find_step(step) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/hat_trick/wizard_steps.rb', line 15

def find_step(step)
  return nil if step.nil?
  if step.is_a?(HatTrick::Step) || step.is_a?(HatTrick::StepDefinition)
    find { |s| s.equal? step }
  else
    find { |s| s.name == step.to_sym }
  end
end

#move_step(step, index) ⇒ Object

Raises:

  • (ArgumentError)


95
96
97
98
99
100
101
102
103
104
105
# File 'lib/hat_trick/wizard_steps.rb', line 95

def move_step(step, index)
  raise ArgumentError, "#{step} isn't in this wizard" unless steps.include?(step)
  current_index = steps.index(step)
  unless index < current_index
    raise ArgumentError, "#{step} has index #{current_index}; must be >= #{index}"
  end

  while steps.index(step) > index
    steps.delete_at(index)
  end
end

#replace_step(_old_step, _new_step) ⇒ Object

Raises:

  • (ArgumentError)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/hat_trick/wizard_steps.rb', line 74

def replace_step(_old_step, _new_step)
  old_step = find_step(_old_step)
  old_index = steps.index(old_step)
  raise ArgumentError, "Couldn't find step #{_old_step}" unless old_step
  new_step_in_wizard = find_step(_new_step)
  if new_step_in_wizard
    # new_step is already in the wizard
    return move_step(new_step_in_wizard, old_index)
  end

  if _new_step.is_a?(HatTrick::Step) || _new_step.is_a?(HatTrick::StepDefinition)
    new_step = _new_step
  else
    new_step = HatTrick::StepDefinition.new(:name => _new_step)
  end

  steps.delete_at(old_index)
  steps.insert(old_index, new_step)
  new_step
end

#step_after(_step) ⇒ Object



24
25
26
# File 'lib/hat_trick/wizard_steps.rb', line 24

def step_after(_step)
  steps_after(_step).first
end

#step_before(_step) ⇒ Object



39
40
41
# File 'lib/hat_trick/wizard_steps.rb', line 39

def step_before(_step)
  steps_before(_step).last
end

#steps_after(_step) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/hat_trick/wizard_steps.rb', line 28

def steps_after(_step)
  step = find_step(_step)
  return [] unless step
  return [] if step.last? && !step.skipped?
  step_index = steps.index(step)
  max_index = steps.count - 1
  return [] if step_index >= max_index
  after_index = step_index + 1
  steps[after_index .. -1]
end

#steps_before(_step) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/hat_trick/wizard_steps.rb', line 43

def steps_before(_step)
  step = find_step(_step)
  return [] unless step
  step_index = steps.index(step)
  return [] if step_index <= 0
  before_index = step_index - 1
  steps[0 .. before_index]
end