Module: Unitsml::MathmlHelper

Included in:
Dimension, Extender, Fenced, Formula, Number, Prefix, Unit
Defined in:
lib/unitsml/mathml_helper.rb

Class Method Summary collapse

Class Method Details

.coerce_mml_v4_collection_attributes!(klass, attributes) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/unitsml/mathml_helper.rb', line 38

def coerce_mml_v4_collection_attributes!(klass, attributes)
  return unless klass.is_a?(Class) && klass <= Lutaml::Model::Serializable

  attributes.each do |name, value|
    attribute = klass.attributes[name]
    attributes[name] = coerce_mml_v4_collection_value(attribute, value)
  end
end

.coerce_mml_v4_collection_value(attribute, value) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/unitsml/mathml_helper.rb', line 47

def coerce_mml_v4_collection_value(attribute, value)
  return value unless attribute&.collection?
  return value if value.nil?
  return value if attribute.collection_instance?(value)

  attribute.build_collection(value)
end

.mml_v4_class_for(klass_ref) ⇒ Object



30
31
32
33
34
# File 'lib/unitsml/mathml_helper.rb', line 30

def mml_v4_class_for(klass_ref)
  return klass_ref if klass_ref.is_a?(Class)

  mml_v4_context.lookup_local(klass_ref.to_sym)
end

.mml_v4_content_attribute(instance) ⇒ Object



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

def mml_v4_content_attribute(instance)
  register = instance.lutaml_register || mml_v4_context.id
  instance.class.mappings_for(:xml, register).content_mapping.to
end

.mml_v4_contextObject



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

def mml_v4_context
  ::Mml::V4::Configuration.context
end

.mml_v4_from_xml(klass_ref, xml) ⇒ Object



11
12
13
# File 'lib/unitsml/mathml_helper.rb', line 11

def mml_v4_from_xml(klass_ref, xml)
  mml_v4_class_for(klass_ref).from_xml(xml, register: mml_v4_context.id)
end

.mml_v4_new(klass_ref, **attributes) ⇒ Object



15
16
17
18
19
# File 'lib/unitsml/mathml_helper.rb', line 15

def mml_v4_new(klass_ref, **attributes)
  klass = mml_v4_class_for(klass_ref)
  coerce_mml_v4_collection_attributes!(klass, attributes)
  klass.new(**attributes, lutaml_register: mml_v4_context.id)
end

.mml_v4_with_content(instance, content) ⇒ Object



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

def mml_v4_with_content(instance, content)
  attributes = instance.to_hash.transform_keys(&:to_sym)
  mml_v4_new(
    instance.class,
    **attributes,
    mml_v4_content_attribute(instance) => content,
  )
end