Class: Quantity::Calculator

Inherits:
Object
  • Object
show all
Includes:
Quantifiable
Defined in:
lib/unitmanager/calculator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Quantifiable

#quantifiable?

Constructor Details

#initialize(units = {}, conversions = []) ⇒ Calculator

Returns a new instance of Calculator.



8
9
10
11
12
# File 'lib/unitmanager/calculator.rb', line 8

def initialize(units = {}, conversions = [])
  @units = units
  @parser ||= Unit::Parser.new(@units)
  @conversions ||= conversions
end

Instance Attribute Details

#conversionsObject (readonly)

Returns the value of attribute conversions.



6
7
8
# File 'lib/unitmanager/calculator.rb', line 6

def conversions
  @conversions
end

#unitsObject (readonly)

Returns the value of attribute units.



6
7
8
# File 'lib/unitmanager/calculator.rb', line 6

def units
  @units
end

Instance Method Details

#add_conversion(params) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/unitmanager/calculator.rb', line 56

def add_conversion(params)
  params.each {|source, target|
    begin
      @conversions << exp { target / source } << exp { source / target }
    rescue
      raise "Unit must be defined before used in conversion"
    end  
  }      
end

#add_unit(params) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/unitmanager/calculator.rb', line 39

def add_unit(params)
  raise "Unit symbol is required" unless (unit_sym = params[:unit])
  
  raise "Unit with symbol: #{unit_sym.to_s} already defined" if @units[unit_sym]
  
  if (based_on_sym = params[:based_on])
    raise "Invalid configuration. " +
       "Based_on unit must be defined before used." unless (based_on = @units[based_on_sym]) 
  end
  
  @units[unit_sym] = Unit::SimpleUnit.new({
    :unit => unit_sym, 
    :based_on => based_on, 
    :coefficient => params[:coefficient] || 1.0 
    })      
end

#exp(return_as = nil) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/unitmanager/calculator.rb', line 30

def exp (return_as = nil)

  operand = yield self
  raise "Operand is not Quantifiable" unless quantifiable?(operand)
  
  
  convert_to(operand.to_quantity(self), to_unit(return_as))
end

#perform_operation(first_operand, second_operand, operation) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/unitmanager/calculator.rb', line 18

def perform_operation(first_operand, second_operand, operation)
  result = case operation
             when :+ then add(first_operand, second_operand)
             when :- then subtract(first_operand, second_operand)
             when :* then multiply(first_operand, second_operand)
             when :/ then divide(first_operand, second_operand)
             else
              raise "Operation #{operation.to_s} is not supported"
           end
  return result                             
end

#quantity(value, unit_sym) ⇒ Object



14
15
16
# File 'lib/unitmanager/calculator.rb', line 14

def quantity(value, unit_sym)
  return Quantity.new(self, {:value => 1.0 * value, :unit => to_unit(unit_sym)})
end