Class: StringDoc::MetaNode Private

Inherits:
Object
  • Object
show all
Defined in:
lib/string_doc/meta_node.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Lets two or more nodes to be represented as a single node in a doc, then manipulated together.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nodes) ⇒ MetaNode

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of MetaNode.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/string_doc/meta_node.rb', line 13

def initialize(nodes)
  # Reparent nodes that belong to the same parent.
  #
  nodes.group_by { |node| node.parent }.each_pair do |parent, children|
    # If the children already belong to a meta node doc, don't reparent them again.
    #
    unless children.first.labeled?(:__meta_node)
      parent.replace_node(children.first, self)
    end

    children[1..-1].each do |node|
      # Remove the node, but don't make it appear to have been removed for transforms.
      #
      node.remove(false, false)
    end
  end

  nodes.each do |node|
    node.set_label(:__meta_node, true)
  end

  @doc = StringDoc.from_nodes(nodes)
  @transforms = { high: [], default: [], low: [] }

  @internal_nodes = nodes.dup

  @pipeline = nil
end

Instance Attribute Details

#docObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
# File 'lib/string_doc/meta_node.rb', line 11

def doc
  @doc
end

#internal_nodesObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
# File 'lib/string_doc/meta_node.rb', line 11

def internal_nodes
  @internal_nodes
end

#transformsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
# File 'lib/string_doc/meta_node.rb', line 11

def transforms
  @transforms
end

Instance Method Details

#after(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



198
199
200
# File 'lib/string_doc/meta_node.rb', line 198

def after(node)
  @doc.append(node)
end

#append(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



206
207
208
209
210
# File 'lib/string_doc/meta_node.rb', line 206

def append(node)
  internal_nodes.each do |each_node|
    each_node.append(node)
  end
end

#append_html(html) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



212
213
214
215
216
# File 'lib/string_doc/meta_node.rb', line 212

def append_html(html)
  internal_nodes.each do |each_node|
    each_node.append_html(html)
  end
end

#attributesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



121
122
123
# File 'lib/string_doc/meta_node.rb', line 121

def attributes
  MetaAttributes.new(internal_nodes.map(&:attributes))
end

#before(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



202
203
204
# File 'lib/string_doc/meta_node.rb', line 202

def before(node)
  @doc.prepend(node)
end

#childrenObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



112
113
114
115
116
117
118
119
# File 'lib/string_doc/meta_node.rb', line 112

def children
  internal_nodes.each_with_object(StringDoc.empty) { |node, children|
    case node.children
    when StringDoc
      children.nodes.concat(node.children.nodes)
    end
  }
end

#clearObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



194
195
196
# File 'lib/string_doc/meta_node.rb', line 194

def clear
  internal_nodes.each(&:clear)
end

#delete_label(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



246
247
248
249
250
# File 'lib/string_doc/meta_node.rb', line 246

def delete_label(name)
  internal_nodes.each do |each_node|
    each_node.delete_label(name)
  end
end

#each(descend: false) {|_self| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Yields:

  • (_self)

Yield Parameters:



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/string_doc/meta_node.rb', line 252

def each(descend: false, &block)
  return enum_for(:each, descend: descend) unless block_given?

  yield self

  nodes.each do |node|
    # Yield each node that isn't an internal node (e.g. added before/after).
    #
    unless @internal_nodes.any? { |internal_node| internal_node.equal?(node) }
      case node
      when MetaNode
        node.each do |each_meta_node|
          yield each_meta_node
        end
      else
        yield node
      end
    end
  end
end

#each_significant_node(type, descend: false, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



273
274
275
276
277
278
279
# File 'lib/string_doc/meta_node.rb', line 273

def each_significant_node(type, descend: false, &block)
  return enum_for(:each_significant_node, type, descend: descend) unless block_given?

  internal_nodes.each do |node|
    node.each_significant_node(type, descend: descend, &block)
  end
end

#each_significant_node_with_name(type, name, descend: false, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



