Module: H::Units

Defined in:
lib/h/units.rb

Constant Summary collapse

UNIT_SYN =
{
  'm2'=>'m**2',
  'kp/m2'=>'kp/m**2',
  '"'=>:inch,
  '\''=>:ft,
  "''"=>:inch,
  'in'=>:inch
}

Class Method Summary collapse

Class Method Details

.denormalize_units(u) ⇒ Object

Convert a units expression to the format to be presented to the user



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

def denormalize_units(u)
  if u.blank?
    u = nil
  else
    u = u.to_s.gsub('**','^').tr('*',' ')
  end
  u
end

.normalize_units(u) ⇒ Object

Convert a units expression to a Ruby expression valid for units-syste



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/h/units.rb', line 24

def normalize_units(u)
  if u.blank?
    u = nil
  else
    u = u.to_s
    u = UNIT_SYN[u] || u
    u = u.to_s.gsub('^','**').tr(' ','*')
    begin
      ::Units.u(u)
    rescue
      u = nil
    end
  end
  u
end

.valid?(u) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
# File 'lib/h/units.rb', line 18

def valid?(u)
  u = normalize_units(u)
  u && ::Units.u(u) rescue nil
end