Sha256: 86d441b7eef60bdab343b66cc3529ebe4d46f471115d61a2472d11204c9adedc

Contents?: true

Size: 1.27 KB

Versions: 17

Compression:

Stored size: 1.27 KB

Contents

module Spree
  class Calculator < Spree::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)
        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

17 entries across 17 versions & 1 rubygems

Version Path
solidus_core-2.0.3 app/models/spree/calculator.rb
solidus_core-1.4.2 app/models/spree/calculator.rb
solidus_core-1.3.2 app/models/spree/calculator.rb
solidus_core-2.0.2 app/models/spree/calculator.rb
solidus_core-1.4.1 app/models/spree/calculator.rb
solidus_core-2.0.1 app/models/spree/calculator.rb
solidus_core-2.0.0 app/models/spree/calculator.rb
solidus_core-1.4.0 app/models/spree/calculator.rb
solidus_core-2.0.0.rc1 app/models/spree/calculator.rb
solidus_core-1.4.0.rc1 app/models/spree/calculator.rb
solidus_core-2.0.0.beta1 app/models/spree/calculator.rb
solidus_core-1.4.0.beta1 app/models/spree/calculator.rb
solidus_core-1.3.1 app/models/spree/calculator.rb
solidus_core-1.3.0 app/models/spree/calculator.rb
solidus_core-1.3.0.rc2 app/models/spree/calculator.rb
solidus_core-1.3.0.rc1 app/models/spree/calculator.rb
solidus_core-1.3.0.beta1 app/models/spree/calculator.rb