289
290
291
292
293
294
295
# File 'lib/string_doc/meta_node.rb', line 289

def each_significant_node_with_name(type, name, descend: false, &block)
  return enum_for(:each_significant_node_with_name, type, name, descend: descend) unless block_given?

  internal_nodes.each do |node|
    node.each_significant_node_with_name(type, name, descend: descend, &block)
  end
end

#each_significant_node_without_descending_into_type(type, descend: false, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



281
282
283
284
285
286
287
# File 'lib/string_doc/meta_node.rb', line 281

def each_significant_node_without_descending_into_type(type, descend: false, &block)
  return enum_for(:each_significant_node_without_descending_into_type, type, descend: descend) unless block_given?

  internal_nodes.each do |node|
    node.each_significant_node_without_descending_into_type(type, descend: descend, &block)
  end
end

#finalize_labels(keep: []) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



91
92
93
94
95
# File 'lib/string_doc/meta_node.rb', line 91

def finalize_labels(keep: [])
  nodes.each do |node|
    node.finalize_labels(keep: keep)
  end
end

#find_first_significant_node(type, descend: false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



297
298
299
300
301
302
303
304
305
# File 'lib/string_doc/meta_node.rb', line 297

def find_first_significant_node(type, descend: false)
  internal_nodes.each do |node|
    if found = node.find_first_significant_node(type, descend: descend)
      return found
    end
  end

  nil
end

#find_significant_nodes(type, descend: false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



307
308
309
310
311
# File 'lib/string_doc/meta_node.rb', line 307

def find_significant_nodes(type, descend: false)
  internal_nodes.each_with_object([]) { |node, collected|
    collected.concat(node.find_significant_nodes(type, descend: descend))
  }
end

#find_significant_nodes_with_name(type, name, descend: false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



313
314
315
316
317
# File 'lib/string_doc/meta_node.rb', line 313

def find_significant_nodes_with_name(type, name, descend: false)
  internal_nodes.each_with_object([]) { |node, collected|
    collected.concat(node.find_significant_nodes_with_name(type, name, descend: descend))
  }
end

#freezeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



97
98
99
100
# File 'lib/string_doc/meta_node.rb', line 97

def freeze(*)
  pipeline
  super
end

#htmlObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



174
175
176
# File 'lib/string_doc/meta_node.rb', line 174

def html
  internal_nodes[0].html
end

#html=(html) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



178
179
180
181
182
# File 'lib/string_doc/meta_node.rb', line 178

def html=(html)
  internal_nodes.each do |node|
    node.html = html
  end
end

#initialize_copy(_) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/string_doc/meta_node.rb', line 43

def initialize_copy(_)
  super

  nodes, internal_nodes = [], []
  @doc.nodes.each do |current_node|
    duped_node = current_node.dup
    nodes << duped_node

    if @internal_nodes.any? { |current_internal_node| current_internal_node.equal?(current_node) }
      internal_nodes << duped_node
    end
  end

  @doc = StringDoc.from_nodes(nodes)

  @transforms = @transforms.each_with_object({}) { |(key, value), hash|
    hash[key] = value.dup
  }

  @internal_nodes = internal_nodes

  @pipeline = nil
end

#label(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



224
225
226
227
228
229
230
# File 'lib/string_doc/meta_node.rb', line 224

def label(name)
  if node = internal_nodes.first
    node.label(name)
  else
    nil
  end
end

#labeled?(name) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


232
233
234
235
236
237
238
# File 'lib/string_doc/meta_node.rb', line 232

def labeled?(name)
  if node = internal_nodes.first
    node.labeled?(name)
  else
    false
  end
end

#next_transformObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



125
126
127
# File 'lib/string_doc/meta_node.rb', line 125

def next_transform
  pipeline.shift
end

#nodesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



108
109
110
# File 'lib/string_doc/meta_node.rb', line 108

def nodes
  @doc.nodes
end

#parent=(parent) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



103
104
105
# File 'lib/string_doc/meta_node.rb', line 103

def parent=(parent)
  @parent = parent
