Class: OfficeClerk::Post

Inherits:
ShippingMethod
  • Object
show all
Defined in:
lib/office_clerk/post.rb

Constant Summary collapse

DEFAULTS =
{   :weight_table     => '1 2 5 10 20' , 
:price_table      => '2 5 10 15 18' ,
:max_item_weight  => "20" ,
:max_price        => "100" ,
:handling_max     => "20" ,
:handling_fee     => "0" ,
:default_weight   => "1" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Post

Returns a new instance of Post.



12
13
14
15
16
# File 'lib/office_clerk/post.rb', line 12

def initialize data
  super DEFAULTS.merge(data)
  set_members
  check_values!
end

Instance Attribute Details

#default_weightObject (readonly)

Returns the value of attribute default_weight.



17
18
19
# File 'lib/office_clerk/post.rb', line 17

def default_weight
  @default_weight
end

#handling_feeObject (readonly)

Returns the value of attribute handling_fee.



17
18
19
# File 'lib/office_clerk/post.rb', line 17

def handling_fee
  @handling_fee
end

#handling_maxObject (readonly)

Returns the value of attribute handling_max.



17
18
19
# File 'lib/office_clerk/post.rb', line 17

def handling_max
  @handling_max
end

#max_item_weightObject (readonly)

Returns the value of attribute max_item_weight.



17
18
19
# File 'lib/office_clerk/post.rb', line 17

def max_item_weight
  @max_item_weight
end

#max_priceObject (readonly)

Returns the value of attribute max_price.



17
18
19
# File 'lib/office_clerk/post.rb', line 17

def max_price
  @max_price
end

#pricesObject (readonly)

Returns the value of attribute prices.



17
18
19
# File 'lib/office_clerk/post.rb', line 17

def prices
  @prices
end

#weightsObject (readonly)

Returns the value of attribute weights.



17
18
19
# File 'lib/office_clerk/post.rb', line 17

def weights
  @weights
end

Instance Method Details

#available?(basket) ⇒ Boolean

Determine if weight or size goes over bounds.

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
# File 'lib/office_clerk/post.rb', line 20

def available?(basket)
  basket.items.each do |item|
    if( weight = item.product.weight)
      return false if weight > max_item_weight
    end
  end
  true
end

#price_for(basket) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/office_clerk/post.rb', line 29

def price_for(basket)
  total_price = basket.total_price
  return 0.0 if total_price > self.max_price
  total_weight = basket.items.map {|item| item.quantity * (item.product.weight || default_weight) }.reduce(:+) || 0.0
  shipping =  0

  while total_weight > weights.last # In several packets if need be.
    total_weight -= weights.last
    shipping += prices.last
  end

  [shipping, prices[compute_index(total_weight)], calc_handling_fee(total_price)].compact.sum
end