Class: Unitsml::Unit

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

Constant Summary collapse

SI_UNIT_SYSTEM =
%w[si_base si_derived_special si_derived_non_special].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unit_name, power_numerator = nil, prefix: nil) ⇒ Unit

Returns a new instance of Unit.



9
10
11
12
13
14
15
# File 'lib/unitsml/unit.rb', line 9

def initialize(unit_name,
               power_numerator = nil,
               prefix: nil)
  @prefix = prefix
  @unit_name = unit_name
  @power_numerator = power_numerator
end

Instance Attribute Details

#power_numeratorObject

Returns the value of attribute power_numerator.



5
6
7
# File 'lib/unitsml/unit.rb', line 5

def power_numerator
  @power_numerator
end

#prefixObject

Returns the value of attribute prefix.



5
6
7
# File 'lib/unitsml/unit.rb', line 5

def prefix
  @prefix
end

#unit_nameObject

Returns the value of attribute unit_name.



5
6
7
# File 'lib/unitsml/unit.rb', line 5

def unit_name
  @unit_name
end

Instance Method Details

#==(object) ⇒ Object



17
18
19
20
21
22
# File 'lib/unitsml/unit.rb', line 17

def ==(object)
  self.class == object.class &&
    prefix == object&.prefix &&
    unit_name == object&.unit_name &&
    power_numerator == object&.power_numerator
end

#downcase_system_typeObject



117
118
119
# File 'lib/unitsml/unit.rb', line 117

def downcase_system_type
  Lutaml::Model::Utils.snake_case(system_type)
end

#enumerated_nameObject



93
94
95
# File 'lib/unitsml/unit.rb', line 93

def enumerated_name
  unit_instance.names.find { |name| name.lang == "en" }&.value
end

#numerator_value(mathml = true) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/unitsml/unit.rb', line 32

def numerator_value(mathml = true)
  integer = power_numerator.to_s
  unless integer.match?(/-/)
    return mathml ? { mn_value: [::Mml::Mn.from_xml("<mn>#{integer}</mn>")] } : integer
  end

  return integer.sub(/(-)(.+)/, '&#x2212;\2') unless mathml

  integer = integer.sub(/(-)(.+)/, '<mn>\2</mn>')
  integer = ::Mml::Mn.from_xml(integer)
  mo_tag = ::Mml::Mo.new(value: "&#x2212;")
  { mo_value: [mo_tag], mn_value: [integer] }
end

#prefix_nameObject



97
98
99
# File 'lib/unitsml/unit.rb', line 97

def prefix_name
  prefix&.prefix_name
end

#si_derived_basesObject



105
106
107
# File 'lib/unitsml/unit.rb', line 105

def si_derived_bases
  unit_instance.si_derived_bases
end

#si_system_type?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/unitsml/unit.rb', line 113

def si_system_type?
  SI_UNIT_SYSTEM.include?(downcase_system_type)
end

#system_typeObject



101
102
103
# File 'lib/unitsml/unit.rb', line 101

def system_type
  system_reference&.first&.id
end

#to_asciimath(options) ⇒ Object



70
71
72
73
74
75
# File 'lib/unitsml/unit.rb', line 70

def to_asciimath(options)
  value = unit_symbols&.ascii
  value = "#{value}^#{power_numerator}" if power_numerator
  value = "#{prefix.to_asciimath(options)}#{value}" if prefix
  value
end

#to_html(options) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/unitsml/unit.rb', line 77

def to_html(options)
  value = unit_symbols&.html
  if power_numerator
    value = "#{value}<sup>#{numerator_value(false)}</sup>"
  end
  value = "#{prefix.to_html(options)}#{value}" if prefix
  value
end

#to_latex(options) ⇒ Object



63
64
65
66
67
68
# File 'lib/unitsml/unit.rb', line 63

def to_latex(options)
  value = unit_symbols&.latex
  value = "#{value}^#{power_numerator}" if power_numerator
  value = "#{prefix.to_latex(options)}#{value}" if prefix
  value
end

#to_mathml(options) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/unitsml/unit.rb', line 46

def to_mathml(options)
  value = unit_symbols&.mathml
  tag_name = value.match(/^<(?<tag>\w+)/)[:tag]
  value = ::Mml.const_get(tag_name.capitalize).from_xml(value)
  value.value = "#{prefix.to_mathml(options)}#{value.value}" if prefix
  if power_numerator
    value = ::Mml::Msup.new(
      mrow_value: [
        ::Mml::Mrow.new("#{tag_name}_value": Array(value)),
        ::Mml::Mrow.new(numerator_value),
      ]
    )
    tag_name = :msup
  end
  { method_name: tag_name.to_sym, value: value }
end

#to_unicode(options) ⇒ Object



86
87
88
89
90
91
# File 'lib/unitsml/unit.rb', line 86

def to_unicode(options)
  value = unit_symbols&.unicode
  value = "#{value}^#{power_numerator}" if power_numerator
  value = "#{prefix.to_unicode(options)}#{value}" if prefix
  value
end

#unit_instanceObject



24
25
26
# File 'lib/unitsml/unit.rb', line 24

def unit_instance
  Unitsdb.units.find_by_name(unit_name)
end

#unit_symbolsObject



28
29
30
# File 'lib/unitsml/unit.rb', line 28

def unit_symbols
  unit_instance.symbols.find { |symbol| symbol.id == unit_name }
end

#xml_postprocess_nameObject



109
110
111
# File 'lib/unitsml/unit.rb', line 109

def xml_postprocess_name
  "#{prefix_name}#{unit_name}#{display_exp}"
end