-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
calculator.rb
49 lines (40 loc) · 1.44 KB
/
calculator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# frozen_string_literal: true
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
# A description for this calculator in few words
# @return [String] A description for the calculator
def self.description
model_name.human
end
###################################################################
# Returns all calculators applicable for kind of work
def self.calculators
Spree::Deprecation.warn("Calling .calculators is deprecated. Please access through Rails.application.config.spree.calculators")
Spree::Config.environment.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