Class: Eymiha::UnitsMeasure
- Defined in:
- lib/eymiha/units/units_measure.rb
Overview
A UnitsMeasure groups sets of units that measure the same quality.
Instance Attribute Summary collapse
-
#derived ⇒ Object
A Hash of UnitsMeasures to powers that represent the bases for this UnitsMeasure if it is derived.
-
#formats ⇒ Object
readonly
A Methodic Hash mapping names of formats to procs that implement them.
Instance Method Summary collapse
-
#*(factor) ⇒ Object
Returns a new UnitsMeasure equal to the instance multiplied by another UnitsMeasure.
-
#**(exponent) ⇒ Object
Returns a new UnitsMeasure equal to the instance raised to the given power.
-
#/(divisor) ⇒ Object
Returns a new UnitsMeasure equal to the instance divided by another UnitsMeasure.
-
#format(options) ⇒ Object
Associates the name of a format with an output formatter.
-
#initialize ⇒ UnitsMeasure
constructor
:nodoc:.
-
#merge_derivation(derivation, multiplier = 1) ⇒ Object
:nodoc:.
-
#names ⇒ Object
Returns the names by which this UnitsMeasure is known.
-
#system(name, &block) ⇒ Object
Defines or extends a UnitsSystem.
-
#to_s ⇒ Object
Returns a String containing the names of this UnitsMeasure and the namse of the UnitsSystems defined within it.
Constructor Details
#initialize ⇒ UnitsMeasure
:nodoc:
17 18 19 |
# File 'lib/eymiha/units/units_measure.rb', line 17 def initialize # :nodoc: @formats = MethodicHash.new end |
Instance Attribute Details
#derived ⇒ Object
A Hash of UnitsMeasures to powers that represent the bases for this UnitsMeasure if it is derived.
11 12 13 |
# File 'lib/eymiha/units/units_measure.rb', line 11 def derived @derived end |
#formats ⇒ Object (readonly)
A Methodic Hash mapping names of formats to procs that implement them.
15 16 17 |
# File 'lib/eymiha/units/units_measure.rb', line 15 def formats @formats end |
Instance Method Details
#*(factor) ⇒ Object
Returns a new UnitsMeasure equal to the instance multiplied by another UnitsMeasure. This is typically used to derive UnitsMeasures - for example, if length is a UnitsMeasure, then length*length could be used as the target to derive area.
59 60 61 |
# File 'lib/eymiha/units/units_measure.rb', line 59 def *(factor) UnitsMeasure.new.merge_derivation(self).merge_derivation(factor) end |
#**(exponent) ⇒ Object
Returns a new UnitsMeasure equal to the instance raised to the given power. This is typically used to derive UnitsMeasures - for example, if length is a UnitsMeasure, then length**3 could be used as the target to derive volume.
43 44 45 |
# File 'lib/eymiha/units/units_measure.rb', line 43 def **(exponent) UnitsMeasure.new.merge_derivation(self => exponent) end |
#/(divisor) ⇒ Object
Returns a new UnitsMeasure equal to the instance divided by another UnitsMeasure. This is typically used to derive UnitsMeasures - for example, if mass and volume are UnitsMeasures, then mass/volume could be used as the target to derive density.
51 52 53 |
# File 'lib/eymiha/units/units_measure.rb', line 51 def /(divisor) UnitsMeasure.new.merge_derivation(self).merge_derivation(divisor,-1) end |
#format(options) ⇒ Object
Associates the name of a format with an output formatter.
89 90 91 |
# File 'lib/eymiha/units/units_measure.rb', line 89 def format() @formats[[:name]] = [:format] end |
#merge_derivation(derivation, multiplier = 1) ⇒ Object
:nodoc:
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/eymiha/units/units_measure.rb', line 63 def merge_derivation derivation, multiplier=1 # :nodoc: current = (self.derived ||= {}) if derivation.kind_of? UnitsMeasure if derivation.derived derivation.derived.each {|key,value| current[key] = (current[key] || 0) + multiplier*value } else current[derivation] = (current[derivation] || 0) + multiplier end elsif derivation.kind_of? Hash derivation.each {|key,value| current[key] = ((current[key] || 0 ) + multiplier*value) } else raise UnitsException, "Cannot add #{derivation.class_name} to derivation" end self end |
#names ⇒ Object
Returns the names by which this UnitsMeasure is known.
35 36 37 |
# File 'lib/eymiha/units/units_measure.rb', line 35 def names Units.names_of self end |
#system(name, &block) ⇒ Object
Defines or extends a UnitsSystem. During definition, if forward references between entities may occur and when encountered, are stored. Upon completion of the definition, if any existing unresolved still exist, an attempt is made to resolve them.
25 26 27 28 29 30 31 32 |
# File 'lib/eymiha/units/units_measure.rb', line 25 def system(name,&block) Units.defining self system = (self[name] ||= UnitsSystem.new(self,name)) block.call system if block_given? Units.resolve_forward_references Units.defining nil system end |
#to_s ⇒ Object
Returns a String containing the names of this UnitsMeasure and the namse of the UnitsSystems defined within it.
84 85 86 |
# File 'lib/eymiha/units/units_measure.rb', line 84 def to_s "#{class_name} #{names} #{keys}" end |