Class: Worldwide::Units

Inherits:
Object
  • Object
show all
Defined in:
lib/worldwide/units.rb

Class Method Summary collapse

Class Method Details

.format(amount, unit, humanize: :short) ⇒ Object

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
# File 'lib/worldwide/units.rb', line 6

def format(amount, unit, humanize: :short)
  supported_humanization = supported_humanizations[humanize.to_sym]
  raise ArgumentError, "Unsupported value for `humanize`: #{humanize}." unless supported_humanization

  measurement_key = measurement_keys[unit.to_sym]
  raise ArgumentError, "Unsupported value for `unit`: #{unit}." unless measurement_key

  Cldr.t("units.unit_length.#{supported_humanization}.#{measurement_key}", count: amount)
end

.measurement_keysObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/worldwide/units.rb', line 23

def measurement_keys
  {
    millimeter: :length_millimeter,
    millimeters: :length_millimeter,
    centimeter: :length_centimeter,
    centimeters: :length_centimeter,
    foot: :length_foot,
    feet: :length_foot,
    inch: :length_inch,
    inches: :length_inch,
    meter: :length_meter,
    meters: :length_meter,
    gram: :mass_gram,
    grams: :mass_gram,
    kilogram: :mass_kilogram,
    kilograms: :mass_kilogram,
    ounce: :mass_ounce,
    ounces: :mass_ounce,
    pound: :mass_pound,
    pounds: :mass_pound,
    centiliter: :volume_centiliter,
    centiliters: :volume_centiliter,
    cubic_meter: :volume_cubic_meter,
    cubic_meters: :volume_cubic_meter,
    imperial_fluid_ounce: :volume_fluid_ounce_imperial,
    imperial_fluid_ounces: :volume_fluid_ounce_imperial,
    fluid_ounce: :volume_fluid_ounce,
    fluid_ounces: :volume_fluid_ounce,
    imperial_gallon: :volume_gallon_imperial,
    imperial_gallons: :volume_gallon_imperial,
    gallon: :volume_gallon,
    gallons: :volume_gallon,
    liter: :volume_liter,
    liters: :volume_liter,
    milliliter: :volume_milliliter,
    milliliters: :volume_milliliter,
    pint: :volume_pint,
    pints: :volume_pint,
    imperial_pint: :volume_pint_imperial,
    imperial_pints: :volume_pint_imperial,
    quart: :volume_quart,
    quarts: :volume_quart,
    imperial_quart: :volume_quart_imperial,
    imperial_quarts: :volume_quart_imperial,
    yard: :length_yard,
    yards: :length_yard,
  }.freeze
end

.supported_humanizationsObject



16
17
18
19
20
21
# File 'lib/worldwide/units.rb', line 16

def supported_humanizations
  {
    short: :short,
    long: :long,
  }.freeze
end