Class: Spree::Calculator

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/spree/calculator.rb,
app/models/spree/calculator/percent_on_line_item.rb

Defined Under Namespace

Modules: Shipping Classes: DefaultTax, FlatPercentItemTotal, FlatRate, FlexiRate, FreeShipping, PercentOnLineItem, PercentPerItem, PriceSack

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.calculatorsObject

Returns all calculators applicable for kind of work



31
32
33
# File 'app/models/spree/calculator.rb', line 31

def self.calculators
  Rails.application.config.spree.calculators
end

.descriptionObject

overwrite to provide description for your calculators



21
22
23
# File 'app/models/spree/calculator.rb', line 21

def self.description
  'Base Calculator'
end

.register(*klasses) ⇒ Object



27
28
# File 'app/models/spree/calculator.rb', line 27

def self.register(*klasses)
end

Instance Method Details

#available?(object) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'app/models/spree/calculator.rb', line 43

def available?(object)
  true
end

#compute(computable) ⇒ Object

This method calls a compute_<computable> method. must be overriden in concrete calculator.

It should return amount computed based on #calculable and the computable parameter



8
9
10
11
12
13
14
15
16
17
18
# File 'app/models/spree/calculator.rb', line 8

def compute(computable)
  # Spree::LineItem -> :compute_line_item
  computable_name = computable.class.name.demodulize.underscore
  method = "compute_#{computable_name}".to_sym
  calculator_class = self.class
  if respond_to?(method)
    self.send(method, computable)
  else
    raise NotImplementedError, "Please implement '#{method}(#{computable_name})' in your calculator: #{calculator_class.name}"
  end
end

#descriptionObject



39
40
41
# File 'app/models/spree/calculator.rb', line 39

def description
  self.class.description
end

#to_sObject



35
36
37
# File 'app/models/spree/calculator.rb', line 35

def to_s
  self.class.name.titleize.gsub("Calculator\/", "")
end