Class: Unitsml::Dimension
- Inherits:
-
Object
- Object
- Unitsml::Dimension
- Defined in:
- lib/unitsml/dimension.rb
Instance Attribute Summary collapse
-
#dimension_name ⇒ Object
Returns the value of attribute dimension_name.
-
#power_numerator ⇒ Object
Returns the value of attribute power_numerator.
Instance Method Summary collapse
- #==(object) ⇒ Object
- #dim_instance ⇒ Object
- #dim_symbols ⇒ Object
- #generate_id ⇒ Object
-
#initialize(dimension_name, power_numerator = nil) ⇒ Dimension
constructor
A new instance of Dimension.
- #modelize(value) ⇒ Object
- #to_asciimath(_) ⇒ Object
- #to_html(_) ⇒ Object
- #to_latex(_) ⇒ Object
- #to_mathml(_) ⇒ Object
- #to_unicode(_) ⇒ Object
- #to_xml(_) ⇒ Object
- #xml_instances_hash(options) ⇒ Object
Constructor Details
#initialize(dimension_name, power_numerator = nil) ⇒ Dimension
Returns a new instance of Dimension.
7 8 9 10 |
# File 'lib/unitsml/dimension.rb', line 7 def initialize(dimension_name, power_numerator = nil) @dimension_name = dimension_name @power_numerator = power_numerator end |
Instance Attribute Details
#dimension_name ⇒ Object
Returns the value of attribute dimension_name.
5 6 7 |
# File 'lib/unitsml/dimension.rb', line 5 def dimension_name @dimension_name end |
#power_numerator ⇒ Object
Returns the value of attribute power_numerator.
5 6 7 |
# File 'lib/unitsml/dimension.rb', line 5 def power_numerator @power_numerator end |
Instance Method Details
#==(object) ⇒ Object
12 13 14 15 16 |
# File 'lib/unitsml/dimension.rb', line 12 def ==(object) self.class == object.class && dimension_name == object&.dimension_name && power_numerator == object&.power_numerator end |
#dim_instance ⇒ Object
18 19 20 |
# File 'lib/unitsml/dimension.rb', line 18 def dim_instance @dim ||= Unitsdb.dimensions.find_parsables_by_id(dimension_name) end |
#dim_symbols ⇒ Object
22 23 24 |
# File 'lib/unitsml/dimension.rb', line 22 def dim_symbols dim_instance.send(@dim.processed_keys.last).dim_symbols.first end |
#generate_id ⇒ Object
61 62 63 |
# File 'lib/unitsml/dimension.rb', line 61 def generate_id "#{dimension_name.split('_').last}#{power_numerator}" end |
#modelize(value) ⇒ Object
77 78 79 |
# File 'lib/unitsml/dimension.rb', line 77 def modelize(value) value&.split("_")&.map(&:capitalize)&.join end |
#to_asciimath(_) ⇒ Object
47 48 49 |
# File 'lib/unitsml/dimension.rb', line 47 def to_asciimath(_) power_numerator_generic_code(:ascii) end |
#to_html(_) ⇒ Object
55 56 57 58 59 |
# File 'lib/unitsml/dimension.rb', line 55 def to_html(_) value = dim_symbols.html value = "#{value}<sup>#{power_numerator}</sup>" if power_numerator value end |
#to_latex(_) ⇒ Object
43 44 45 |
# File 'lib/unitsml/dimension.rb', line 43 def to_latex(_) power_numerator_generic_code(:latex) end |
#to_mathml(_) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/unitsml/dimension.rb', line 26 def to_mathml(_) # MathML key's value in unitsdb/dimensions.yaml file includes mi tags only. value = ::Mml::Mi.from_xml(dim_symbols.mathml) method_name = power_numerator ? :msup : :mi if power_numerator value = ::Mml::Msup.new( mrow_value: [ ::Mml::Mrow.new(mi_value: [value]), ::Mml::Mrow.new( mn_value: [::Mml::Mn.new(value: power_numerator)], ), ] ) end { method_name: method_name, value: value } end |
#to_unicode(_) ⇒ Object
51 52 53 |
# File 'lib/unitsml/dimension.rb', line 51 def to_unicode(_) power_numerator_generic_code(:unicode) end |
#to_xml(_) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/unitsml/dimension.rb', line 65 def to_xml(_) attributes = { symbol: dim_instance.processed_symbol, power_numerator: power_numerator || 1, } Model::DimensionQuantities.const_get(modelize(element_name)).new(attributes) end |
#xml_instances_hash(options) ⇒ Object
73 74 75 |
# File 'lib/unitsml/dimension.rb', line 73 def xml_instances_hash() { element_name => to_xml() } end |