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

#activateArray[Element]

Returns instantiated copy of this Element.

Returns:

  • (Array[Element])

    instantiated copy of this Element



22
23
24
25
26
27
28
29
30
# File 'lib/con_duxml/duxml_ext/element.rb', line 22

def activate
  if name_space == 'duxml'
    maudule = ConDuxml.const_get(simple_name.constantize)
    extend maudule
    activate
  else
    [clone]
  end
end

#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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/con_duxml/duxml_ext/element.rb', line 38

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

#simple_nameObject



32
33
34
# File 'lib/con_duxml/duxml_ext/element.rb', line 32

def simple_name
  name.split(':').last
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