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, FreeShipping, PercentOnLineItem, PercentPerItem, PriceSack, TieredFlatRate, TieredPercent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

display_includes, #initialize_preference_defaults, page, preference

Methods included from Preferences::Preferable

#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



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

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

.descriptionString

A description for this calculator in few words

Returns:

  • (String)

    A description for the calculator



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

def self.description
  model_name.human
end

.register(*_klasses) ⇒ Object



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

def self.register(*_klasses)
end

Instance Method Details

#available?(_object) ⇒ Boolean

Returns:

  • (Boolean)


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

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)
    send(method, computable)
  else
    raise NotImplementedError, "Please implement '#{method}(#{computable_name})' in your calculator: #{calculator_class.name}"
  end
end

#descriptionObject



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

def description
  self.class.description
end

#to_sObject



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

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