Class: Stall::Shipping::Calculator
- Inherits:
-
Object
- Object
- Stall::Shipping::Calculator
- Defined in:
- lib/stall/shipping/calculator.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#cart ⇒ Object
readonly
Returns the value of attribute cart.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Class Method Summary collapse
-
.for(shipping_method) ⇒ Object
Fetch a shipping calculator from a shipping method or a shipping method identifier.
-
.register(name) ⇒ Object
Register a calculator from inside the class.
Instance Method Summary collapse
- #available? ⇒ Boolean
- #eot_price ⇒ Object
-
#initialize(cart, config) ⇒ Calculator
constructor
A new instance of Calculator.
- #price ⇒ Object
- #vat_rate ⇒ Object
Constructor Details
#initialize(cart, config) ⇒ Calculator
Returns a new instance of Calculator.
6 7 8 9 |
# File 'lib/stall/shipping/calculator.rb', line 6 def initialize(cart, config) @cart = cart @config = config end |
Instance Attribute Details
#cart ⇒ Object (readonly)
Returns the value of attribute cart.
4 5 6 |
# File 'lib/stall/shipping/calculator.rb', line 4 def cart @cart end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
4 5 6 |
# File 'lib/stall/shipping/calculator.rb', line 4 def config @config end |
Class Method Details
.for(shipping_method) ⇒ Object
Fetch a shipping calculator from a shipping method or a shipping method identifier
44 45 46 47 48 49 50 51 52 |
# File 'lib/stall/shipping/calculator.rb', line 44 def self.for(shipping_method) identifier = case shipping_method when String, Symbol then shipping_method.to_s else shipping_method.identifier end name = Stall::Shipping.calculators[identifier] String === name ? name.constantize : name end |
.register(name) ⇒ Object
Register a calculator from inside the class.
This is useful for registering the calculator direclty in the class body, but is not suited for “auto-loaded” classes because of Rails’ class unloading behavior
37 38 39 |
# File 'lib/stall/shipping/calculator.rb', line 37 def self.register(name) Stall.config.shipping.register_calculator(name, self) end |
Instance Method Details
#available? ⇒ Boolean
11 12 13 14 15 |
# File 'lib/stall/shipping/calculator.rb', line 11 def available? raise NoMethodError, 'Shipping calculators must implement the #available? method ' \ 'to allow filtering available shipping methods' end |
#eot_price ⇒ Object
23 24 25 |
# File 'lib/stall/shipping/calculator.rb', line 23 def eot_price price / (1 + (vat_rate / 100.0)) end |
#price ⇒ Object
17 18 19 20 21 |
# File 'lib/stall/shipping/calculator.rb', line 17 def price raise NoMethodError, 'Shipping calculators must implement the #price method and return ' \ 'the actual shipping price for the given cart' end |