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
-
#trackable? ⇒ Boolean
Override this method in the shipping calculators to declare wether a shipping method provides a tracking URL or not.
- #tracking_url ⇒ 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
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/stall/shipping/calculator.rb', line 56 def self.for(shipping_method) identifier = case shipping_method when String, Symbol then shipping_method.to_s else shipping_method && shipping_method.identifier end return unless identifier 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
49 50 51 |
# File 'lib/stall/shipping/calculator.rb', line 49 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
35 36 37 |
# File 'lib/stall/shipping/calculator.rb', line 35 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 |
#trackable? ⇒ Boolean
Override this method in the shipping calculators to declare wether a shipping method provides a tracking URL or not.
25 26 27 |
# File 'lib/stall/shipping/calculator.rb', line 25 def trackable? false end |
#tracking_url ⇒ Object
29 30 31 32 33 |
# File 'lib/stall/shipping/calculator.rb', line 29 def tracking_url raise NoMethodError, 'Trackable shipping calculators should override the #tracking_url ' \ 'method and return a tracking URL for the associated shipment.' end |
#vat_rate ⇒ Object
39 40 41 |
# File 'lib/stall/shipping/calculator.rb', line 39 def vat_rate Stall.config.vat_rate end |