class Threatinator::Parsers::XML::Node
Attributes
attrs[R]
children[R]
name[R]
text[RW]
Public Class Methods
new(name, opts = {})
click to toggle source
@param [String, Symbol] name @param [Hash] opts @option opts [String] :text The text @option opts [Hash] :attrs The attributes @option opts [Array<Threatinator::Parsers::XML::Node>] :children An array
of child child nodes that belong to this node.
# File lib/threatinator/parsers/xml/node.rb, line 16 def initialize(name, opts = {}) unless name.kind_of?(::Symbol) or name.kind_of?(::String) raise TypeError.new("name must be a String or a Symbol") end @name = name.to_sym @text = opts.delete(:text) || "" unless @text.kind_of?(::String) raise TypeError.new(":text must be a String") end @attrs = opts.delete(:attrs) || {} unless @attrs.kind_of?(::Hash) raise TypeError.new(":text must be a Hash") end @children = {} if _children = opts.delete(:children) _children.each do |child| add_child(child) end end end
Public Instance Methods
==(other)
click to toggle source
# File lib/threatinator/parsers/xml/node.rb, line 39 def ==(other) @name == other.name && @attrs == other.attrs && @text == other.text && @children == other.children end
[](name)
click to toggle source
@param [String, Symbol] name The name of the child element @return [Array<Node>] An array containing all the child nodes for the given
name. The array will be empty if there are no children by the given name.
# File lib/threatinator/parsers/xml/node.rb, line 59 def [](name) @children[name.to_sym] || [] end
child_names()
click to toggle source
@return [Array<Symbol>] an array containing all the names of child elements
# File lib/threatinator/parsers/xml/node.rb, line 64 def child_names @children.keys end
eql?(other)
click to toggle source
# File lib/threatinator/parsers/xml/node.rb, line 46 def eql?(other) other.kind_of?(self.class) && self == other end
num_children()
click to toggle source
@return [Integer] the number of children
# File lib/threatinator/parsers/xml/node.rb, line 52 def num_children @children.values.inject(0) {|total, child_set| total + child_set.count} end
Private Instance Methods
add_child(child)
click to toggle source
# File lib/threatinator/parsers/xml/node.rb, line 69 def add_child(child) name = child.name unless child_set = @children[name] child_set = @children[name] = [] end child_set << child end