module Duxml::ElementGuts

Public Instance Methods

activate() click to toggle source

@return [Array] instantiated copy of this Element

# 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) click to toggle source

@param pattern [several_variants] 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 @param &block [block] groups nodes by &block then merges each group into a single row @see chunk

# 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_name() click to toggle source
# File lib/con_duxml/duxml_ext/element.rb, line 32
def simple_name
  name.split(':').last
end
split(&block) click to toggle source

@param &block [Block] calls Enumerable#chunk on this element’s nodes to group them by result of &block @return [Element] a duplicate element of this node initialized with each subset’s nodes

# 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

Private Instance Methods

diff_attrs(chunk, pattern) click to toggle source
# File lib/con_duxml/duxml_ext/element.rb, line 62
def diff_attrs(chunk, pattern)
  homogeneous = true
  merged_args = {}
  chunk.each do |node|
    node.attributes.each do |attr, val|
      case
        when pattern && pattern.is_a?(Hash)
          pattern.each do |k,v|
            if attr.match(k.to_s).to_s == attr
              merged_args[k] = v
            else
              merged_args[attr] ||= val
            end
          end
        when pattern && pattern.is_a?(Array)
          pattern.each do |p|
            merged_args.merge! diff_attrs(chunk, p) if matches?(attr, p)
          end
        when pattern && matches?(attr, pattern)   then merged_args[attr] ||= val
        when pattern && !matches?(attr, pattern)  then merged_args[attr] ||= val
        when chunk.any? do |n| val != n[attr] end then homogeneous = false; next
        else
      end
    end
  end
  return merged_args, homogeneous
end
matches?(attr, pattern) click to toggle source

TODO add !pattern or at least rewrite to work with XML

# File lib/con_duxml/duxml_ext/element.rb, line 91
def matches?(attr, pattern)
  var = attr.to_s[0] == '@' ? attr.to_s[1..-1] : attr.to_s
  pattern = pattern.keys.collect do |k| k.to_s end if pattern.is_a?(Hash)
  var=='nodes' || pattern && var.match(pattern.to_s).to_s == var
end