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.



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

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

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


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

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

#append_native(element) ⇒ Object



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

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



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

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



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

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



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

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



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

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