relative-require 1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +34 -0
- data/lib/elative-require.rb +1 -0
- data/lib/relative-require.rb +31 -0
- data/test/fixtures/a.rb +4 -0
- data/test/fixtures/b/c.rb +4 -0
- data/test/fixtures/d.rb +2 -0
- data/test/fixtures/e.rb +1 -0
- data/test/test.rb +26 -0
- metadata +73 -0
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
relative-require.rb
|
2
|
+
===================
|
3
|
+
|
4
|
+
This module provides relative requires to the requiring file. This should solve
|
5
|
+
most of the uglyness of pushing to $LOAD_PATH and loading #expand_path-ed files.
|
6
|
+
|
7
|
+
Tested with ruby-1.8.7 and ruby-1.9.2
|
8
|
+
|
9
|
+
by Jonas Pfenniger <jonas@pfenniger.name>
|
10
|
+
|
11
|
+
Example project
|
12
|
+
---------------
|
13
|
+
|
14
|
+
This example shows that you don't have to mess with $LOAD_PATH anymore.
|
15
|
+
|
16
|
+
$ export RUBYOPT=-relative-require
|
17
|
+
|
18
|
+
lib/
|
19
|
+
* myproject.rb
|
20
|
+
* myproject/xyz.rb
|
21
|
+
test/
|
22
|
+
test.rb
|
23
|
+
|
24
|
+
in myproject.rb
|
25
|
+
require './myproject/xyz.rb'
|
26
|
+
|
27
|
+
in test.rb
|
28
|
+
require '../lib/myproject.rb'
|
29
|
+
|
30
|
+
Licence
|
31
|
+
-------
|
32
|
+
|
33
|
+
This project is under public licence. http://unlicence.org
|
34
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
require File.expand_path('../relative-require', __FILE__)
|
@@ -0,0 +1,31 @@
|
|
1
|
+
$:.reject! do |x|
|
2
|
+
x == '.'
|
3
|
+
end
|
4
|
+
|
5
|
+
module Kernel
|
6
|
+
##
|
7
|
+
# The Kernel#require from before Relative was loaded.
|
8
|
+
alias rel_original_require require
|
9
|
+
|
10
|
+
##
|
11
|
+
# When relative is required, Kernel#require is replaces with
|
12
|
+
# our own which is capable of loading files relatively.
|
13
|
+
#
|
14
|
+
# The meaning of ./ is replaced with "relative to the directory
|
15
|
+
# of the file that has called require"
|
16
|
+
#
|
17
|
+
# The +path+ argument also accepts a tuple (array of two elements)
|
18
|
+
# in case the path is a variable that is going to required later,
|
19
|
+
# in another file. In that case, the second argument to the tuple
|
20
|
+
# is the __FILE__ where the variable was defined.
|
21
|
+
def require(path) # :doc:
|
22
|
+
if path.kind_of? Array
|
23
|
+
dir = File.dirname(path[1])
|
24
|
+
path = File.expand_path File.join(dir, path[0])
|
25
|
+
elsif /^\.\.?\// =~ path
|
26
|
+
dir = File.dirname caller[0].sub(/:\d+:in.*/, '')
|
27
|
+
path = File.expand_path File.join(dir, path)
|
28
|
+
end
|
29
|
+
rel_original_require path
|
30
|
+
end
|
31
|
+
end
|
data/test/fixtures/a.rb
ADDED
data/test/fixtures/d.rb
ADDED
data/test/fixtures/e.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
E = true
|
data/test/test.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
$:.reject{|x| x=='.'}
|
2
|
+
$:.unshift File.expand_path('../../lib', __FILE__)
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
|
6
|
+
class TestRelative < Test::Unit::TestCase
|
7
|
+
def test_that_it_works?
|
8
|
+
assert_raise(LoadError) do
|
9
|
+
require './fixtures/a'
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'relative-require'
|
13
|
+
|
14
|
+
assert_nothing_raised() do
|
15
|
+
require './fixtures/a'
|
16
|
+
end
|
17
|
+
|
18
|
+
assert(defined? C)
|
19
|
+
assert(defined? D)
|
20
|
+
|
21
|
+
assert_nothing_raised do
|
22
|
+
require ['./fixtures/e', __FILE__]
|
23
|
+
end
|
24
|
+
assert(defined? E)
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: relative-require
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 15
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: "1.0"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Jonas Pfenniger
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-11-27 00:00:00 +00:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: This module changes the meaning of relative requires from "relative to the current pwd" to "relative to the requiring file". By just changing this little thing, it lets you remove lots of uglyness regarding $LOAD_PATH
|
22
|
+
email: jonas@pfenniger.name
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- README.md
|
31
|
+
- lib/elative-require.rb
|
32
|
+
- lib/relative-require.rb
|
33
|
+
- test/fixtures/a.rb
|
34
|
+
- test/fixtures/b/c.rb
|
35
|
+
- test/fixtures/d.rb
|
36
|
+
- test/fixtures/e.rb
|
37
|
+
- test/test.rb
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: https://github.com/zimbatm/relative-require.rb
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
hash: 3
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.3.7
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Bringing relative requires to Ruby
|
72
|
+
test_files: []
|
73
|
+
|