Class: Unitsml::Dimension

Inherits:
Object
  • Object
show all
Includes:
MathmlHelper
Defined in:
lib/unitsml/dimension.rb

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(dimension_name, power_numerator = nil) ⇒ Dimension

Returns a new instance of Dimension.



9
10
11
12
# File 'lib/unitsml/dimension.rb', line 9

def initialize(dimension_name, power_numerator = nil)
  @dimension_name = dimension_name
  @power_numerator = power_numerator
end

Instance Attribute Details

#dimension_nameObject

Returns the value of attribute dimension_name.



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

def dimension_name
  @dimension_name
end

#power_numeratorObject

Returns the value of attribute power_numerator.



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

def power_numerator
  @power_numerator
end

Instance Method Details

#==(other) ⇒ Object



14
15
16
17
18
# File 'lib/unitsml/dimension.rb', line 14

def ==(other)
  self.class == other.class &&
    dimension_name == other&.dimension_name &&
    power_numerator == other&.power_numerator
end

#dim_instanceObject



20
21
22
# File 'lib/unitsml/dimension.rb', line 20

def dim_instance
  @dim_instance ||= Unitsdb.dimensions.find_parsables_by_id(dimension_name)
end

#dim_symbolsObject



24
25
26
# File 'lib/unitsml/dimension.rb', line 24

def dim_symbols
  dim_instance.public_send(dim_instance.processed_keys.last).symbols.first
end

#generate_idObject



59
60
61
# File 'lib/unitsml/dimension.rb', line 59

def generate_id
  "#{dimension_name.split('_').last}#{power_numerator&.raw_value}"
end

#modelize(value) ⇒ Object



78
79
80
# File 'lib/unitsml/dimension.rb', line 78

def modelize(value)
  value&.split("_")&.map(&:capitalize)&.join
end

#to_asciimath(options) ⇒ Object



45
46
47
# File 'lib/unitsml/dimension.rb', line 45

def to_asciimath(options)
  power_numerator_generic_code(:ascii, options)
end

#to_html(options) ⇒ Object



53
54
55
56
57
# File 'lib/unitsml/dimension.rb', line 53

def to_html(options)
  value = dim_symbols.html
  value = "#{value}#{html_numerator_conversion(options)}" if power_numerator
  value
end

#to_latex(options) ⇒ Object



41
42
43
# File 'lib/unitsml/dimension.rb', line 41

def to_latex(options)
  power_numerator_generic_code(:latex, options)
end

#to_mathml(options) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/unitsml/dimension.rb', line 28

def to_mathml(options)
  # MathML key's value in unitsdb/dimensions.yaml
  # file includes mi tags only.
  value = mml_v4_from_xml(:mi, dim_symbols.mathml)
  method_name = if power_numerator
                  value = msup_tag(value, options)
                  :msup
                else
                  :mi
                end
  { method_name: method_name, value: value }
end

#to_unicode(options) ⇒ Object



49
50
51
# File 'lib/unitsml/dimension.rb', line 49

def to_unicode(options)
  power_numerator_generic_code(:unicode, options)
end

#to_xml(_) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/unitsml/dimension.rb', line 63

def to_xml(_)
  attributes = {
    symbol: dim_instance.processed_symbol,
    power_numerator: power_numerator&.raw_value || 1,
  }
  Model::DimensionQuantities.const_get(modelize(element_name)).new(
    **attributes,
    lutaml_register: Configuration.context.id,
  )
end

#xml_instances_hash(options) ⇒ Object



74
75
76
# File 'lib/unitsml/dimension.rb', line 74

def xml_instances_hash(options)
  { element_name => to_xml(options) }
end