Method: Unitsml::Utility.dim_id

Defined in:
lib/unitsml/utility.rb

.dim_id(dims) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/unitsml/utility.rb', line 71

def dim_id(dims)
  return nil if dims.nil? || dims.empty?

  dim_hash = dims.each_with_object({}) { |h, m| m[h[:dimension]] = h }
  dims_vector = DIMS_VECTOR.map { |h| dim_hash.dig(h, :exponent) }.join(":")
  id = Unitsdb.dimensions.find_by_vector(dims_vector)&.id and return id.to_s

  "D_" + dims.map do |d|
    (U2D.dig(d[:unit], :symbol) || Dim2D.dig(d[:id], :symbol)) +
      (d[:exponent] == 1 ? "" : float_to_display(d[:exponent]))
  end.join("")
end