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



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

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



23
24
25
# File 'lib/stall/shipping/calculator.rb', line 23

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

#vat_rateObject



27
28
29
# File 'lib/stall/shipping/calculator.rb', line 27

def vat_rate
  Stall.config.vat_rate
end