Class: DNZ::NamespaceArray

Inherits:
Array
  • Object
show all
Defined in:
lib/dnz/namespace_array.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dnz/namespace_array.rb', line 62

def method_missing(symbol, *args, &block)
  method = symbol.to_s

  if method =~ /(.*)\?$/
    self[$1].any?
  elsif self.length == 1 && self.first.respond_to?(symbol)
    self.first.send(symbol, *args, &block)
  elsif args.empty?
    self[method]
  end
end

Instance Method Details

#[](index) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dnz/namespace_array.rb', line 23

def [](index)
  if index.is_a?(String)
    if (nodes = self.select{|node| node.namespace.prefix.downcase.to_s == index }).any?
      return NamespaceArray.new(nodes)
    end

    if (nodes = self.select{|node| node.name == index }).any?
      return NamespaceArray.new(nodes)
    end

    NamespaceArray.new
  else
    super
  end
end

#inspectObject



58
59
60
# File 'lib/dnz/namespace_array.rb', line 58

def inspect
  namespaces.collect {|namespace| '%s => [ %s ]' % [namespace, self[namespace].names.join(', ')] }.join(', ')
end

#namesObject

An array of all unique node names in this array.



44
45
46
# File 'lib/dnz/namespace_array.rb', line 44

def names
  map(&:name).map(&:downcase).uniq
end

#namespacesObject

An array of all unique namespaces in this array.



49
50
51
# File 'lib/dnz/namespace_array.rb', line 49

def namespaces
  map(&:namespace).map(&:prefix).map(&:to_s).map(&:downcase).uniq
end

#textObject

The combined text of all nodes in this array.



54
55
56
# File 'lib/dnz/namespace_array.rb', line 54

def text
  map(&:text).join
end

#typeObject



39
40
41
# File 'lib/dnz/namespace_array.rb', line 39

def type
  self['type']
end