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.

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>



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

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

#child_namesArray<Symbol>



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_childrenInteger



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