Class: MarketTown::Checkout::Step
- Inherits:
-
Object
- Object
- MarketTown::Checkout::Step
- Defined in:
- lib/market_town/checkout/steps/step.rb
Overview
Extended by all steps provided by MarketTown::Checkout.
## Creating your own Step
If you wish to create your own step then you can extend Step or you can create a class that looks like step.
### Extending Step
“‘ ruby class MyStep < MarketTown::Checkout::Step
steps :a_step,
:a_second_step
protected
def a_step(state)
end
def a_second_step(state)
end
end “‘
### Duck typing Step
“‘ ruby class MyCustomStep
def initialize(dependencies)
end
def process(state)
end
end “‘
## Extending an existing step
You may want to alter the behaviour of a Step. You can do this quite easily by extending the class.
In the example below we’ll replace the behaviour of AddressStep to allow the use of delivery address as billing address instead of the default behaviour that does the opposite.
“‘ ruby class MyAddressStep < MarketTown::Checkout::AddressStep
# Here we override the step order as defined in AddressStep. We
# also replace #use_billing_address_as_delivery_address with
# #use_delivery_address_as_billing_address.
#
steps :validate_delivery_address,
:use_delivery_address_as_billing_address,
:validate_billing_address,
:ensure_delivery,
:store_addresses,
:propose_shipments,
:finish_address_step
protected
def use_delivery_address_as_billing_address(state)
if state[:use_delivery_address] == true
state.merge(billing_address: state[:delivery_address])
end
end
end “‘
Direct Known Subclasses
Instance Attribute Summary collapse
-
#deps ⇒ Object
readonly
Returns the value of attribute deps.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
Class Method Summary collapse
-
.steps(*steps) ⇒ Object
Set steps for a subclass of Step.
Instance Method Summary collapse
-
#initialize(dependencies = Dependencies.new) ⇒ Step
constructor
Setup step meta object.
-
#process(state) ⇒ Object
Process each sub-step that makes up step.
Constructor Details
#initialize(dependencies = Dependencies.new) ⇒ Step
Setup step meta object.
104 105 106 107 |
# File 'lib/market_town/checkout/steps/step.rb', line 104 def initialize(dependencies = Dependencies.new) @meta = { name: name_from_class } @deps = dependencies end |
Instance Attribute Details
#deps ⇒ Object (readonly)
Returns the value of attribute deps.
98 99 100 |
# File 'lib/market_town/checkout/steps/step.rb', line 98 def deps @deps end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
98 99 100 |
# File 'lib/market_town/checkout/steps/step.rb', line 98 def @meta end |
Class Method Details
.steps(*steps) ⇒ Object
Set steps for a subclass of MarketTown::Checkout::Step.
90 91 92 93 94 95 96 |
# File 'lib/market_town/checkout/steps/step.rb', line 90 def self.steps(*steps) if steps.empty? @steps else @steps = steps end end |
Instance Method Details
#process(state) ⇒ Object
Process each sub-step that makes up step.
111 112 113 114 115 |
# File 'lib/market_town/checkout/steps/step.rb', line 111 def process(state) self.class.steps.reduce(state) do |state, step| send(step, state) || state end end |