Class: Alchemist::NumericConversion

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/alchemist/numeric_conversion.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(unit_name, *args, &block) ⇒ Object (private)



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/alchemist/numeric_conversion.rb', line 61

def method_missing unit_name, *args, &block
  exponent, unit_name = Alchemist.parse_prefix(unit_name)
  if Conversions[ unit_name ]  
    types = Conversions[ @unit_name] & Conversions[ unit_name]
    if types[0] # assume first type
      if(Alchemist.conversion_table[types[0]][unit_name].is_a?(Array))
        Alchemist.conversion_table[types[0]][unit_name][1].call(base(types[0]))
      else
        NumericConversion.new(base(types[0]) / (exponent * Alchemist.conversion_table[types[0]][unit_name]), unit_name)
      end
    else
      raise Exception, "Incompatible Types"
    end
  else
    if args[0] && args[0].is_a?(NumericConversion) && Alchemist.operator_actions[unit_name]
      t1 = Conversions[ @unit_name ][0]
      t2 = Conversions[ args[0].unit_name ][0]
      Alchemist.operator_actions[unit_name].each do |s1, s2, new_type|
        if t1 == s1 && t2 == s2
          return (@value * args[0].to_f).send(new_type)
        end
      end
    end
    if unit_name == :*
      if args[0].is_a?(Numeric)
        @value *= args[0]
        return self
      else
        raise Exception, "Incompatible Types"
      end
    end
    if unit_name == :/ && args[0].is_a?(NumericConversion)
      raise Exception, "Incompatible Types" unless (Conversions[@unit_name] & Conversions[args[0].unit_name]).length > 0  
    end
    args.map!{|a| a.is_a?(NumericConversion) ? a.send(@unit_name).to_f / @exponent : a }
    @value = @value.send( unit_name, *args, &block )
    
    
    unit_name == :/ ? @value : self
  end
end

Instance Method Details

#<=>(other) ⇒ Object



50
51
52
# File 'lib/alchemist/numeric_conversion.rb', line 50

def <=>(other)
  other.respond_to?(:to) && (self.to_f * @exponent).to_f <=> other.to(@unit_name).to_f
end

#==(other) ⇒ Object



46
47
48
# File 'lib/alchemist/numeric_conversion.rb', line 46

def ==(other)
  self <=> other
end

#base(unit_type) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/alchemist/numeric_conversion.rb', line 22

def base(unit_type)
  if(Alchemist.conversion_table[unit_type][@unit_name].is_a?(Array))
    @exponent * Alchemist.conversion_table[unit_type][@unit_name][0].call(@value)
  else
    @exponent * @value * Alchemist.conversion_table[unit_type][@unit_name]
  end
end

#pObject



9
10
11
# File 'lib/alchemist/numeric_conversion.rb', line 9

def p
  per
end

#perObject



5
6
7
# File 'lib/alchemist/numeric_conversion.rb', line 5

def per
  Alchemist::CompoundNumericConversion.new(self)
end

#to(type = nil) ⇒ Object Also known as: as



13
14
15
16
17
18
19
# File 'lib/alchemist/numeric_conversion.rb', line 13

def to(type = nil)
  unless type
    self 
  else
    send(type)
  end
end

#to_fObject



42
43
44
# File 'lib/alchemist/numeric_conversion.rb', line 42

def to_f
  @value
end

#to_sObject



34
35
36
# File 'lib/alchemist/numeric_conversion.rb', line 34

def to_s
  @value.to_s
end

#unit_nameObject



30
31
32
# File 'lib/alchemist/numeric_conversion.rb', line 30

def unit_name
  @unit_name
end

#valueObject



38
39
40
# File 'lib/alchemist/numeric_conversion.rb', line 38

def value
  @value
end