Class: Unitsml::Formula

Inherits:
Object
  • Object
show all
Defined in:
lib/unitsml/formula.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = [], explicit_value: nil, root: false, orig_text: nil, norm_text: nil) ⇒ Formula

Returns a new instance of Formula.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/unitsml/formula.rb', line 9

def initialize(value = [],
               explicit_value: nil,
               root: false,
               orig_text: nil,
               norm_text: nil)
  @value = value
  @explicit_value = explicit_value
  @root = root
  @orig_text = orig_text
  @norm_text = norm_text
end

Instance Attribute Details

#explicit_valueObject

Returns the value of attribute explicit_value.



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

def explicit_value
  @explicit_value
end

#rootObject

Returns the value of attribute root.



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

def root
  @root
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#==(object) ⇒ Object



21
22
23
24
25
26
# File 'lib/unitsml/formula.rb', line 21

def ==(object)
  self.class == object.class &&
    value == object&.value &&
    explicit_value == object&.explicit_value &&
    root == object.root
end

#to_asciimathObject



47
48
49
# File 'lib/unitsml/formula.rb', line 47

def to_asciimath
  value.map(&:to_asciimath).join
end

#to_htmlObject



51
52
53
# File 'lib/unitsml/formula.rb', line 51

def to_html
  value.map(&:to_html).join
end

#to_latexObject



43
44
45
# File 'lib/unitsml/formula.rb', line 43

def to_latex
  value.map(&:to_latex).join
end

#to_mathmlObject



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

def to_mathml
  if root
    attributes = { xmlns: "http://www.w3.org/1998/Math/MathML", display: "block" }
    math = Utility.ox_element("math", attributes: attributes)
    Ox.dump(
      Utility.update_nodes(
        math,
        value.map(&:to_mathml).flatten,
      ),
    ).gsub(/&amp;(.*?)(?=<\/)/, '&\1')
  else
    value.map(&:to_mathml)
  end
end

#to_plurimathObject



70
71
72
73
74
# File 'lib/unitsml/formula.rb', line 70

def to_plurimath
  return Plurimath::Math.parse(to_asciimath, :asciimath) if @orig_text.match?(/-$/)

  Plurimath::Math.parse(to_mathml, :mathml)
end

#to_unicodeObject



55
56
57
# File 'lib/unitsml/formula.rb', line 55

def to_unicode
  value.map(&:to_unicode).join
end

#to_xmlObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/unitsml/formula.rb', line 59

def to_xml
  dimensions_array = extract_dimensions(value)
  if (dimensions_array).any?
    dimensions(sort_dims(dimensions_array))
  elsif @orig_text.match(/-$/)
    prefixes
  else
    units
  end
end