Class: Stall::Checkout::Wizard

Inherits:
Object
  • Object
show all
Defined in:
lib/stall/checkout/wizard.rb

Defined Under Namespace

Classes: StepUndefinedError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cart) ⇒ Wizard

Returns a new instance of Wizard.



18
19
20
# File 'lib/stall/checkout/wizard.rb', line 18

def initialize(cart)
  @cart = cart
end

Instance Attribute Details

#cartObject (readonly)

Returns the value of attribute cart.



6
7
8
# File 'lib/stall/checkout/wizard.rb', line 6

def cart
  @cart
end

Class Method Details

.from_route_key(key) ⇒ Object



87
88
89
90
91
# File 'lib/stall/checkout/wizard.rb', line 87

def self.from_route_key(key)
  Stall::Utils.try_load_constant(
    [key.underscore.camelize, 'CheckoutWizard'].join
  )
end

.route_keyObject



83
84
85
# File 'lib/stall/checkout/wizard.rb', line 83

def self.route_key
  name.gsub(/CheckoutWizard/, '').underscore.gsub('_', ' ').parameterize
end

.steps(*identifiers) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/stall/checkout/wizard.rb', line 10

def self.steps(*identifiers)
  if identifiers.length > 0
    self._steps = identifiers
  else
    _steps
  end
end

Instance Method Details

#complete?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/stall/checkout/wizard.rb', line 56

def complete?
  step_index && step_index >= steps_count - 1
end

#current_stepObject



37
38
39
# File 'lib/stall/checkout/wizard.rb', line 37

def current_step
  Stall::Checkout::Step.for(current_step_name)
end

#current_step_nameObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/stall/checkout/wizard.rb', line 41

def current_step_name
  if step?(cart.state)
    cart.state
  else
    raise StepUndefinedError,
      "The current carte state #{ cart.state } does not exist " +
      "for the current #{ self.class.name } wizard, which has the " +
      "following steps : #{ steps.join(', ') }."
  end
end

#initialize_current_step(&block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/stall/checkout/wizard.rb', line 22

def initialize_current_step(&block)
  step = current_step.new(cart)

  # This block allows us to let inject controller-bound dependencies
  # into the step just after it's initialized
  block.call(step) if block

  if step.skip?
    validate_current_step!
    initialize_current_step(&block)
  else
    step
  end
end

#move_to_step!(target_step) ⇒ Object



78
79
80
81
# File 'lib/stall/checkout/wizard.rb', line 78

def move_to_step!(target_step)
  cart.state = target_step
  cart.save!
end

#next_step_nameObject



52
53
54
# File 'lib/stall/checkout/wizard.rb', line 52

def next_step_name
  step_name_for(step_index + 1) if step_index && step_index < steps_count
end

#step_complete?(step) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
# File 'lib/stall/checkout/wizard.rb', line 64

def step_complete?(step)
  step_identifier = (Step === step ? step.identifier : step.to_sym)
  steps.index(cart.state) > steps.index(step_identifier)
end

#stepsObject



60
61
62
# File 'lib/stall/checkout/wizard.rb', line 60

def steps
  @steps ||= self.class.steps
end

#steps_countObject



69
70
71
# File 'lib/stall/checkout/wizard.rb', line 69

def steps_count
  @steps_count ||= steps.length
end

#validate_current_step!Object



73
74
75
76
# File 'lib/stall/checkout/wizard.rb', line 73

def validate_current_step!
  cart.state = next_step_name
  cart.save!
end