Class: Numeric
Overview
Numerics are extended to allow them to be united with units.
Part of this extension is in the added dynamism of the class. During unit definition, if an unencountered unit is referenced, the use is held until more information is loaded into the system and the unknown unit has been resolved. During use, unencountered units are raised.
Instance Method Summary collapse
-
#method_missing(method, *args) ⇒ Object
:nodoc:.
- #method_missing_before_units ⇒ Object
-
#unite(unit = nil, power = 1, measure = nil) ⇒ Object
Returns a NumericWithUnits whose numeric value is the value of the instance, and whose unit is determined from the provided unit and power in the context of a given measure: * as a String or Symbol equal to the singular, plural or abbreviated name of a unit, * as a UnitsHash, * as the unit part of a NumericWithUnits, * as an Array of any of the above.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
:nodoc:
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 |
# File 'lib/eymiha/units/numeric.rb', line 16 def method_missing(method,*args) # :nodoc: if Units.defining? reference = Units.make_forward_reference(method,Units.defining?) begin value = Units.convert self, 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 ps = method.to_s.split '_per_' case ps.size when 1 then unite method when 2 then unite(ps[0])/(1.unite(ps[1])) else raise UnitsException('invalid per method') end rescue UnitsException => exception units_problem("usage",exception,method,args) rescue Exception method_missing_before_units(method,*args) end end end |
Instance Method Details
#method_missing_before_units ⇒ Object
14 |
# File 'lib/eymiha/units/numeric.rb', line 14 alias :method_missing_before_units :method_missing |
#unite(unit = nil, power = 1, measure = nil) ⇒ Object
Returns a NumericWithUnits whose numeric value is the value of the instance, and whose unit is determined from the provided unit and power in the context of a given measure:
-
as a String or Symbol equal to the singular, plural or abbreviated name of a unit,
-
as a UnitsHash,
-
as the unit part of a NumericWithUnits,
-
as an Array of any of the above.
53 54 55 56 57 58 59 60 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 |
# File 'lib/eymiha/units/numeric.rb', line 53 def unite(unit=nil,power=1,measure=nil) if !unit self else if (unit.kind_of? Array) units = UnitsHash.new unit.each {|u| units.merge! 1.unite(u)} unit = units elsif (unit.kind_of? String)||(unit.kind_of? Symbol) s = unit.to_s as = s.split('_and_') if as.size == 1 units = Units.lookup(as[0]) case units.size when 1 then unit = units[0] when 0 then as = as[0].split('_') units_problem("usage", AmbiguousUnitsException.new(unit), :unit=,unit) if as.size == 0 unit = unite(as,power,measure).unit else units_problem("usage", AmbiguousUnitsException.new(unit), :unit=,unit) end else unit = unite(as,power,measure).unit end elsif unit.kind_of? NumericWithUnits unit = unit.unit end NumericWithUnits.new(self,unit,power) end end |