end

#prepend(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



218
219
220
221
222
# File 'lib/string_doc/meta_node.rb', line 218

def prepend(node)
  internal_nodes.each do |each_node|
    each_node.prepend(node)
  end
end

#remove(label = true, descend = true) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



162
163
164
165
166
167
168
# File 'lib/string_doc/meta_node.rb', line 162

def remove(label = true, descend = true)
  internal_nodes.each do |node|
    node.remove(label, descend)
  end

  @internal_nodes = []
end

#removed?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


319
320
321
# File 'lib/string_doc/meta_node.rb', line 319

def removed?
  internal_nodes.all?(&:removed?)
end

#render(output = String.new, context: nil) ⇒ Object Also known as: to_html, to_xml

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts the node to an xml string.



325
326
327
328
329
330
331
332
333
334
335
# File 'lib/string_doc/meta_node.rb', line 325

def render(output = String.new, context: nil)
  if transforms_itself?
    __transform(output, context: context)
  else
    nodes.each do |each_node|
      each_node.render(output, context: context)
    end
  end

  output
end

#replace(replacement) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



154
155
156
157
158
159
160
# File 'lib/string_doc/meta_node.rb', line 154

def replace(replacement)
  internal_nodes.each do |each_node|
    each_node.replace(replacement)
  end

  @internal_nodes = StringDoc.nodes_from_doc_or_string(replacement)
end

#replace_children(children) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



184
185
186
187
188
# File 'lib/string_doc/meta_node.rb', line 184

def replace_children(children)
  internal_nodes.each do |node|
    node.replace_children(children)
  end
end

#set_label(name, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



240
241
242
243
244
# File 'lib/string_doc/meta_node.rb', line 240

def set_label(name, value)
  internal_nodes.each do |each_node|
    each_node.set_label(name, value)
  end
end

#significance?(*types) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


148
149
150
151
152
# File 'lib/string_doc/meta_node.rb', line 148

def significance?(*types)
  internal_nodes.any? { |node|
    node.significance?(*types)
  }
end

#significant?(type = nil) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


142
143
144
145
146
# File 'lib/string_doc/meta_node.rb', line 142

def significant?(type = nil)
  internal_nodes.any? { |node|
    node.significant?(type)
  }
end

#soft_copyObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/string_doc/meta_node.rb', line 68

def soft_copy
  instance = self.class.allocate

  nodes, internal_nodes = [], []
  @doc.nodes.each do |current_node|
    duped_node = current_node.soft_copy
    nodes << duped_node

    if @internal_nodes.any? { |current_internal_node| current_internal_node.equal?(current_node) }
      internal_nodes << duped_node
    end
  end

  instance.instance_variable_set(:@doc, StringDoc.from_nodes(nodes))
  instance.instance_variable_set(:@transforms, @transforms)

  instance.instance_variable_set(:@internal_nodes, internal_nodes)

  instance.instance_variable_set(:@pipeline, @pipeline.dup)

  instance
end

#tagnameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



190
191
192
# File 'lib/string_doc/meta_node.rb', line 190

def tagname
  internal_nodes[0].tagname
end

#textObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



170
171
172
# File 'lib/string_doc/meta_node.rb', line 170

def text
  internal_nodes[0].text
end

#to_sObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the node as an xml string, without transforming.



341
342
343
344
345
# File 'lib/string_doc/meta_node.rb', line 341

def to_s
  nodes.each_with_object(String.new) do |node, string|
    string << node.to_s
  end
end

#transform(priority: :default, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



129
130
131
132
# File 'lib/string_doc/meta_node.rb', line 129

def transform(priority: :default, &block)
  @transforms[priority] << block
  @pipeline = nil
end

#transforms?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


134
135
136
# File 'lib/string_doc/meta_node.rb', line 134

def transforms?
  transforms_itself? || children.transforms?
end

#transforms_itself?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


138
139
140
# File 'lib/string_doc/meta_node.rb', line 138

def transforms_itself?
  pipeline.any?
end