Class: Measured::ConversionTable

Inherits:
Object
  • Object
show all
Defined in:
lib/measured/conversion_table.rb

Instance Method Summary collapse

Constructor Details

#initialize(units) ⇒ ConversionTable

Returns a new instance of ConversionTable.



3
4
5
# File 'lib/measured/conversion_table.rb', line 3

def initialize(units)
  @units = units
end

Instance Method Details

#to_hObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/measured/conversion_table.rb', line 7

def to_h
  table = {}

  @units.map{|u| u.name}.each do |to_unit|
    to_table = {to_unit => BigDecimal("1")}

    table.each do |from_unit, from_table|
      to_table[from_unit] = find_conversion(to: from_unit, from: to_unit)
      from_table[to_unit] = find_conversion(to: to_unit, from: from_unit)
    end

    table[to_unit] = to_table
  end

  table
end