Class: Calculator

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/calculator.rb

Defined Under Namespace

Classes: FlatPercentItemTotal, FlatRate, FlexiRate, PerItem, PriceBucket, SalesTax, Vat

Constant Summary collapse

@@calculators =
Set.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.calculatorsObject

Returns all calculators applicable for kind of work If passed nil, will return only general calculators



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

def self.calculators
  @@calculators.to_a
end

.descriptionObject

overwrite to provide description for your calculators



12
13
14
# File 'app/models/calculator.rb', line 12

def self.description
  "Base Caclulator"
end

.register(*klasses) ⇒ Object

Registers calculator to be used with selected kinds of operations



20
21
22
23
24
25
26
27
# File 'app/models/calculator.rb', line 20

def self.register(*klasses)
  @@calculators.add(self)
  klasses.each do |klass|
    klass = klass.constantize if klass.is_a?(String)
    klass.register_calculator(self)
  end
  self
end

Instance Method Details

#available?(object) ⇒ Boolean

Returns:

  • (Boolean)


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

def available?(object)
  return true #should be overridden if needed
end

#compute(something = nil) ⇒ Object

This method must be overriden in concrete calculator.

It should return amount computed based on #calculable and/or optional parameter

Raises:

  • (NotImplementedError)


7
8
9
# File 'app/models/calculator.rb', line 7

def compute(something=nil)
  raise(NotImplementedError, "please use concrete calculator")
end

#descriptionObject



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

def description
  self.class.description
end

#to_sObject



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

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