Class: Object

Inherits:
BasicObject
Defined in:
lib/eymiha/units/object.rb

Overview

:nodoc:

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/eymiha/units/object.rb', line 7

def method_missing(method,*args)
  begin
    s = method.to_s.split('_')
    if s.select{|e| e == 'per'}.size > 0
      per_ratio method, args
    elsif s.select{|e| e == 'in'}.size > 0
      per_ratio method.to_s.sub(/_in_/,'_per_').to_sym, args
    else
      convert_unit_value method, args
    end
  rescue Exception => exception
    method_missing_before_units method, args
  end
end

Instance Method Details

#convert_per_ratio(method) ⇒ Object

Raises:



66
67
68
69
70
71
72
73
# File 'lib/eymiha/units/object.rb', line 66

def convert_per_ratio(method)
  ps = method.to_s.split '_per_'
  raise UnitsException('invalid per method') if ps.size != 2
  value = (1.unite ps[1]) / (1.unite ps[0])
  raise UnitsException.new("per ratio has units") if
    value.kind_of? NumericWithUnits
  value
end

#convert_unit_value(method, *args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/eymiha/units/object.rb', line 22

def convert_unit_value(method,*args)
  if Units.defining?
    reference = Units.make_forward_reference(method,Units.defining?)
    begin
      value = Units.convert 1, method
      Units.release_forward_reference reference
      value
    rescue MissingUnitsException
      Units.hold_forward_reference
    rescue UnitsException => exception
      units_problem("definition",exception,method,args)
    rescue Exception => exception
      method_missing_before_units(method,args)
    end
  else
    begin
      Units.convert 1, method
    rescue UnitsException => exception
      units_problem("use",exception,method,args)
    rescue Exception
      method_missing_before_units(method,args)
    end
  end
end

#method_missing_before_unitsObject



5
# File 'lib/eymiha/units/object.rb', line 5

alias :method_missing_before_units :method_missing

#per_ratio(method, *args) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/eymiha/units/object.rb', line 47

def per_ratio(method,*args)
  if Units.defining?
    reference = Units.make_forward_reference(method,Units.defining?)
    begin
      value = convert_per_ratio method
	Units.release_forward_reference reference
	value
    rescue MissingUnitsException
      Units.hold_forward_reference
    rescue UnitsException => exception
      units_problem("definition",exception,method,args)
    rescue Exception
      method_missing_before_units(method,args)
    end
  else
    convert_per_ratio method
  end
end

#units_problem(state, exception, method, args) ⇒ Object



75
76
77
# File 'lib/eymiha/units/object.rb', line 75

def units_problem(state,exception,method,args)
  raise exception
end