el_req 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +4 -0
- data/MIT-LICENSE +23 -0
- data/Manifest.txt +15 -0
- data/README.txt +42 -0
- data/Rakefile +31 -0
- data/lib/file_extensions.rb +6 -0
- data/lib/kernel_extensions.rb +42 -0
- data/lib/relative_require.rb +5 -0
- data/spec/file_extensions_spec.rb +22 -0
- data/spec/kernel_extensions_spec.rb +78 -0
- data/spec/lib/faux_extensions.rb +0 -0
- data/spec/local_extensions.rb +0 -0
- data/spec/object_helpers_spec.rb +60 -0
- data/spec/require_spec.rb +31 -0
- data/spec/spec_helper.rb +45 -0
- metadata +80 -0
data/CHANGELOG
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
elreq http://elreq.rubyforge.org
|
2
|
+
Copyright (c) 2007 Kevin R. Barnes <vinbarnes@gmail.com>
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person
|
5
|
+
obtaining a copy of this software and associated documentation
|
6
|
+
files (the "Software"), to deal in the Software without
|
7
|
+
restriction, including without limitation the rights to use,
|
8
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the
|
10
|
+
Software is furnished to do so, subject to the following
|
11
|
+
conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
18
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
20
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
21
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
22
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
23
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest.txt
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
CHANGELOG
|
2
|
+
Manifest.txt
|
3
|
+
MIT-LICENSE
|
4
|
+
Rakefile
|
5
|
+
README.txt
|
6
|
+
lib/file_extensions.rb
|
7
|
+
lib/kernel_extensions.rb
|
8
|
+
lib/relative_require.rb
|
9
|
+
spec/file_extensions_spec.rb
|
10
|
+
spec/kernel_extensions_spec.rb
|
11
|
+
spec/local_extensions.rb
|
12
|
+
spec/object_helpers_spec.rb
|
13
|
+
spec/require_spec.rb
|
14
|
+
spec/spec_helper.rb
|
15
|
+
spec/lib/faux_extensions.rb
|
data/README.txt
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
= RelATIVE reqUIRE
|
2
|
+
|
3
|
+
A simple wrapper for Ruby's built-in require method.
|
4
|
+
|
5
|
+
Allows files to be required relative to the current file. Like,
|
6
|
+
|
7
|
+
require '../lib/my_file'
|
8
|
+
|
9
|
+
Which serves as a replacement for the tired idiom of,
|
10
|
+
|
11
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'my_file')
|
12
|
+
|
13
|
+
Ruby's built-in require still works the same
|
14
|
+
|
15
|
+
require 'test/unit'
|
16
|
+
|
17
|
+
== Requirements
|
18
|
+
|
19
|
+
* rubygems
|
20
|
+
|
21
|
+
== Install
|
22
|
+
|
23
|
+
Download and install relative_require with the following.
|
24
|
+
|
25
|
+
sudo gem install --remote elreq
|
26
|
+
|
27
|
+
== Credits
|
28
|
+
|
29
|
+
[<b>Rick Bradley</b> http://rickbradley.com] For the caller reminder.
|
30
|
+
|
31
|
+
[<b>Yossef Mendelssohn</b> http://rubybyraeli.org] For all the prodding, urging and provoking.
|
32
|
+
|
33
|
+
== License
|
34
|
+
|
35
|
+
:include: MIT-LICENSE
|
36
|
+
|
37
|
+
== Warranty
|
38
|
+
|
39
|
+
This software is provided "as is" and without any express or
|
40
|
+
implied warranties, including, without limitation, the implied
|
41
|
+
warranties of merchantibility and fitness for a particular
|
42
|
+
purpose.
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'hoe'
|
3
|
+
|
4
|
+
$: << 'lib'
|
5
|
+
require './lib/relative_require.rb'
|
6
|
+
|
7
|
+
Hoe.new('el_req', RelativeRequire::VERSION) do |p|
|
8
|
+
authorship = p.paragraphs_of('MIT-LICENSE', 0).first.match(/Copyright \(c\) \d+ (.*) <(.*)>/)
|
9
|
+
p.author, p.email = authorship[1], authorship[2]
|
10
|
+
p.summary = p.paragraphs_of('README.txt', 1).first
|
11
|
+
p.description = p.paragraphs_of('README.txt', 2..6).join("\n\n")
|
12
|
+
p.changes = p.paragraphs_of('CHANGELOG', 0..1).join("\n\n")
|
13
|
+
p.rubyforge_name = p.name.gsub('_', '')
|
14
|
+
p.url = 'http://elreq.rubyforge.org'
|
15
|
+
p.test_globs = ['spec/**/*_spec.rb']
|
16
|
+
end
|
17
|
+
|
18
|
+
desc 'generate manifest file'
|
19
|
+
task :manifest do
|
20
|
+
files = FileList['**/*'].exclude('doc', 'pkg').to_ary
|
21
|
+
files.reject! {|f| File.directory?(f)}
|
22
|
+
open('Manifest.txt', 'w') do |f|
|
23
|
+
f.puts files.join("\n")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
desc 'run specs'
|
28
|
+
task :spec do
|
29
|
+
specs = FileList['spec/**/*_spec.rb'].to_ary.join(' ')
|
30
|
+
puts `spec #{specs}`
|
31
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'file_extensions'
|
2
|
+
|
3
|
+
# Removes the ubiquitous and long-winded requiring. So this,
|
4
|
+
#
|
5
|
+
# <tt>require File.join(File.dirname(__FILE__), '..', 'lib', 'file_under_test')</tt>
|
6
|
+
#
|
7
|
+
# becomes,
|
8
|
+
#
|
9
|
+
# <tt>require '../lib/file_under_test/'</tt>
|
10
|
+
#
|
11
|
+
# <tt>require</tt> will accept the following:
|
12
|
+
# * A fully qualified path -- '/Users/elvis/projects/next_best_thing/main.rb'
|
13
|
+
# * A relative path -- '../lib/file_under_test'
|
14
|
+
# * A file local to the current directory -- 'test_helper'
|
15
|
+
#
|
16
|
+
# Plus, we try to run File.expand_path on all paths given to <tt>require</tt>
|
17
|
+
# so that files will not accidentally be re-required just because their
|
18
|
+
# relative paths were slightly different.
|
19
|
+
#
|
20
|
+
module Kernel
|
21
|
+
def require_with_lookup(path)
|
22
|
+
require_without_lookup(path)
|
23
|
+
rescue LoadError
|
24
|
+
relative_require(path)
|
25
|
+
end
|
26
|
+
|
27
|
+
unless method_defined? :require_without_lookup
|
28
|
+
alias_method :require_without_lookup, :require
|
29
|
+
alias_method :require, :require_with_lookup
|
30
|
+
end
|
31
|
+
|
32
|
+
def relative_require(path)
|
33
|
+
calling_file = caller[1].sub(/\.rb:\d+/, '.rb')
|
34
|
+
full_path = File.expand_path(File.join(File.dirname(calling_file), File.split_path(path)))
|
35
|
+
require_without_lookup(full_path)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Wrapper function to ensure that the caller stack is correct.
|
39
|
+
def relative_require_wrapper(path) # :nodoc:
|
40
|
+
relative_require(path)
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
$:.unshift File.expand_path(File.dirname(__FILE__))
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'file_extensions'
|
4
|
+
|
5
|
+
describe 'File.split_path' do
|
6
|
+
it 'should slit a path into components' do
|
7
|
+
File.split_path('../lib').should == ['..', 'lib']
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should use File::Separator to split on by default' do
|
11
|
+
original_separator = File::Separator
|
12
|
+
File.send(:remove_const, :Separator)
|
13
|
+
File::Separator = '|'
|
14
|
+
File.split_path('..|lib').should == ['..', 'lib']
|
15
|
+
File.send(:remove_const, :Separator)
|
16
|
+
File::Separator = original_separator
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should use custom separator if provided' do
|
20
|
+
File.split_path('..|lib', '|').should == ['..', 'lib']
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
$:.unshift File.expand_path(File.dirname(__FILE__))
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'kernel_extensions'
|
4
|
+
|
5
|
+
describe Object do
|
6
|
+
it 'should have method require_with_lookup' do
|
7
|
+
Object.new.should respond_to(:require_with_lookup)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should have method require_without_lookup' do
|
11
|
+
Object.new.should respond_to(:require_without_lookup)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should have method relative_require' do
|
15
|
+
Object.new.should respond_to(:relative_require)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should have method require' do
|
19
|
+
Object.new.should respond_to(:require)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'relative_require' do
|
24
|
+
before :each do
|
25
|
+
remove_from_loadpath(/faux_extensions\.rb/)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should require a file with a relative path' do
|
29
|
+
relative_require_wrapper('../spec/lib/faux_extensions').should == true
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should not require a file twice with different relative paths' do
|
33
|
+
relative_require_wrapper('lib/faux_extensions').should == true
|
34
|
+
relative_require_wrapper('../spec/lib/../lib/faux_extensions').should == false
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should require a file with a relative path and file extension' do
|
38
|
+
relative_require_wrapper('../spec/lib/faux_extensions.rb').should == true
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe 'require' do
|
43
|
+
before :each do
|
44
|
+
@relative_path = '../spec/lib/faux_extensions'
|
45
|
+
@full_path = File.expand_path(File.join(File.dirname(__FILE__), 'lib', 'faux_extensions.rb'))
|
46
|
+
remove_from_loadpath(/faux_extensions\.rb/)
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should call relative_require when given a relative path' do
|
50
|
+
self.expects(:relative_require).with(@relative_path)
|
51
|
+
require @relative_path
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should require a file when given a relative path' do
|
55
|
+
require(@relative_path).should == true
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should require a file in the same directory' do
|
59
|
+
require('local_extensions').should == true
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should require a file given the complete path' do
|
63
|
+
require(@full_path).should == true
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'should not require the same file if alternate paths used' do
|
67
|
+
require @relative_path
|
68
|
+
require(@full_path).should == false
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should throw a LoadError if file/path not found' do
|
72
|
+
lambda {require 'not_here.rb'}.should raise_error(LoadError)
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'should accept a relative path' do
|
76
|
+
lambda {require '../spec/lib/faux_extensions'}.should_not raise_error(LoadError)
|
77
|
+
end
|
78
|
+
end
|
File without changes
|
File without changes
|
@@ -0,0 +1,60 @@
|
|
1
|
+
$:.unshift File.expand_path(File.dirname(__FILE__))
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe 'in_loadpath?' do
|
5
|
+
before :each do
|
6
|
+
$LOADED_FEATURES << 'mock_file.rb'
|
7
|
+
end
|
8
|
+
|
9
|
+
after :each do
|
10
|
+
$LOADED_FEATURES.pop
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should return true if search string is in $LOADED_FEATURES' do
|
14
|
+
in_loadpath?('mock_file.rb').should == true
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should return false if search string is not in $LOADED_FEATURES' do
|
18
|
+
in_loadpath?('mock_file').should == false
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should return true if search regexp is in $LOADED_FEATURES' do
|
22
|
+
in_loadpath?(/mock_file/).should == true
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should return false if search regexp is not in $LOADED_FEATURES' do
|
26
|
+
in_loadpath?(/mox_file/).should == false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'remove_from_loadpath' do
|
31
|
+
before :each do
|
32
|
+
$LOADED_FEATURES << 'mock_file.rb'
|
33
|
+
end
|
34
|
+
|
35
|
+
after :each do
|
36
|
+
$LOADED_FEATURES.pop
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should remove a file from $LOADED_FEATURES' do
|
40
|
+
remove_from_loadpath('mock_file.rb')
|
41
|
+
in_loadpath?('mock_file.rb').should == false
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should not remove a file not in $LOADED_FEATURES' do
|
45
|
+
in_loadpath?('mox_file.rb').should == false
|
46
|
+
remove_from_loadpath('mox_file.rb')
|
47
|
+
in_loadpath?('mox_file.rb').should == false
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should remove a regexp file from $LOADED_FEATURES' do
|
51
|
+
remove_from_loadpath(/mock_file/)
|
52
|
+
in_loadpath?('mock_file.rb').should == false
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should not remove a file not in $LOADED_FEATURES' do
|
56
|
+
in_loadpath?('mox_file.rb').should == false
|
57
|
+
remove_from_loadpath(/mox_file/)
|
58
|
+
in_loadpath?('mox_file.rb').should == false
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
$:.unshift File.expand_path(File.dirname(__FILE__))
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
# Make sure that ruby's built-in <tt>require</tt> works the way we expect it to
|
5
|
+
describe 'built-in require' do
|
6
|
+
before :each do
|
7
|
+
remove_from_loadpath(/local_extensions\.rb/)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should be able to find items in the same directory' do
|
11
|
+
require('local_extensions').should == true
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should be able to find items in the same directory with file extension' do
|
15
|
+
require('local_extensions.rb').should == true
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should be able to find items with a complete path' do
|
19
|
+
full_path = File.expand_path(File.join(File.dirname(__FILE__), 'local_extensions.rb'))
|
20
|
+
require(full_path).should == true
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should not reload a file' do
|
24
|
+
require('local_extensions').should == true
|
25
|
+
require('local_extensions').should == false
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should not be able to reliably find files using relative path' do
|
29
|
+
lambda { require '../../spec/local_extensions' }.should raise_error(LoadError)
|
30
|
+
end
|
31
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
require 'mocha'
|
3
|
+
|
4
|
+
Spec::Runner.configure do |config|
|
5
|
+
config.mock_with :mocha
|
6
|
+
end
|
7
|
+
|
8
|
+
# Helpers for testing if a file has been required or not.
|
9
|
+
# These are mixed-in to Object so they are available to all.
|
10
|
+
module ObjectHelpers
|
11
|
+
|
12
|
+
# Searches $LOADED_FEATURES for the entire string or
|
13
|
+
# for a partial match if a Regexp is supplied and returns
|
14
|
+
# boolean response. If 'my_file.rb' is in $LOADED_FEATURES,
|
15
|
+
#
|
16
|
+
# in_loadpath?('my_file.rb') # => true
|
17
|
+
# in_loadpath?(/my_file/) # => true
|
18
|
+
#
|
19
|
+
def in_loadpath?(search)
|
20
|
+
case search
|
21
|
+
when String
|
22
|
+
$LOADED_FEATURES.include?(search)
|
23
|
+
when Regexp
|
24
|
+
$LOADED_FEATURES.any? {|file| file =~ search}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Removes a file from $LOADED_FEATURES. If a string is given,
|
29
|
+
# an exact match is removed if found. If a Regexp is given,
|
30
|
+
# all files matching are removed. Note: The return value is
|
31
|
+
# not consistent and therefore in the realm of unsupported.
|
32
|
+
#
|
33
|
+
# remove_from_loadpath('my_file.rb')
|
34
|
+
# remove_from_loadpath(/my_file/)
|
35
|
+
#
|
36
|
+
def remove_from_loadpath(search)
|
37
|
+
case search
|
38
|
+
when String
|
39
|
+
$LOADED_FEATURES.delete(search)
|
40
|
+
when Regexp
|
41
|
+
$LOADED_FEATURES.reject! {|file| file =~ search}
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
Object.send(:include, ObjectHelpers)
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: el_req
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ""
|
6
|
+
authors:
|
7
|
+
- Kevin R. Barnes
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2007-12-02 00:00:00 -06: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.3.0
|
23
|
+
version:
|
24
|
+
description: Allows files to be required relative to the current file. Like, require '../lib/my_file' Which serves as a replacement for the tired idiom of, require File.join(File.dirname(__FILE__), '..', 'lib', 'my_file') Ruby's built-in require still works the same require 'test/unit'
|
25
|
+
email: vinbarnes@gmail.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files:
|
31
|
+
- Manifest.txt
|
32
|
+
- README.txt
|
33
|
+
files:
|
34
|
+
- CHANGELOG
|
35
|
+
- Manifest.txt
|
36
|
+
- MIT-LICENSE
|
37
|
+
- Rakefile
|
38
|
+
- README.txt
|
39
|
+
- lib/file_extensions.rb
|
40
|
+
- lib/kernel_extensions.rb
|
41
|
+
- lib/relative_require.rb
|
42
|
+
- spec/file_extensions_spec.rb
|
43
|
+
- spec/kernel_extensions_spec.rb
|
44
|
+
- spec/local_extensions.rb
|
45
|
+
- spec/object_helpers_spec.rb
|
46
|
+
- spec/require_spec.rb
|
47
|
+
- spec/spec_helper.rb
|
48
|
+
- spec/lib/faux_extensions.rb
|
49
|
+
has_rdoc: true
|
50
|
+
homepage: http://elreq.rubyforge.org
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options:
|
53
|
+
- --main
|
54
|
+
- README.txt
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
version:
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project: elreq
|
72
|
+
rubygems_version: 0.9.5
|
73
|
+
signing_key:
|
74
|
+
specification_version: 2
|
75
|
+
summary: A simple wrapper for Ruby's built-in require method.
|
76
|
+
test_files:
|
77
|
+
- spec/file_extensions_spec.rb
|
78
|
+
- spec/kernel_extensions_spec.rb
|
79
|
+
- spec/object_helpers_spec.rb
|
80
|
+
- spec/require_spec.rb
|