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.



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

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.



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

def explicit_value
  @explicit_value
end

#rootObject

Returns the value of attribute root.



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

def root
  @root
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#==(object) ⇒ Object



23
24
25
26
27
28
# File 'lib/unitsml/formula.rb', line 23

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

#dimensions_extractionObject



82
83
84
# File 'lib/unitsml/formula.rb', line 82

def dimensions_extraction
  extract_dimensions(value)
end

#to_asciimath(options = {}) ⇒ Object



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

def to_asciimath(options = {})
  value.map { |obj| obj.to_asciimath(update_options(options)) }.join
end

#to_html(options = {}) ⇒ Object



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

def to_html(options = {})
  value.map { |obj| obj.to_html(update_options(options)) }.join
end

#to_latex(options = {}) ⇒ Object



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

def to_latex(options = {})
  value.map { |obj| obj.to_latex(update_options(options)) }.join
end

#to_mathml(options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/unitsml/formula.rb', line 30

def to_mathml(options = {})
  if root
    options = update_options(options)
    nullify_mml_models if plurimath_available?
    math = ::Mml::MathWithNamespace.new(display: "block")
    math.ordered = true
    math.element_order ||= []
    value.each { |instance| process_value(math, instance.to_mathml(options)) }

    generated_math = math.to_xml.gsub(/&amp;(.*?)(?=<\/)/, '&\1')
    reset_mml_models if plurimath_available?
    generated_math
  else
    value.map { |obj| obj.to_mathml(options) }
  end
end

#to_plurimath(options = {}) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/unitsml/formula.rb', line 74

def to_plurimath(options = {})
  ensure_plurimath_defined!
  options = update_options(options)
  return Plurimath::Math.parse(to_asciimath(options), :asciimath) if @orig_text.match?(/-$/)

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

#to_unicode(options = {}) ⇒ Object



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

def to_unicode(options = {})
  value.map { |obj| obj.to_unicode(update_options(options)) }.join
end

#to_xml(options = {}) ⇒ Object



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

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