Method: Unitsml::Utility.decompose_unit

Defined in:
lib/unitsml/utility.rb

.decompose_unit(u) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/unitsml/utility.rb', line 88

def decompose_unit(u)
  if u&.unit_name == "g" || Lutaml::Model::Utils.snake_case(u.system_type) == "si_base"
    { unit: u, prefix: u&.prefix }
  elsif u.si_derived_bases.nil? || u.si_derived_bases.empty?
    { unit: Unit.new("unknown") }
  else
    u.si_derived_bases.each_with_object([]) do |k, object|
      prefix = if !k.prefix_reference.nil?
                 combine_prefixes(prefix_object(k.prefix_reference), u.prefix)
               else
                 u.prefix
               end
      unit_name = Unitsdb.units.find_by_id(k.unit_reference.id).symbols.first.id
      exponent = (k.power&.to_i || 1) * (u.power_numerator&.to_f || 1)
      object << { prefix: prefix,
             unit: Unit.new(unit_name, exponent, prefix: prefix),
           }
    end
  end
end