Sha256: 1631bdd4ea9142b67d28656c4af2ed2aa9c01fc5c8a8b72832358f00e6b821ff

Contents?: true

Size: 1.28 KB

Versions: 14

Compression:

Stored size: 1.28 KB

Contents

module Spree
  class Calculator < ActiveRecord::Base
    belongs_to :calculable, polymorphic: true

    # 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
    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

    # overwrite to provide description for your calculators
    def self.description
      'Base Calculator'
    end

    ###################################################################

    def self.register(*klasses)
    end

    # Returns all calculators applicable for kind of work
    def self.calculators
      Rails.application.config.spree.calculators
    end

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

    def description
      self.class.description
    end

    def available?(object)
      true
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
spree_core-2.2.14 app/models/spree/calculator.rb
spree_core-2.2.13 app/models/spree/calculator.rb
spree_core-2.2.12 app/models/spree/calculator.rb
spree_core-2.2.11 app/models/spree/calculator.rb
spree_core-2.2.10 app/models/spree/calculator.rb
spree_core-2.2.9 app/models/spree/calculator.rb
spree_core-2.2.8 app/models/spree/calculator.rb
spree_core-2.2.7 app/models/spree/calculator.rb
spree_core-2.2.6 app/models/spree/calculator.rb
spree_core-2.2.5 app/models/spree/calculator.rb
spree_core-2.2.4 app/models/spree/calculator.rb
spree_core-2.2.3 app/models/spree/calculator.rb
spree_core-2.2.2 app/models/spree/calculator.rb
spree_core-2.2.1 app/models/spree/calculator.rb