Class: Unitsml::Unit

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

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.



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

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



15
16
17
18
19
20
# File 'lib/unitsml/unit.rb', line 15

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

#enumerated_nameObject



91
92
93
# File 'lib/unitsml/unit.rb', line 91

def enumerated_name
  unit_instance&.unit_name&.first
end

#numerator_value(mathml = true) ⇒ Object



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

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



95
96
97
# File 'lib/unitsml/unit.rb', line 95

def prefix_name
  prefix&.prefix_name
end

#si_derived_basesObject



107
108
109
# File 'lib/unitsml/unit.rb', line 107

def si_derived_bases
  unit_instance.si_derived_bases
end

#system_nameObject



103
104
105
# File 'lib/unitsml/unit.rb', line 103

def system_name
  unit_instance.unit_system.name
end

#system_typeObject



99
100
101
# File 'lib/unitsml/unit.rb', line 99

def system_type
  unit_instance.unit_system.type
end

#to_asciimath(options) ⇒ Object



68
69
70
71
72
73
# File 'lib/unitsml/unit.rb', line 68

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



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

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



61
62
63
64
65
66
# File 'lib/unitsml/unit.rb', line 61

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



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

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



84
85
86
87
88
89
# File 'lib/unitsml/unit.rb', line 84

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



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

def unit_instance
  Unitsdb.units.find_by_name(unit_name)
end

#unit_symbolsObject



26
27
28
# File 'lib/unitsml/unit.rb', line 26

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