Class: Calculator::FlexiRate

Inherits:
Calculator
  • Object
show all
Defined in:
app/models/calculator/flexi_rate.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Calculator

#available?, calculators, #description, #to_s

Class Method Details

.available?(object) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'app/models/calculator/flexi_rate.rb', line 10

def self.available?(object)
  true
end

.descriptionObject



6
7
8
# File 'app/models/calculator/flexi_rate.rb', line 6

def self.description
  I18n.t("flexible_rate")
end

.registerObject



14
15
16
17
# File 'app/models/calculator/flexi_rate.rb', line 14

def self.register
  super
  ShippingMethod.register_calculator(self)
end

Instance Method Details

#compute(object) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/calculator/flexi_rate.rb', line 19

def compute(object)
  sum = 0
  max = self.preferred_max_items
  items_count = object.line_items.map(&:quantity).sum
  items_count.times do |i|
    if (i % max == 0) && (max > 0)
      sum += self.preferred_first_item
    else
      sum += self.preferred_additional_item
    end
  end
  return(sum)
end