Class: Spree::Calculator

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

Defined Under Namespace

Modules: Returns, Shipping Classes: DefaultTax, FlatPercentItemTotal, FlatRate, FlexiRate, PercentOnLineItem, PercentPerItem, PriceSack, TieredFlatRate, TieredPercent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

page, spree_base_scopes

Methods included from Preferences::Preferable

#clear_preferences, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

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



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

def self.description
  'Base Calculator'
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



13
14
15
16
17
18
19
20
21
22
23
# File 'app/models/spree/calculator.rb', line 13

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