Class: Eymiha::UnitsHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/eymiha/units/units_hash.rb

Overview

The unit part of a NumericWithUnits is a UnitsHash - a Hash from UnitsUnit instances to powers.

Constant Summary collapse

@@debug =
false

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unit = nil, power = 1) ⇒ UnitsHash

:nodoc



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/eymiha/units/units_hash.rb', line 15

def initialize(unit = nil,power = 1) # :nodoc
  if unit
    if unit.kind_of? UnitsUnit
      self[unit] = power
    elsif unit.kind_of? UnitsHash
      unit.each { |k,v| self[k] = v*power }
    else
      raise UnitsException.new("invalid unit: #{unit}")
    end
  end
end

Class Method Details

.debug=(value) ⇒ Object



11
12
13
# File 'lib/eymiha/units/units_hash.rb', line 11

def self.debug=(value)
  @@debug = value
end

Instance Method Details

#**(power) ⇒ Object

Returns a new UnitsHash whose value is the instance raised to the given power.



59
60
61
# File 'lib/eymiha/units/units_hash.rb', line 59

def **(power)
  clone.power!(power)
end

#abbrevs_to_sObject

:nodoc:



41
42
43
44
45
46
47
48
49
# File 'lib/eymiha/units/units_hash.rb', line 41

def abbrevs_to_s # :nodoc:
  p = select {|k,v| v > 0}.sort{|e1,e2| e1[1] <=> e2[1]}.collect {|e|
    "#{e[0].abbrevs[0] || e[0].name}#{(e[1] == 1) ? "" : "^#{e[1]}"}"}
  n = select {|k,v| v < 0}.sort{|e1,e2| e1[1] <=> e2[1]}.collect {|e|
    "#{e[0].abbrevs[0] || e[0].name}#{(e[1] == -1) ? "" : "^#{-e[1]}"}"}
  numerator = (p.size > 0)? p.join(" ") : (n.size > 0)? "1" : ""
  denominator = (n.size > 0)? ((p.size > 0)? " / " : "/")+n.join(" ") : ""
  "#{numerator}#{denominator}"
end

#derived?Boolean

Returns true is any of the instance’s components are derived.

Returns:

  • (Boolean)


90
91
92
93
# File 'lib/eymiha/units/units_hash.rb', line 90

def derived?
  keys.select{|unit|
    unit.units_system.units_measure.derived != nil}.size > 0
end

#has_units?Boolean

Return true if the instance is not equal to 1, ie. has at least one non-zero exponent.

Returns:

  • (Boolean)


112
113
114
# File 'lib/eymiha/units/units_hash.rb', line 112

def has_units?
  (self.select {|k,v| v != 0.0}).size > 0
end

#measureObject

Returns the UnitsMeasure of the instance if defined.



82
83
84
85
86
87
# File 'lib/eymiha/units/units_hash.rb', line 82

def measure
  measure = UnitsMeasure.new
  each { |unit,power|
    measure.merge_derivation unit.units_system.units_measure => power }
  Units.find_by_derivation(measure.derived)
end

#merge(value, power = 1) ⇒ Object Also known as: *

Returns a new UnitsHash whose value is the instance’s units and powers have been merged with the given value raised to the power.



65
66
67
# File 'lib/eymiha/units/units_hash.rb', line 65

def merge(value,power=1)
  clone.merge!(value,power)
end

#merge!(value, power = 1) ⇒ Object

Merges and returns the instance after the value raised to the power has been merged into it.



71
72
73
74
75
76
77
# File 'lib/eymiha/units/units_hash.rb', line 71

def merge!(value,power=1)
  value.unit.each { |k,v|
    self[k] = (self[k] || 0) + v*power
    delete k if self[k] == 0
  }
  self
end

#power!(power) ⇒ Object

Raises and returns the instance after each of its exponents has been raised by the given power.



53
54
55
# File 'lib/eymiha/units/units_hash.rb', line 53

def power!(power)
  self.each { |k,v| self[k] = v*power }
end

#reduceObject

Return a NumericWithUnits that represents the instance when all derived units have been replaced with the units from which they derive.



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/eymiha/units/units_hash.rb', line 97

def reduce
  puts "UnitsHash:reduce #{to_s}" if @@debug
  factor = 1.0
  new_unit = UnitsHash.new
  each do |unit,power|
    puts "  #{unit} #{power}" if @@debug
    factor *= unit.equals.numeric**power
    puts "  #{factor}" if @@debug
    new_unit.merge!(unit.equals,power)
  end
  factor.unite new_unit
end

#to_s(numeric = 1, use_abbrevs = false) ⇒ Object

Returns a String reprentation of the instance. If there is only one item in the UnitsHash, whole names are rendered; otherwise abbreviations are used.



30
31
32
33
34
35
36
37
38
39
# File 'lib/eymiha/units/units_hash.rb', line 30

def to_s(numeric = 1,use_abbrevs = false)
  if size == 1 &&
      (su = select {|k,v| v == 1}).size == 1 &&
      !use_abbrevs
    su = su.to_a[0][0]
    ((numeric == 1)? su.name : su.plural).gsub(/_/,' ')
  else
    abbrevs_to_s
  end
end