Class: Threatinator::Parsers::XML::Node
- Inherits:
-
Object
- Object
- Threatinator::Parsers::XML::Node
- Defined in:
- lib/threatinator/parsers/xml/node.rb
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#text ⇒ Object
Returns the value of attribute text.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#[](name) ⇒ Array<Node>
An array containing all the child nodes for the given name.
-
#child_names ⇒ Array<Symbol>
An array containing all the names of child elements.
- #eql?(other) ⇒ Boolean
-
#initialize(name, opts = {}) ⇒ Node
constructor
A new instance of Node.
-
#num_children ⇒ Integer
The number of children.
Constructor Details
#initialize(name, opts = {}) ⇒ Node
Returns a new instance of Node.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# 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 |
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
7 8 9 |
# File 'lib/threatinator/parsers/xml/node.rb', line 7 def attrs @attrs end |
#children ⇒ Object (readonly)
Returns the value of attribute children.
8 9 10 |
# File 'lib/threatinator/parsers/xml/node.rb', line 8 def children @children end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/threatinator/parsers/xml/node.rb', line 6 def name @name end |
#text ⇒ Object
Returns the value of attribute text.
5 6 7 |
# File 'lib/threatinator/parsers/xml/node.rb', line 5 def text @text end |
Instance Method Details
#==(other) ⇒ Object
39 40 41 42 43 44 |
# 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) ⇒ Array<Node>
Returns 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.
59 60 61 |
# File 'lib/threatinator/parsers/xml/node.rb', line 59 def [](name) @children[name.to_sym] || [] end |
#child_names ⇒ Array<Symbol>
Returns an array containing all the names of child elements.
64 65 66 |
# File 'lib/threatinator/parsers/xml/node.rb', line 64 def child_names @children.keys end |
#eql?(other) ⇒ Boolean
46 47 48 49 |
# File 'lib/threatinator/parsers/xml/node.rb', line 46 def eql?(other) other.kind_of?(self.class) && self == other end |
#num_children ⇒ Integer
Returns the number of children.
52 53 54 |
# File 'lib/threatinator/parsers/xml/node.rb', line 52 def num_children @children.values.inject(0) {|total, child_set| total + child_set.count} end |