Class: Stall::Shipping::Calculator

Inherits:
Object
  • Object
show all
Defined in:
lib/stall/shipping/calculator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#cartObject (readonly)

Returns the value of attribute cart.



4
5
6
# File 'lib/stall/shipping/calculator.rb', line 4

def cart
  @cart
end

#configObject (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

Returns:

  • (Boolean)

Raises:

  • (NoMethodError)


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_priceObject



35
36
37
# File 'lib/stall/shipping/calculator.rb', line 35

def eot_price
  price / (1 + (vat_rate / 100.0))
end

#priceObject

Raises:

  • (NoMethodError)


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.

Returns:

  • (Boolean)


25
26
27
# File 'lib/stall/shipping/calculator.rb', line 25

def trackable?
  false
end

#tracking_urlObject

Raises:

  • (NoMethodError)


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_rateObject



39
40
41
# File 'lib/stall/shipping/calculator.rb', line 39

def vat_rate
  Stall.config.vat_rate
end