Class: Eymiha::UnitsSystem

Inherits:
MethodicHash
  • Object
show all
Defined in:
lib/eymiha/units/units_system.rb

Overview

A UnitsSystem groups units that measure the same quality and are conceptually organized together within a UnitsMeasure. Its a MethodicHash of unit names to UnitsUnit instances.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(units_measure, name) ⇒ UnitsSystem

:nodoc:



16
17
18
19
# File 'lib/eymiha/units/units_system.rb', line 16

def initialize(units_measure,name) # :nodoc:
  @units_measure = units_measure
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

The name of this UnitsSystem



14
15
16
# File 'lib/eymiha/units/units_system.rb', line 14

def name
  @name
end

#units_measureObject (readonly)

The UnitsMeasure to which this instance is associated.



12
13
14
# File 'lib/eymiha/units/units_system.rb', line 12

def units_measure
  @units_measure
end

Instance Method Details

#format(options) ⇒ Object

Associates the name of a format with an output formatter in the instance’s UnitsMeasure.



42
43
44
# File 'lib/eymiha/units/units_system.rb', line 42

def format(options)
  @units_measure.format(options)
end

#greek_ten(base) ⇒ Object

:nodoc:



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

def greek_ten(base) # :nodoc:
  greek_unit base, "yocto", "y",  0.000000000000000000000001
  greek_unit base, "zepto", "z",  0.000000000000000000001
  greek_unit base, "atto",  "a",  0.000000000000000001
  greek_unit base, "femto", "f",  0.000000000000001
  greek_unit base, "pico",  "p",  0.000000000001
  greek_unit base, "nano",  "n",  0.000000001
  greek_unit base, "micro", "u",  0.000001
  greek_unit base, "milli", "m",  0.001
  greek_unit base, "centi", "c",  0.01
  greek_unit base, "deci",  "d",  0.1
  greek_unit base, "deca",  "da", 10.0
  greek_unit base, "hecto", "h",  100.0
  greek_unit base, "kilo",  "k",  1000.0
  greek_unit base, "mega",  "M",  1000000.0
  greek_unit base, "giga",  "G",  1000000000.0
  greek_unit base, "tera",  "T",  1000000000000.0
  greek_unit base, "peta",  "P",  1000000000000000.0
  greek_unit base, "exa",   "E",  1000000000000000000.0
  greek_unit base, "zetta", "Z",  1000000000000000000000.0
  greek_unit base, "yotta", "Y",  1000000000000000000000000.0
end

#greek_two(base) ⇒ Object

:nodoc:



69
70
71
72
73
74
75
76
77
78
# File 'lib/eymiha/units/units_system.rb', line 69

def greek_two(base) # :nodoc:
  greek_unit base, "kilo",  "k", 2**10
  greek_unit base, "mega",  "m", 2**20
  greek_unit base, "giga",  "g", 2**30
  greek_unit base, "tera",  "t", 2**40
  greek_unit base, "peta",  "p", 2**50
  greek_unit base, "exa",   "e", 2**60
  greek_unit base, "zetta", "z", 2**70
  greek_unit base, "yotta", "y", 2**80
end

#greek_unit(base, prefix, abbrev_prefix, scalar) ⇒ Object

:nodoc:



80
81
82
83
84
85
# File 'lib/eymiha/units/units_system.rb', line 80

def greek_unit(base,prefix,abbrev_prefix,scalar) # :nodoc:
  unit :name => prefix+base.name,
  :plural => prefix+base.plural,
  :abbrevs => base.abbrevs.collect { |abbrev| abbrev_prefix+abbrev },
  :equals => base.equals * scalar
end

#unit(attributes) ⇒ Object

Defines a new UnitsUnit. If ten-based or two-based greeking is defined, it is applied.



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

def unit(attributes)
  unit = nil
  if !Units.holding_forward_reference?
    unit = UnitsUnit.new self, attributes
    self[unit.name] = unit
    Units.add_unit unit
    case unit.greek
    when :ten then greek_ten unit
    when :two then greek_two unit
    end
  else
    Units.hold_forward_reference nil
  end
  Units.continue_forward_reference_resolution
  unit
end