Class: Unitsml::Unit
- Inherits:
-
Object
- Object
- Unitsml::Unit
- 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
-
#power_numerator ⇒ Object
Returns the value of attribute power_numerator.
-
#prefix ⇒ Object
Returns the value of attribute prefix.
-
#unit_name ⇒ Object
Returns the value of attribute unit_name.
Instance Method Summary collapse
- #==(object) ⇒ Object
- #downcase_system_type ⇒ Object
- #enumerated_name ⇒ Object
-
#initialize(unit_name, power_numerator = nil, prefix: nil) ⇒ Unit
constructor
A new instance of Unit.
- #numerator_value(mathml = true) ⇒ Object
- #prefix_name ⇒ Object
- #si_derived_bases ⇒ Object
- #si_system_type? ⇒ Boolean
- #system_type ⇒ Object
- #to_asciimath(options) ⇒ Object
- #to_html(options) ⇒ Object
- #to_latex(options) ⇒ Object
- #to_mathml(options) ⇒ Object
- #to_unicode(options) ⇒ Object
- #unit_instance ⇒ Object
- #unit_symbols ⇒ Object
- #xml_postprocess_name ⇒ Object
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_numerator ⇒ Object
Returns the value of attribute power_numerator.
5 6 7 |
# File 'lib/unitsml/unit.rb', line 5 def power_numerator @power_numerator end |
#prefix ⇒ Object
Returns the value of attribute prefix.
5 6 7 |
# File 'lib/unitsml/unit.rb', line 5 def prefix @prefix end |
#unit_name ⇒ Object
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_type ⇒ Object
117 118 119 |
# File 'lib/unitsml/unit.rb', line 117 def downcase_system_type Lutaml::Model::Utils.snake_case(system_type) end |
#enumerated_name ⇒ Object
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(/(-)(.+)/, '−\2') unless mathml integer = integer.sub(/(-)(.+)/, '<mn>\2</mn>') integer = ::Mml::Mn.from_xml(integer) mo_tag = ::Mml::Mo.new(value: "−") { mo_value: [mo_tag], mn_value: [integer] } end |
#prefix_name ⇒ Object
97 98 99 |
# File 'lib/unitsml/unit.rb', line 97 def prefix_name prefix&.prefix_name end |
#si_derived_bases ⇒ Object
105 106 107 |
# File 'lib/unitsml/unit.rb', line 105 def si_derived_bases unit_instance.si_derived_bases end |
#si_system_type? ⇒ Boolean
113 114 115 |
# File 'lib/unitsml/unit.rb', line 113 def si_system_type? SI_UNIT_SYSTEM.include?(downcase_system_type) end |
#system_type ⇒ Object
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() value = unit_symbols&.ascii value = "#{value}^#{power_numerator}" if power_numerator value = "#{prefix.to_asciimath()}#{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() value = unit_symbols&.html if power_numerator value = "#{value}<sup>#{numerator_value(false)}</sup>" end value = "#{prefix.to_html()}#{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() value = unit_symbols&.latex value = "#{value}^#{power_numerator}" if power_numerator value = "#{prefix.to_latex()}#{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() 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()}#{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() value = unit_symbols&.unicode value = "#{value}^#{power_numerator}" if power_numerator value = "#{prefix.to_unicode()}#{value}" if prefix value end |
#unit_instance ⇒ Object
24 25 26 |
# File 'lib/unitsml/unit.rb', line 24 def unit_instance Unitsdb.units.find_by_name(unit_name) end |
#unit_symbols ⇒ Object
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_name ⇒ Object
109 110 111 |
# File 'lib/unitsml/unit.rb', line 109 def xml_postprocess_name "#{prefix_name}#{unit_name}#{display_exp}" end |