Class: PathRelativeToCaller

Inherits:
Object
  • Object
show all
Defined in:
lib/relative/pathrelativetocaller.rb

Overview

An instance of this class represents a file path relative to the source file that created the instance.

For example, if the caller’s source file is:

/usr/bin/scripts/print_file.rb

then

path = PathRelativeToCaller.new("config/print_file.cfg")
puts path.relative
puts path.absolute

will output:

config/print_file.cfg
/usr/bin/scripts/config/print_file.cfg

PathRelativeToCaller instances evaluate to their absolute path expansions in core and standard I/O methods like File.open and IO.read, allowing code like this:

IO.read(PathRelativeToCaller.new("config/print_file.cfg"))

Constant Summary collapse

TO_PATH =

to_path is implemented so Pathname objects are usable with File.open,etc.

:to_path

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ PathRelativeToCaller

Description:

This initializes a new PathRelativeToCaller instance with path, a file system path relative to the calling file.

Parameters:

path

A file system path relative to the calling file



43
44
45
46
47
48
49
50
51
52
# File 'lib/relative/pathrelativetocaller.rb', line 43

def initialize(path)
  if(path.respond_to?(TO_PATH))
    @relative = path.__send__(TO_PATH)
  else
    @relative = path
  end

  @relative = @relative.dup()
  @absolute = File.expand_path_relative_to_caller(@relative)
end

Instance Attribute Details

#absoluteObject (readonly)

The absolute expansion of the relative_path attribute.



32
33
34
# File 'lib/relative/pathrelativetocaller.rb', line 32

def absolute
  @absolute
end

#relativeObject (readonly)

The path relative to the code that created the PathNameRelativeToCaller instance (the argument to new).



27
28
29
# File 'lib/relative/pathrelativetocaller.rb', line 27

def relative
  @relative
end

Instance Method Details

#to_sObject

Returns:

A String containing the absolute expansion of the relative path contained in self.



59
60
61
# File 'lib/relative/pathrelativetocaller.rb', line 59

def to_s
  return @absolute
end