Class: Threatinator::Parsers::XML::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/threatinator/parsers/xml/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ Node

Returns a new instance of Node.

Parameters:

  • name (String, Symbol)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :text (String)

    The text

  • :attrs (Hash)

    The attributes

  • :children (Array<Threatinator::Parsers::XML::Node>)

    An array of child child nodes that belong to this 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

#attrsObject (readonly)

Returns the value of attribute attrs.



7
8
9
# File 'lib/threatinator/parsers/xml/node.rb', line 7

def attrs
  @attrs
end

#childrenObject (readonly)

Returns the value of attribute children.



8
9
10
# File 'lib/threatinator/parsers/xml/node.rb', line 8

def children
  @children
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/threatinator/parsers/xml/node.rb', line 6

def name
  @name
end

#textObject

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.

Parameters:

  • name (String, Symbol)

    The name of the child element

Returns:

  • (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.



59
60
61
# File 'lib/threatinator/parsers/xml/node.rb', line 59

def [](name)
  @children[name.to_sym] || []
end

#child_namesArray<Symbol>

Returns an array containing all the names of child elements.

Returns:

  • (Array<Symbol>)

    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

Returns:

  • (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_childrenInteger

Returns the number of children.

Returns:

  • (Integer)

    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