Class: Moxml::Adapter::Libxml::EntityRefRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/moxml/adapter/libxml/entity_ref_registry.rb

Overview

Tracks entity-reference insertions that cannot live in LibXML’s native node tree, plus the child sequence needed to serialize them in order.

Instance Method Summary collapse

Constructor Details

#initialize(attachments, doc) ⇒ EntityRefRegistry

Returns a new instance of EntityRefRegistry.



15
16
17
18
# File 'lib/moxml/adapter/libxml/entity_ref_registry.rb', line 15

def initialize(attachments, doc)
  @attachments = attachments
  @doc = doc
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/moxml/adapter/libxml/entity_ref_registry.rb', line 20

def active?
  @doc ? @attachments.key?(@doc, ENTITY_REFS_KEY) : false
end

#append_native(element) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/moxml/adapter/libxml/entity_ref_registry.rb', line 45

def append_native(element)
  return unless @doc && element

  seq_by_path = @attachments.get(@doc, CHILD_SEQUENCE_KEY)
  return unless seq_by_path

  seq = seq_by_path[path_for(element)]
  return unless seq

  seq << :native
end

#refs_for(element) ⇒ Object



57
58
59
60
61
62
# File 'lib/moxml/adapter/libxml/entity_ref_registry.rb', line 57

def refs_for(element)
  return nil unless @doc && element

  refs_by_path = @attachments.get(@doc, ENTITY_REFS_KEY)
  refs_by_path && refs_by_path[path_for(element)]
end

#register(element, ref) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/moxml/adapter/libxml/entity_ref_registry.rb', line 24

def register(element, ref)
  return unless @doc && element

  path = path_for(element)

  refs_by_path = @attachments.get(@doc, ENTITY_REFS_KEY) || {}
  (refs_by_path[path] ||= []) << ref
  @attachments.set(@doc, ENTITY_REFS_KEY, refs_by_path)

  seq_by_path = @attachments.get(@doc, CHILD_SEQUENCE_KEY) || {}
  existing = seq_by_path[path]
  if existing
    existing << :eref
  else
    seq_by_path[path] =
      Array.new(count_native_children(element), :native)
    seq_by_path[path] << :eref
    @attachments.set(@doc, CHILD_SEQUENCE_KEY, seq_by_path)
  end
end

#sequence_for(element) ⇒ Object



64
65
66
67
68
69
# File 'lib/moxml/adapter/libxml/entity_ref_registry.rb', line 64

def sequence_for(element)
  return nil unless @doc && element

  seq_by_path = @attachments.get(@doc, CHILD_SEQUENCE_KEY)
  seq_by_path && seq_by_path[path_for(element)]
end

#serialization_for(element) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/moxml/adapter/libxml/entity_ref_registry.rb', line 71

def serialization_for(element)
  refs = refs_for(element)
  return [nil, nil] unless refs && !refs.empty?

  seq = sequence_for(element)
  return [nil, nil] unless seq

  [refs, seq]
end