Class: Unitsml::Formula

Inherits:
Object
  • Object
show all
Includes:
MathmlHelper
Defined in:
lib/unitsml/formula.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(value = [], explicit_value: nil, root: false, orig_text: nil, norm_text: nil) ⇒ Formula

Returns a new instance of Formula.



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

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.



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

def explicit_value
  @explicit_value
end

#rootObject

Returns the value of attribute root.



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

def root
  @root
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



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

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

#dimensions_extractionObject



88
89
90
# File 'lib/unitsml/formula.rb', line 88

def dimensions_extraction
  extract_dimensions(value)
end

#to_asciimath(options = {}) ⇒ Object



53
54
55
# File 'lib/unitsml/formula.rb', line 53

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

#to_html(options = {}) ⇒ Object



57
58
59
# File 'lib/unitsml/formula.rb', line 57

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

#to_latex(options = {}) ⇒ Object



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

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

#to_mathml(options = {}) ⇒ Object



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

def to_mathml(options = {})
  if root
    options = update_options(options)
    math = mml_v4_new(:math, display: "block")
    math.ordered = true
    math.element_order ||= []
    value.each do |instance|
      process_value(math, instance.to_mathml(options))
    end
    generated_math = math.to_xml(register: mml_v4_context.id)
      .gsub(%r{&amp;(.*?)(?=</)}, '&\1')

    generated_math.force_encoding("UTF-8")
  else
    value.map { |obj| obj.to_mathml(options) }
  end
end

#to_plurimath(options = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/unitsml/formula.rb', line 76

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

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

#to_unicode(options = {}) ⇒ Object



61
62
63
# File 'lib/unitsml/formula.rb', line 61

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

#to_xml(options = {}) ⇒ Object



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

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