relative 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +8 -0
- data/LICENSE +20 -0
- data/Manifest.txt +14 -0
- data/README.txt +117 -0
- data/Rakefile +15 -0
- data/lib/relative.rb +25 -0
- data/lib/relative/file.rb +49 -0
- data/lib/relative/kernel.rb +30 -0
- data/lib/relative/pathname.rb +31 -0
- data/lib/relative/pathrelativetocaller.rb +77 -0
- data/test/test_file.rb +30 -0
- data/test/test_kernel.rb +22 -0
- data/test/test_pathname.rb +30 -0
- data/test/test_pathrelativetocaller.rb +33 -0
- metadata +81 -0
data/History.txt
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
=== 1.0.0 / 2008-06-25
|
2
|
+
Initial release of relative. This release includes support for:
|
3
|
+
* constructing pathnames relative to the caller
|
4
|
+
(File.expand_path_relative_to_caller and
|
5
|
+
Pathname#expand_path_relative_to_caller)
|
6
|
+
* Passing pathnames relative to the caller into standard I/O methods
|
7
|
+
(PathRelativeToCaller)
|
8
|
+
* requiring files relative to the caller (Kernel#require_relative)
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Designing Patterns
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
'Software'), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
17
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
18
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
19
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
20
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest.txt
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
Manifest.txt
|
2
|
+
LICENSE
|
3
|
+
History.txt
|
4
|
+
README.txt
|
5
|
+
Rakefile
|
6
|
+
lib/relative.rb
|
7
|
+
lib/relative/file.rb
|
8
|
+
lib/relative/kernel.rb
|
9
|
+
lib/relative/pathname.rb
|
10
|
+
lib/relative/pathrelativetocaller.rb
|
11
|
+
test/test_file.rb
|
12
|
+
test/test_kernel.rb
|
13
|
+
test/test_pathname.rb
|
14
|
+
test/test_pathrelativetocaller.rb
|
data/README.txt
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
= relative
|
2
|
+
* Project Page: http://rubyforge.org/projects/relative/
|
3
|
+
* Documentation: http://relative.rubyforge.org/
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
The relative library enhances Ruby's core and standard libraries to support
|
7
|
+
naming, opening, and reading files relative to the Ruby file currently being
|
8
|
+
interpreted (the contents of the __ FILE __ identifier). This functionality is
|
9
|
+
especially useful in embedded Ruby (eruby, erb, erubis, etc.) where absolute
|
10
|
+
paths or paths relative to the interpreter's current working directory are
|
11
|
+
problematic (due to file system structures and working directories
|
12
|
+
varying across platforms and web servers).
|
13
|
+
|
14
|
+
== FEATURES
|
15
|
+
* The File.expand_path_relative_to_caller class method that
|
16
|
+
expands paths relative to the calling file (analogous to File.expand_path,
|
17
|
+
which expands paths relative to the interpreter's current working directory)
|
18
|
+
* The Kernel#require_relative instance method that allows Ruby
|
19
|
+
modules to be imported with paths relative to the calling file
|
20
|
+
* The Pathname#expand_path_relative_to_caller instance method that
|
21
|
+
returns a new Pathname object created by expanding the receiver Pathname
|
22
|
+
relative to the calling file (analogous to Pathname.expand_path, which
|
23
|
+
expands a Pathname relative to the interpreter's current working directory)
|
24
|
+
* The PathRelativeToCaller class, instances of which are constructed with a
|
25
|
+
path relative to the calling file and can be passed into all of the
|
26
|
+
core and standard Ruby I/O methods (IO.read, File.open, etc).
|
27
|
+
A PathRelativeToCaller instance expands the relative path with which it is
|
28
|
+
initialized and evaluates to the absolute path in I/O methods;
|
29
|
+
it makes the relative path accessible through an attribute accessor,
|
30
|
+
however.
|
31
|
+
|
32
|
+
== PROBLEMS:
|
33
|
+
None (known).
|
34
|
+
|
35
|
+
== SYNOPSIS:
|
36
|
+
======+examples/example.rb+:
|
37
|
+
#!/usr/bin/env ruby
|
38
|
+
require 'rubygems'
|
39
|
+
require 'relative'
|
40
|
+
|
41
|
+
relative_path = "../Rakefile"
|
42
|
+
|
43
|
+
print("====================================================================\n")
|
44
|
+
print("The absolute path of #{relative_path} (relative to #{__FILE__}):\n")
|
45
|
+
print("#{File.expand_path_relative_to_caller(relative_path)}\n")
|
46
|
+
print("====================================================================\n")
|
47
|
+
print("The Pathname of #{relative_path} (relative to #{__FILE__}):\n")
|
48
|
+
print("#{Pathname.new(relative_path).expand_path_relative_to_caller()}\n")
|
49
|
+
print("====================================================================\n")
|
50
|
+
print("The contents of #{relative_path} (relative to #{__FILE__}):\n")
|
51
|
+
print("#{IO.read(PathRelativeToCaller.new(relative_path))}")
|
52
|
+
print("====================================================================\n")
|
53
|
+
|
54
|
+
|
55
|
+
This example shows the use of File.expand_path_relative_to_caller to expand
|
56
|
+
+../Rakefile+ relative to the path of the example script,
|
57
|
+
+examples/example.rb+. It also illustrates how
|
58
|
+
Pathname#expand_path_relative_to_caller can return an expansion of
|
59
|
+
the receiver Pathname (+../Rakefile+) relative to +examples/example.rb+.
|
60
|
+
Finally, it demonstrates that a PathRelativeToCaller object can be passed
|
61
|
+
into ordinary File and IO methods, within which it will evaluate to an
|
62
|
+
absolute expansion of +../Rakefile+ (the path with which it was initialized)
|
63
|
+
relative to +examples/example.rb+ (the source file that constructed the
|
64
|
+
PathRelativeToCaller instance).
|
65
|
+
|
66
|
+
== REQUIREMENTS:
|
67
|
+
Hoe is required but only for running the tests.
|
68
|
+
|
69
|
+
== INSTALL:
|
70
|
+
sudo gem install relative
|
71
|
+
|
72
|
+
== AUTHORS:
|
73
|
+
=== Designing Patterns
|
74
|
+
* Homepage: http://www.designingpatterns.com
|
75
|
+
* Blogs: http://blogs.designingpatterns.com
|
76
|
+
|
77
|
+
== SUPPORT
|
78
|
+
Please post questions, concerns, or requests for enhancement to the forums on
|
79
|
+
the project page. Alternatively, direct contact information for
|
80
|
+
Designing Patterns can be found on the project page for this gem.
|
81
|
+
|
82
|
+
== ENHANCEMENTS
|
83
|
+
Please feel free to contact us with any ideas; we will try our best to
|
84
|
+
enhance the software and respond to user requests. Of course, we are more
|
85
|
+
likely to work on a particular enhancement if we know that there are users
|
86
|
+
who want it. Designing Patterns provides contracting and consulting services,
|
87
|
+
so if there is an enhancement that *must* get done (and in a specified time
|
88
|
+
frame), please inquire about retaining our services!
|
89
|
+
|
90
|
+
== LICENSE:
|
91
|
+
The license text can be found in the +LICENSE+ file at the root of the
|
92
|
+
distribution.
|
93
|
+
|
94
|
+
This package is licensed with an MIT license:
|
95
|
+
|
96
|
+
Copyright (c) 2008 Designing Patterns
|
97
|
+
|
98
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
99
|
+
a copy of this software and associated documentation files (the
|
100
|
+
'Software'), to deal in the Software without restriction, including
|
101
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
102
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
103
|
+
permit persons to whom the Software is furnished to do so, subject to
|
104
|
+
the following conditions:
|
105
|
+
|
106
|
+
The above copyright notice and this permission notice shall be
|
107
|
+
included in all copies or substantial portions of the Software.
|
108
|
+
|
109
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
110
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
111
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
112
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
113
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
114
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
115
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
116
|
+
|
117
|
+
== SHARE AND ENJOY!
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift("lib")
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
require 'hoe'
|
7
|
+
|
8
|
+
$stderr = STDERR
|
9
|
+
|
10
|
+
Hoe.new('relative', "1.0.0") do |p|
|
11
|
+
p.remote_rdoc_dir = ''
|
12
|
+
p.developer('DesigningPatterns', 'technical.inquiries@designingpatterns.com')
|
13
|
+
end
|
14
|
+
|
15
|
+
# vim: syntax=Ruby
|
data/lib/relative.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
#
|
2
|
+
# Require the 'relative' components corresponding to Ruby core
|
3
|
+
# libraries.
|
4
|
+
#
|
5
|
+
require 'relative/file'
|
6
|
+
require 'relative/kernel'
|
7
|
+
require 'relative/pathrelativetocaller'
|
8
|
+
|
9
|
+
#
|
10
|
+
# Only require the 'relative' components corresponding to Ruby
|
11
|
+
# standard libraries if the associated standard library module is
|
12
|
+
# loaded. This trick allows
|
13
|
+
# require 'relative'
|
14
|
+
# to extend the core and standard libraries but not to include any
|
15
|
+
# ununsed standard libraries.
|
16
|
+
#
|
17
|
+
# autoload does not work as one might expect... if the module already is
|
18
|
+
# defined, autoload will *not* load the files. So, directly load the files if
|
19
|
+
# the module already is defined.
|
20
|
+
#
|
21
|
+
if(!Object.const_defined?(:Pathname))
|
22
|
+
autoload :Pathname, 'relative/pathname'
|
23
|
+
else
|
24
|
+
require 'relative/pathname'
|
25
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
class File
|
2
|
+
#
|
3
|
+
# Compute this information (the directory containing the relative gem's
|
4
|
+
# source code) when this file is loaded; this information is used within
|
5
|
+
# File#expand_path_relative_to_caller.
|
6
|
+
#
|
7
|
+
RELATIVE_GEM_SOURCE_DIR = File.expand_path(File.dirname(__FILE__)) #:nodoc:
|
8
|
+
|
9
|
+
#
|
10
|
+
# ====Description:
|
11
|
+
# This method returns an absolute expansion of file_name, relative to
|
12
|
+
# the caller's source file. Note that this method is a no-op if
|
13
|
+
# file_name is absolute.
|
14
|
+
#
|
15
|
+
# ====Examples:
|
16
|
+
# If the caller's source file is:
|
17
|
+
# /usr/bin/scripts/print_file.rb
|
18
|
+
# then
|
19
|
+
# File.expand_path_relative_to_caller("config/print_file.cfg")
|
20
|
+
# will return
|
21
|
+
# "/usr/bin/scripts/config/print_file.cfg"
|
22
|
+
#
|
23
|
+
# ====Parameters:
|
24
|
+
# [file_name]
|
25
|
+
# A file system path relative to the calling file
|
26
|
+
#
|
27
|
+
# ====Returns:
|
28
|
+
# An absolute expansion of file_name, relative to the caller's source file.
|
29
|
+
#
|
30
|
+
def self.expand_path_relative_to_caller(file_name)
|
31
|
+
caller_stack = caller(1) # Start the call stack at the caller
|
32
|
+
external_caller_file_dir = caller_stack.each do |caller_stack_frame|
|
33
|
+
caller_file_name = caller_stack_frame.split(":")[0]
|
34
|
+
caller_file_dir = File.expand_path(File.dirname(caller_file_name))
|
35
|
+
|
36
|
+
#
|
37
|
+
# Treat callers inside the relative gem differently from those
|
38
|
+
# outside the relative gem. In particular, assume that
|
39
|
+
# callers within the relative gem want paths expanded relative
|
40
|
+
# to *their* callers.
|
41
|
+
#
|
42
|
+
if(caller_file_dir != RELATIVE_GEM_SOURCE_DIR)
|
43
|
+
break caller_file_dir
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
return File.expand_path(file_name, external_caller_file_dir)
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'relative/file'
|
2
|
+
|
3
|
+
module Kernel
|
4
|
+
#
|
5
|
+
# Only implement require_relative if it is not implemented
|
6
|
+
# already.
|
7
|
+
#
|
8
|
+
if(!Kernel.method_defined?(:require_relative))
|
9
|
+
#
|
10
|
+
# ====Description:
|
11
|
+
# This method loads the Ruby code contained in file_name, where
|
12
|
+
# file_name is expanded relative to the calling source file.
|
13
|
+
# Basically, this method is exactly the same as the require method
|
14
|
+
# except that it interprets its argument relative to the calling
|
15
|
+
# source file rather than to the interpreter's current working
|
16
|
+
# directory. An equivalent require_relative method will be
|
17
|
+
# provided as part of the core Ruby 1.9 distribution (see
|
18
|
+
# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/16356;
|
19
|
+
# if a require_relative already is defined, this method will not
|
20
|
+
# override the existing implementation).
|
21
|
+
#
|
22
|
+
# ====Parameters:
|
23
|
+
# [file_name]
|
24
|
+
# The file system path of the Ruby source file to load
|
25
|
+
#
|
26
|
+
def require_relative(file_name)
|
27
|
+
require(File.expand_path_relative_to_caller(file_name))
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'relative/file'
|
2
|
+
|
3
|
+
require 'pathname'
|
4
|
+
|
5
|
+
class Pathname
|
6
|
+
#
|
7
|
+
# ====Description:
|
8
|
+
# This method returns an absolute expansion of self, relative to
|
9
|
+
# the caller's source file. Note that this method is a no-op if
|
10
|
+
# self is an absolute Pathname.
|
11
|
+
#
|
12
|
+
# ====Examples:
|
13
|
+
# If the caller's source file is:
|
14
|
+
# /usr/bin/scripts/print_file.rb
|
15
|
+
# then
|
16
|
+
# relative_path = Pathname.new("config/print_file.cfg")
|
17
|
+
# absolute_path = relative_path.expand_path_relative_to_caller()
|
18
|
+
# will make absolute_path equal to:
|
19
|
+
# Pathname.new("/usr/bin/scripts/config/print_file.cfg")
|
20
|
+
#
|
21
|
+
# ====Parameters:
|
22
|
+
# [file_name]
|
23
|
+
# A file system path relative to the calling file
|
24
|
+
#
|
25
|
+
# ====Returns:
|
26
|
+
# An absolute expansion of self, relative to the caller's source file.
|
27
|
+
#
|
28
|
+
def expand_path_relative_to_caller
|
29
|
+
return Pathname.new(File.expand_path_relative_to_caller(to_s()))
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'relative/file'
|
2
|
+
|
3
|
+
#
|
4
|
+
# An instance of this class represents a file path relative to the
|
5
|
+
# source file that created the instance.
|
6
|
+
#
|
7
|
+
# For example, if the caller's source file is:
|
8
|
+
# /usr/bin/scripts/print_file.rb
|
9
|
+
# then
|
10
|
+
# path = PathRelativeToCaller.new("config/print_file.cfg")
|
11
|
+
# puts path.relative
|
12
|
+
# puts path.absolute
|
13
|
+
# will output:
|
14
|
+
# config/print_file.cfg
|
15
|
+
# /usr/bin/scripts/config/print_file.cfg
|
16
|
+
#
|
17
|
+
# PathRelativeToCaller instances evaluate to their absolute path expansions
|
18
|
+
# in core and standard I/O methods like File.open and IO.read, allowing
|
19
|
+
# code like this:
|
20
|
+
# IO.read(PathRelativeToCaller.new("config/print_file.cfg"))
|
21
|
+
#
|
22
|
+
class PathRelativeToCaller
|
23
|
+
#
|
24
|
+
# The path relative to the code that created the PathNameRelativeToCaller
|
25
|
+
# instance (the argument to new).
|
26
|
+
#
|
27
|
+
attr_reader :relative
|
28
|
+
|
29
|
+
#
|
30
|
+
# The absolute expansion of the relative_path attribute.
|
31
|
+
#
|
32
|
+
attr_reader :absolute
|
33
|
+
|
34
|
+
#
|
35
|
+
# ====Description:
|
36
|
+
# This initializes a new PathRelativeToCaller instance with
|
37
|
+
# path, a file system path relative to the calling file.
|
38
|
+
#
|
39
|
+
# ====Parameters:
|
40
|
+
# [path]
|
41
|
+
# A file system path relative to the calling file
|
42
|
+
#
|
43
|
+
def initialize(path)
|
44
|
+
if(path.respond_to?(TO_PATH))
|
45
|
+
@relative = path.__send__(TO_PATH)
|
46
|
+
else
|
47
|
+
@relative = path
|
48
|
+
end
|
49
|
+
|
50
|
+
@relative = @relative.dup()
|
51
|
+
@absolute = File.expand_path_relative_to_caller(@relative)
|
52
|
+
end
|
53
|
+
|
54
|
+
#
|
55
|
+
# ====Returns:
|
56
|
+
# A String containing the absolute expansion of the
|
57
|
+
# relative path contained in self.
|
58
|
+
#
|
59
|
+
def to_s
|
60
|
+
return @absolute
|
61
|
+
end
|
62
|
+
|
63
|
+
#:stopdoc:
|
64
|
+
# This block was ripped from pathname.rb in the Ruby Standard library.
|
65
|
+
# Basically, PathNameRelativeToCaller will define a to_path method if
|
66
|
+
# being interpreted by Ruby 1.9 and a to_str method otherwise. to_path
|
67
|
+
# (or to_str) must be defined in order for PathNameRelativeToCaller instances
|
68
|
+
# to be interchangeable with strings as arguments to Ruby I/O methods.
|
69
|
+
if RUBY_VERSION < "1.9"
|
70
|
+
TO_PATH = :to_str
|
71
|
+
else
|
72
|
+
# to_path is implemented so Pathname objects are usable with File.open,etc.
|
73
|
+
TO_PATH = :to_path
|
74
|
+
end
|
75
|
+
alias_method(TO_PATH, :to_s)
|
76
|
+
#:startdoc:
|
77
|
+
end
|
data/test/test_file.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'relative/file'
|
4
|
+
require 'relative/pathrelativetocaller'
|
5
|
+
|
6
|
+
require 'test/unit'
|
7
|
+
|
8
|
+
class RelativeFileTest < Test::Unit::TestCase
|
9
|
+
def expand_path_relative_to_caller_trial(relative_path)
|
10
|
+
absolute_path = File.expand_path_relative_to_caller(relative_path)
|
11
|
+
|
12
|
+
expected_absolute_path = File.expand_path(File.join(File.dirname(__FILE__), relative_path))
|
13
|
+
|
14
|
+
assert_equal(expected_absolute_path, absolute_path)
|
15
|
+
|
16
|
+
#
|
17
|
+
# Expanding an absolute path relative to the caller should be
|
18
|
+
# a no-op.
|
19
|
+
#
|
20
|
+
absolute_path = File.expand_path_relative_to_caller(expected_absolute_path)
|
21
|
+
assert_equal(expected_absolute_path, absolute_path)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_expand_path_relative_to_caller
|
25
|
+
expand_path_relative_to_caller_trial(".")
|
26
|
+
expand_path_relative_to_caller_trial("..")
|
27
|
+
expand_path_relative_to_caller_trial("../lib")
|
28
|
+
expand_path_relative_to_caller_trial(File.basename(__FILE__))
|
29
|
+
end
|
30
|
+
end
|
data/test/test_kernel.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'relative/kernel'
|
4
|
+
|
5
|
+
require 'test/unit'
|
6
|
+
|
7
|
+
class RelativeKernelTest < Test::Unit::TestCase
|
8
|
+
def require_relative_trial(relative_path)
|
9
|
+
require_relative(relative_path)
|
10
|
+
|
11
|
+
#
|
12
|
+
# require_relative also should handle absolute paths
|
13
|
+
# correctly.
|
14
|
+
#
|
15
|
+
absolute_path = File.expand_path(File.join(File.dirname(__FILE__), relative_path))
|
16
|
+
require_relative(absolute_path)
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_require_relative
|
20
|
+
require_relative_trial('../lib/relative/kernel')
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'relative/pathname'
|
4
|
+
require 'relative/pathrelativetocaller'
|
5
|
+
|
6
|
+
require 'test/unit'
|
7
|
+
|
8
|
+
class RelativePathnameTest < Test::Unit::TestCase
|
9
|
+
def expand_path_relative_to_caller_trial(relative_path)
|
10
|
+
absolute_path = relative_path.expand_path_relative_to_caller()
|
11
|
+
|
12
|
+
expected_absolute_path = (Pathname(__FILE__).dirname() + relative_path).expand_path()
|
13
|
+
|
14
|
+
assert_equal(expected_absolute_path, absolute_path)
|
15
|
+
|
16
|
+
#
|
17
|
+
# Expanding an absolute path relative to the caller should be
|
18
|
+
# a no-op.
|
19
|
+
#
|
20
|
+
absolute_path = expected_absolute_path.expand_path_relative_to_caller()
|
21
|
+
assert_equal(expected_absolute_path, absolute_path)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_expand_path_relative_to_caller
|
25
|
+
expand_path_relative_to_caller_trial(Pathname("."))
|
26
|
+
expand_path_relative_to_caller_trial(Pathname(".."))
|
27
|
+
expand_path_relative_to_caller_trial(Pathname(PathRelativeToCaller.new("../lib")))
|
28
|
+
expand_path_relative_to_caller_trial(Pathname(File.basename(__FILE__)))
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'relative/pathrelativetocaller'
|
4
|
+
|
5
|
+
class PathRelativeToCallerTest < Test::Unit::TestCase
|
6
|
+
def path_trial(relative_path, expected_absolute_path)
|
7
|
+
path = PathRelativeToCaller.new(relative_path)
|
8
|
+
assert_equal(relative_path, path.relative)
|
9
|
+
assert_equal(expected_absolute_path, path.absolute)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_path_expansion
|
13
|
+
path_trial(File.basename(__FILE__), File.expand_path(__FILE__))
|
14
|
+
path_trial("../Rakefile",
|
15
|
+
File.expand_path(File.join(File.dirname(__FILE__), "../Rakefile")))
|
16
|
+
path_trial(File.expand_path(__FILE__), File.expand_path(__FILE__))
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_io_operations
|
20
|
+
#
|
21
|
+
# Verify that PathRelativeToCaller correctly can be passed into
|
22
|
+
# the core I/O methods.
|
23
|
+
#
|
24
|
+
assert_equal(IO.read(File.expand_path(File.join(File.dirname(__FILE__), "../Rakefile"))),
|
25
|
+
IO.read(PathRelativeToCaller.new("../Rakefile")))
|
26
|
+
|
27
|
+
File.open(PathRelativeToCaller.new(File.basename(__FILE__))) do |rel_file|
|
28
|
+
File.open(__FILE__) do |abs_file|
|
29
|
+
assert_equal(abs_file.read(), rel_file.read())
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: relative
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- DesigningPatterns
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-06-26 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.6.0
|
23
|
+
version:
|
24
|
+
description: The relative library enhances Ruby's core and standard libraries to support naming, opening, and reading files relative to the Ruby file currently being interpreted (the contents of the __ FILE __ identifier). This functionality is especially useful in embedded Ruby (eruby, erb, erubis, etc.) where absolute paths or paths relative to the interpreter's current working directory are problematic (due to file system structures and working directories varying across platforms and web servers).
|
25
|
+
email:
|
26
|
+
- technical.inquiries@designingpatterns.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- Manifest.txt
|
33
|
+
- History.txt
|
34
|
+
- README.txt
|
35
|
+
files:
|
36
|
+
- Manifest.txt
|
37
|
+
- LICENSE
|
38
|
+
- History.txt
|
39
|
+
- README.txt
|
40
|
+
- Rakefile
|
41
|
+
- lib/relative.rb
|
42
|
+
- lib/relative/file.rb
|
43
|
+
- lib/relative/kernel.rb
|
44
|
+
- lib/relative/pathname.rb
|
45
|
+
- lib/relative/pathrelativetocaller.rb
|
46
|
+
- test/test_file.rb
|
47
|
+
- test/test_kernel.rb
|
48
|
+
- test/test_pathname.rb
|
49
|
+
- test/test_pathrelativetocaller.rb
|
50
|
+
has_rdoc: true
|
51
|
+
homepage: "Project Page: http://rubyforge.org/projects/relative/"
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options:
|
54
|
+
- --main
|
55
|
+
- README.txt
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
version:
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project: relative
|
73
|
+
rubygems_version: 1.0.1
|
74
|
+
signing_key:
|
75
|
+
specification_version: 2
|
76
|
+
summary: The relative library enhances Ruby's core and standard libraries to support naming, opening, and reading files relative to the Ruby file currently being interpreted (the contents of the __ FILE __ identifier)
|
77
|
+
test_files:
|
78
|
+
- test/test_pathrelativetocaller.rb
|
79
|
+
- test/test_pathname.rb
|
80
|
+
- test/test_file.rb
|
81
|
+
- test/test_kernel.rb
|