class Stall::Shipping::Calculator

Attributes

cart[R]
config[R]

Public Class Methods

for(shipping_method) click to toggle source

Fetch a shipping calculator from a shipping method or a shipping method identifier

# 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
new(cart, config) click to toggle source
# File lib/stall/shipping/calculator.rb, line 6
def initialize(cart, config)
  @cart = cart
  @config = config
end
register(name) click to toggle source

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

# File lib/stall/shipping/calculator.rb, line 49
def self.register(name)
  Stall.config.shipping.register_calculator(name, self)
end

Public Instance Methods

available?() click to toggle source
# 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() click to toggle source
# File lib/stall/shipping/calculator.rb, line 35
def eot_price
  price / (1 + (vat_rate / 100.0)) if price
end
price() click to toggle source
# 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?() click to toggle source

Override this method in the shipping calculators to declare wether a shipping method provides a tracking URL or not.

# File lib/stall/shipping/calculator.rb, line 25
def trackable?
  false
end
tracking_url() click to toggle source
# 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() click to toggle source
# File lib/stall/shipping/calculator.rb, line 39
def vat_rate
  Stall.config.vat_rate
end

Private Instance Methods

address() click to toggle source
# File lib/stall/shipping/calculator.rb, line 70
def address
  cart.shipping_address || cart.billing_address
end