Class: Ronin::Support::Path

Inherits:
Pathname
  • Object
show all
Defined in:
lib/ronin/support/path.rb

Overview

The Path class extends Pathname to allow representing directory traversal paths.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, separator = File::SEPARATOR) ⇒ Path

Initializes a new Path.

Parameters:

  • path (String)

    The path.

  • separator (String) (defaults to: File::SEPARATOR)

    The directory separator to use.



45
46
47
48
49
# File 'lib/ronin/support/path.rb', line 45

def initialize(path,separator=File::SEPARATOR)
  @separator = separator

  super(path)
end

Instance Attribute Details

#separatorString

The separator to join paths together with

Returns:



34
35
36
# File 'lib/ronin/support/path.rb', line 34

def separator
  @separator
end

Class Method Details

.rootPath

The root path.

Returns:

  • (Path)

    The root path.



57
58
59
# File 'lib/ronin/support/path.rb', line 57

def self.root
  new('/')
end

.up(n, separator = File::SEPARATOR) ⇒ Path

Creates a new path object for upward directory traversal.

Examples:

Generate a relative path that goes up 7 directories:

Path.up(7)
# => #<Ronin::Support::Path:../../../../../../..>

Generate multiple relative paths, going up 1 to 3 directories:

Path.up(1..3)
# => [#<Ronin::Support::Path:..>, #<Ronin::Support::Path:../..>,
#<Ronin::Support::Path:../../..>]

Parameters:

  • n (Integer, Array, Range)

    The number of directories to go up.

  • separator (String) (defaults to: File::SEPARATOR)

    Path separator.

Returns:

  • (Path)

    The new path object.

Raises:

  • (ArgumentError)

    A negative number was given as the first argument.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ronin/support/path.rb', line 85

def self.up(n,separator=File::SEPARATOR)
  case n
  when Integer
    if n == 0
      separator
    elsif n > 0
      path = new('..',separator)
      path.join(*(['..'] * (n - 1)))
    else
      raise(ArgumentError,"negative argument")
    end
  when Enumerable
    n.map { |i| up(i) }
  else
    raise(ArgumentError,"The first argument of Path.up must be either an Integer or Enumerable")
  end
end

Instance Method Details

#join(*names) ⇒ Path Also known as: /

Joins directory names together with the path, but does not resolve the resulting path.

Examples:

Path.up(7).join('etc/passwd')
# => #<Ronin::Support::Path:../../../../../../../etc/passwd>

Parameters:

  • names (Array)

    The names to join together.

Returns:

  • (Path)

    The joined path.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/ronin/support/path.rb', line 117

def join(*names)
  joined_path = if root? then String.new(encoding: Encoding::UTF_8)
                else          self.to_s
                end

  names.each do |name|
    name = name.to_s

    joined_path << @separator unless name.start_with?(@separator)
    joined_path << name       unless name == @separator
  end

  return self.class.new(joined_path,@separator)
end