Class: Unitsml::Unit

Inherits:
Object
  • Object
show all
Includes:
MathmlHelper
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

Methods included from MathmlHelper

coerce_mml_v4_collection_attributes!, coerce_mml_v4_collection_value, mml_v4_class_for, mml_v4_content_attribute, mml_v4_context, mml_v4_from_xml, mml_v4_new, mml_v4_with_content

Constructor Details

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

Returns a new instance of Unit.



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

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.



7
8
9
# File 'lib/unitsml/unit.rb', line 7

def power_numerator
  @power_numerator
end

#prefixObject

Returns the value of attribute prefix.



7
8
9
# File 'lib/unitsml/unit.rb', line 7

def prefix
  @prefix
end

#unit_nameObject

Returns the value of attribute unit_name.



7
8
9
# File 'lib/unitsml/unit.rb', line 7

def unit_name
  @unit_name
end

Instance Method Details

#==(other) ⇒ Object



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

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

#downcase_system_typeObject



111
112
113
# File 'lib/unitsml/unit.rb', line 111

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

#enumerated_nameObject



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

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

#inverse_power_numeratorObject



115
116
117
118
119
120
121
# File 'lib/unitsml/unit.rb', line 115

def inverse_power_numerator
  if power_numerator
    power_numerator.update_negative_sign
  else
    @power_numerator = Number.new("-1")
  end
end

#prefix_nameObject



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

def prefix_name
  prefix&.prefix_name
end

#si_derived_basesObject



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

def si_derived_bases
  unit_instance.si_derived_bases
end

#si_system_type?Boolean

Returns:

  • (Boolean)


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

def si_system_type?
  SI_UNIT_SYSTEM.include?(downcase_system_type)
end

#system_typeObject



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

def system_type
  system_reference&.first&.id
end

#to_asciimath(options) ⇒ Object



66
67
68
69
70
71
# File 'lib/unitsml/unit.rb', line 66

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

#to_html(options) ⇒ Object



73
74
75
76
77
78
# File 'lib/unitsml/unit.rb', line 73

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

#to_latex(options) ⇒ Object



59
60
61
62
63
64
# File 'lib/unitsml/unit.rb', line 59

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

#to_mathml(options) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/unitsml/unit.rb', line 39

def to_mathml(options)
  value = unit_symbols&.mathml
  tag_name = value.match(/^<(?<tag>\w+)/)[:tag]
  value = mml_v4_from_xml(tag_name, value)
  if prefix
    value = mml_v4_with_content(
      value,
      "#{prefix.to_mathml(options.merge(parent: value))}#{Array(value.value).join}",
    )
  end
  if power_numerator
    value = msup_tag(
      { method_name: tag_name, value: value },
      options,
    )
    tag_name = :msup
  end
  { method_name: tag_name.to_sym, value: value }
end

#to_unicode(options) ⇒ Object



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

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

#unit_instanceObject



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

def unit_instance
  Unitsdb.units.find_by_name(unit_name)
end

#unit_symbolsObject



35
36
37
# File 'lib/unitsml/unit.rb', line 35

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

#unknown_name?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/unitsml/unit.rb', line 31

def unknown_name?
  unit_name == Utility::UNKNOWN
end

#xml_postprocess_nameObject



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

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