Class: Threatinator::Parsers::XML::Path

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str_or_parts = nil) ⇒ Path

Returns a new instance of Path.

Parameters:

  • str_or_parts (String, Array, nil) (defaults to: nil)

    ([]) If set to a String, splits the string by ‘/’ into an array. If set to an Array, sets parts to a duplicate of the array. If set to nil or not specified, defaults to a new array.

Raises:

  • (TypeError)

    if something other than a String, Array, or nil is specified for str_or_parts.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/threatinator/parsers/xml/path.rb', line 13

def initialize(str_or_parts = nil)
  @parts = 
    case str_or_parts
    when ::String
      if str_or_parts.length == 0 or !str_or_parts.start_with?('/')
        raise ArgumentError.new('str_or_parts must be a String beginning with "/"')
      end
      r = str_or_parts.split('/')
      r.shift
      r
    when ::Array
      str_or_parts.dup
    when nil
      []
    else
      raise TypeError.new("Expected argument must be a String, Array, or nil")
    end
end

Instance Attribute Details

#partsObject (readonly)

Returns the value of attribute parts.



5
6
7
# File 'lib/threatinator/parsers/xml/path.rb', line 5

def parts
  @parts
end

Instance Method Details

#==(other) ⇒ Object



32
33
34
# File 'lib/threatinator/parsers/xml/path.rb', line 32

def ==(other)
  @parts == other.parts
end

#end_with?(other_path) ⇒ Boolean

length = 5

 0 1 2 3 4
/a/b/c/d/e
       0 1
      /d/e

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
# File 'lib/threatinator/parsers/xml/path.rb', line 46

def end_with?(other_path)
  return false if other_path.length > self.length
  return true if other_path.length == 0
  pos = length - other_path.length
  other_path.parts.each_with_index do |other_part, idx|
    return false unless @parts[(pos + idx)] == other_part
  end
  true
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/threatinator/parsers/xml/path.rb', line 36

def eql?(other)
  other.kind_of?(self.class) &&
    self == other
end

#lengthObject



64
65
66
# File 'lib/threatinator/parsers/xml/path.rb', line 64

def length
  @parts.length
end

#popObject



60
61
62
# File 'lib/threatinator/parsers/xml/path.rb', line 60

def pop
  @parts.pop
end

#push(name) ⇒ Object



56
57
58
# File 'lib/threatinator/parsers/xml/path.rb', line 56

def push(name)
  @parts.push(name)
end