Class: Unitsml::Unit
- Inherits:
-
Object
- Object
- Unitsml::Unit
- Defined in:
- lib/unitsml/unit.rb
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
- #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
- #system_name ⇒ Object
- #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
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_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
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_name ⇒ Object
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(/(-)(.+)/, '−\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
95 96 97 |
# File 'lib/unitsml/unit.rb', line 95 def prefix_name prefix&.prefix_name end |
#si_derived_bases ⇒ Object
107 108 109 |
# File 'lib/unitsml/unit.rb', line 107 def si_derived_bases unit_instance.si_derived_bases end |
#system_name ⇒ Object
103 104 105 |
# File 'lib/unitsml/unit.rb', line 103 def system_name unit_instance.unit_system.name end |
#system_type ⇒ Object
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() value = unit_symbols&.ascii value = "#{value}^#{power_numerator}" if power_numerator value = "#{prefix.to_asciimath()}#{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() 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
61 62 63 64 65 66 |
# File 'lib/unitsml/unit.rb', line 61 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
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() 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
84 85 86 87 88 89 |
# File 'lib/unitsml/unit.rb', line 84 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
22 23 24 |
# File 'lib/unitsml/unit.rb', line 22 def unit_instance Unitsdb.units.find_by_name(unit_name) end |
#unit_symbols ⇒ Object
26 27 28 |
# File 'lib/unitsml/unit.rb', line 26 def unit_symbols unit_instance.unit_symbols.find { |symbol| symbol.id == unit_name } end |