Class: WizardSteps::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Concern
Defined in:
lib/wizard_steps/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store, current_key, context: {}) ⇒ Base

Returns a new instance of Base.

Raises:



37
38
39
40
41
42
43
# File 'lib/wizard_steps/base.rb', line 37

def initialize(store, current_key, context: {})
  raise(UnknownStep) unless step_keys.include?(current_key)

  @store = store
  @context = context
  @current_key = current_key
end

Instance Attribute Details

#current_keyObject (readonly)

Returns the value of attribute current_key.



35
36
37
# File 'lib/wizard_steps/base.rb', line 35

def current_key
  @current_key
end

Class Method Details

.first_keyObject



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

def first_key
  step_keys.first
end

.indexed_stepsObject



12
13
14
# File 'lib/wizard_steps/base.rb', line 12

def indexed_steps
  @indexed_steps ||= steps.index_by(&:key)
end

.key_index(key) ⇒ Object



20
21
22
# File 'lib/wizard_steps/base.rb', line 20

def key_index(key)
  steps.index step(key)
end

.step(key) ⇒ Object



16
17
18
# File 'lib/wizard_steps/base.rb', line 16

def step(key)
  indexed_steps[key] || raise(UnknownStep)
end

.step_keysObject



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

def step_keys
  indexed_steps.keys
end

Instance Method Details

#complete!Object



81
82
83
84
85
86
87
88
# File 'lib/wizard_steps/base.rb', line 81

def complete!
  return unless complete?

  do_complete.tap do |result|
    @store.purge!
    yield(result) if block_given?
  end
end

#complete?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/wizard_steps/base.rb', line 77

def complete?
  last_step? && valid?
end

#earlier_keys(key = current_key) ⇒ Object



102
103
104
105
106
107
# File 'lib/wizard_steps/base.rb', line 102

def earlier_keys(key = current_key)
  index = key_index(key)
  return [] unless index.positive?

  steps[0..(index - 1)].map(&:key)
end

#export_dataObject



109
110
111
# File 'lib/wizard_steps/base.rb', line 109

def export_data
  all_steps.map(&:export).reduce({}, :merge)
end

#find(key) ⇒ Object



45
46
47
# File 'lib/wizard_steps/base.rb', line 45

def find(key)
  step(key).new self, @store
end

#find_current_stepObject



49
50
51
# File 'lib/wizard_steps/base.rb', line 49

def find_current_step
  find current_key
end

#first_invalid_stepObject



94
95
96
# File 'lib/wizard_steps/base.rb', line 94

def first_invalid_step
  active_steps.find(&:invalid?)
end

#first_step?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/wizard_steps/base.rb', line 65

def first_step?
  previous_key.nil?
end

#invalid_stepsObject



90
91
92
# File 'lib/wizard_steps/base.rb', line 90

def invalid_steps
  active_steps.select(&:invalid?)
end

#last_step?Boolean

Returns:

  • (Boolean)


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

def last_step?
  next_key.nil?
end

#later_keys(key = current_key) ⇒ Object



98
99
100
# File 'lib/wizard_steps/base.rb', line 98

def later_keys(key = current_key)
  steps[(key_index(key) + 1)..].to_a.map(&:key)
end

#next_key(key = current_key) ⇒ Object



59
60
61
62
63
# File 'lib/wizard_steps/base.rb', line 59

def next_key(key = current_key)
  later_keys(key).find do |k|
    !find(k).skipped?
  end
end

#previous_key(key = current_key) ⇒ Object



53
54
55
56
57
# File 'lib/wizard_steps/base.rb', line 53

def previous_key(key = current_key)
  earlier_keys(key).reverse.find do |k|
    !find(k).skipped?
  end
end

#reviewable_answers_by_stepObject



113
114
115
116
117
# File 'lib/wizard_steps/base.rb', line 113

def reviewable_answers_by_step
  all_steps.reject(&:skipped?).each_with_object({}) do |step, hash|
    hash[step.class] = step.reviewable_answers
  end
end

#valid?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/wizard_steps/base.rb', line 73

def valid?
  active_steps.all?(&:valid?)
end