Sha256: 7aa187b7c36725177b1b28d8d2e306215cf11ab21c75f57d2f43262d602425c7

Contents?: true

Size: 1.29 KB

Versions: 66

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

require 'spree/preferences/persistable'

module Spree
  class Calculator < Spree::Base
    include Spree::Preferences::Persistable

    belongs_to :calculable, polymorphic: true, optional: 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_name = "compute_#{computable_name}".to_sym
      calculator_class = self.class
      if respond_to?(method_name)
        send(method_name, computable)
      else
        raise NotImplementedError, "Please implement '#{method_name}(#{computable_name})' in your calculator: #{calculator_class.name}"
      end
    end

    # A description for this calculator in few words
    # @return [String] A description for the calculator
    def self.description
      model_name.human
    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

66 entries across 66 versions & 1 rubygems

Version Path
solidus_core-4.2.0 app/models/spree/calculator.rb
solidus_core-4.1.1 app/models/spree/calculator.rb
solidus_core-4.0.2 app/models/spree/calculator.rb
solidus_core-3.4.4 app/models/spree/calculator.rb
solidus_core-3.3.4 app/models/spree/calculator.rb
solidus_core-3.2.9 app/models/spree/calculator.rb
solidus_core-3.2.8 app/models/spree/calculator.rb
solidus_core-3.3.3 app/models/spree/calculator.rb
solidus_core-3.4.3 app/models/spree/calculator.rb
solidus_core-4.0.1 app/models/spree/calculator.rb
solidus_core-4.1.0 app/models/spree/calculator.rb
solidus_core-4.0.0 app/models/spree/calculator.rb
solidus_core-3.4.2 app/models/spree/calculator.rb
solidus_core-3.2.7 app/models/spree/calculator.rb
solidus_core-3.3.2 app/models/spree/calculator.rb
solidus_core-3.4.1 app/models/spree/calculator.rb
solidus_core-3.4.0 app/models/spree/calculator.rb
solidus_core-3.3.1 app/models/spree/calculator.rb
solidus_core-3.2.6 app/models/spree/calculator.rb
solidus_core-3.1.9 app/models/spree/calculator.rb