Class: UnitHelper

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

Overview

Simple class to provide a wrapper/common pattern around Ruby-Units

Class Method Summary collapse

Class Method Details

.abbreviation(unit) ⇒ String

Unit is a RubyUnits::UnitDefinition

Returns:

  • (String)

    the unit abbreviation



22
23
24
# File 'lib/unit_helper.rb', line 22

def abbreviation(unit)
  unit.aliases.first
end

.all_units(add_kind: false) ⇒ Array

Returns the cached unit list.

Returns:

  • (Array)

    the cached unit list



45
46
47
48
49
# File 'lib/unit_helper.rb', line 45

def all_units(add_kind: false)
  Rails.cache.fetch([:units, :all, I18n.locale, :v9], expires_in: 1.week) do
    units_by_kind(:all, add_kind: add_kind)
  end
end

.display_name(unit) ⇒ String

Unit is a RubyUnits::UnitDefinition

Returns:

  • (String)

    the localized display name



12
13
14
15
16
17
18
# File 'lib/unit_helper.rb', line 12

def display_name(unit)
  # the longest aliases seems the nicest compared to the display_name
  name_en = unit.aliases.max_by(&:length).titleize
  return name_en if I18n.locale.to_s.first(2) == 'en'

  TranslationKey.translate(name_en, I18n.locale, namespace: 'Units')
end

.kind_of_unitsArray<Symbol>

Returns the available unit kinds.

Returns:

  • (Array<Symbol>)

    the available unit kinds



6
7
8
# File 'lib/unit_helper.rb', line 6

def kind_of_units
  RubyUnits::Unit.definitions.values.map(&:kind).uniq
end

.length_unitsArray

Returns length units.

Returns:

  • (Array)

    length units



52
53
54
# File 'lib/unit_helper.rb', line 52

def length_units
  units_by_kind(:length)
end

.mass_unitsArray

Returns mass units.

Returns:

  • (Array)

    mass units



57
58
59
# File 'lib/unit_helper.rb', line 57

def mass_units
  units_by_kind(:mass)
end

.unit_symbol(unit) ⇒ String?

Returns the display symbol for the unit.

Returns:

  • (String, nil)

    the display symbol for the unit



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/unit_helper.rb', line 62

def unit_symbol(unit)
  case unit&.downcase
  when 'degf'
    " °F" # could be ℉ but seem too compressed
  when 'degc'
    " °C" # or ℃
  when 'ohm'
    " Ω"
  when "hertz"
    ""
  # when "kg"
  #   "㎏"
  when "ft"
    ""
  when "in"
    ""
  when "sqft"
    " ft²"
  when "sqin"
    " in²"
  when "m2"
    ""
  else
    " #{unit}"
  end
end

.units_by_kind(kind, add_kind: false) ⇒ Array<Array(String, String)>

Returns [display name, abbreviation] pairs.

Returns:

  • (Array<Array(String, String)>)

    [display name, abbreviation] pairs



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/unit_helper.rb', line 27

def units_by_kind(kind, add_kind: false)
  udv = RubyUnits::Unit.definitions.values
  udv.select! { |d| d.kind.to_s == kind.to_s } unless kind == :all || kind.nil?
  # Reject prefixes stuff
  udv.reject! { |d| d.kind == :prefix }
  udv.map do |ud|
    n = display_name(ud)
    v = abbreviation(ud)
    unless n.casecmp(v).zero?
      # We append the abbreviation in the name
      n = "#{n} (#{v})"
    end
    n = "#{n} [#{ud.kind}]" if add_kind
    [n, v]
  end
end