Module: Duxml::ElementGuts

Includes:
Linkable
Defined in:
lib/con_duxml/duxml_ext/element.rb

Instance Method Summary collapse

Methods included from Linkable

#linked_by

Instance Method Details

#merge(pattern = nil, &block) ⇒ Object

Parameters:

  • pattern (several_variants) (defaults to: nil)

    if String/Symbol or array of such, differences between merged entities’ instance vars matching pattern are masked; if pattern is a hash, the key is the instance var, and the value becomes the new value for the merged entity

  • &block (block)

    groups nodes by &block then merges each group into a single row @see #chunk



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/con_duxml/duxml_ext/element.rb', line 23

def merge(pattern=nil, &block)
  self_clone = self.clone
  self_clone.nodes = []
  chunks = block_given? ? nodes.chunk(&block) : [nil, nodes.dup]
  chunks.each do |type, chunk|
    if chunk.size == 1
      self_clone.nodes << chunk.first
      next
    end

    merged_args, homogeneous = diff_attrs(chunk, pattern)
    if homogeneous
      self_clone.nodes << chunk.first.class.new(chunk.first.name, merged_args)
    else
      self_clone << chunk
    end
  end # chunks.each do |type, nodes|

  report :Merge, self_clone

  self_clone
end

#split(&block) ⇒ Element

Returns a duplicate element of this node initialized with each subset’s nodes.

Parameters:

  • &block (Block)

    calls Enumerable#chunk on this element’s nodes to group them by result of &block

Returns:

  • (Element)

    a duplicate element of this node initialized with each subset’s nodes



11
12
13
14
15
16
17
18
19
# File 'lib/con_duxml/duxml_ext/element.rb', line 11

def split(&block)
  chunks = nodes.chunk(&block)
  new_nodes = chunks.collect do |type, nodes|
    self.class.new(name, nodes)
  end

  report :Split, new_nodes
  new_nodes
end