shadows 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +47 -0
- data/Rakefile +57 -0
- data/VERSION +1 -0
- data/lib/shadows.rb +62 -0
- data/test/fixtures/a/four.rb +0 -0
- data/test/fixtures/a/one.rb +7 -0
- data/test/fixtures/b/four.rb +1 -0
- data/test/fixtures/b/one.rb +8 -0
- data/test/fixtures/b/three.rb +1 -0
- data/test/fixtures/b/two.rb +1 -0
- data/test/shadows_test.rb +30 -0
- data/test/test_helper.rb +15 -0
- metadata +95 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Simon Menke
|
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
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
= Shadows
|
2
|
+
|
3
|
+
Shadows is an extention for Kernel#require which allows you to require shadowed files. This is very useful when extending some other gem. Just shadow the original file by creating a file with exacly the same relative path as the file you want to shadow. Then <tt>#require(:shadow)</tt> somewhere in your file.
|
4
|
+
|
5
|
+
== Usage
|
6
|
+
|
7
|
+
Suppose you have file called <tt>some_gem/some_file.rb</tt> which you want to shadow.
|
8
|
+
|
9
|
+
Just create file with the same path in your gem/app (ie. <tt>some_gem/some_file.rb</tt>) and call <tt>require(:shadow)</tt> somwhere in that file.
|
10
|
+
|
11
|
+
shadowed <tt>some_gem/some_file.rb</tt>:
|
12
|
+
|
13
|
+
class Person < Struct.new(:name)
|
14
|
+
end
|
15
|
+
|
16
|
+
shadowing <tt>some_gem/some_file.rb</tt>:
|
17
|
+
|
18
|
+
require :shadow
|
19
|
+
|
20
|
+
class Person
|
21
|
+
def whats_your_name?
|
22
|
+
"I'm #{self.name}. What's your name?"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
Person.new('Simon').whats_your_name? # => "I'm Simon. What's your name?"
|
27
|
+
|
28
|
+
|
29
|
+
== Installation
|
30
|
+
|
31
|
+
gem install shadows
|
32
|
+
|
33
|
+
|
34
|
+
== Note on Patches/Pull Requests
|
35
|
+
|
36
|
+
* Fork the project.
|
37
|
+
* Make your feature addition or bug fix.
|
38
|
+
* Add tests for it. This is important so I don't break it in a
|
39
|
+
future version unintentionally.
|
40
|
+
* Commit, do not mess with rakefile, version, or history.
|
41
|
+
(if you want to have your own version, that is fine but
|
42
|
+
bump version in a commit by itself I can ignore when I pull)
|
43
|
+
* Send me a pull request. Bonus points for topic branches.
|
44
|
+
|
45
|
+
== Copyright
|
46
|
+
|
47
|
+
Copyright (c) 2010 Simon Menke. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "shadows"
|
8
|
+
gem.summary = %Q{Recursively require files.}
|
9
|
+
gem.description = %Q{Like #include and #extend but for ruby source files.}
|
10
|
+
gem.email = "simon.menke@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/fd/shadows"
|
12
|
+
gem.authors = ["Simon Menke"]
|
13
|
+
gem.add_development_dependency "thoughtbot-shoulda"
|
14
|
+
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/*_test.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/*_test.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
if File.exist?('VERSION')
|
48
|
+
version = File.read('VERSION')
|
49
|
+
else
|
50
|
+
version = ""
|
51
|
+
end
|
52
|
+
|
53
|
+
rdoc.rdoc_dir = 'rdoc'
|
54
|
+
rdoc.title = "shadows #{version}"
|
55
|
+
rdoc.rdoc_files.include('README*')
|
56
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
57
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/shadows.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
module Shadows
|
2
|
+
|
3
|
+
def require_with_shadow(filename, *args, &block)
|
4
|
+
if filename == :shadow
|
5
|
+
|
6
|
+
# find the path of the caller
|
7
|
+
caller_list = caller
|
8
|
+
caller_path = caller_list.first.split(':', 2).first
|
9
|
+
relative_caller_path = nil
|
10
|
+
|
11
|
+
# we don't support eval
|
12
|
+
if caller_path == '(eval)'
|
13
|
+
e = LoadError.new("#require(:shadow) cannot be called from an eval unless you specify what file it represents!")
|
14
|
+
e.set_backtrace caller_list
|
15
|
+
raise e
|
16
|
+
end
|
17
|
+
|
18
|
+
# find a file with the same relative path a caller
|
19
|
+
$:.uniq.each do |load_path|
|
20
|
+
|
21
|
+
# find load path of caller
|
22
|
+
if caller_path[0,load_path.size] == load_path
|
23
|
+
relative_caller_path = caller_path[(load_path.size + 1)..-1]
|
24
|
+
|
25
|
+
# check for shadowed file
|
26
|
+
elsif relative_caller_path
|
27
|
+
full_path = File.join(load_path, relative_caller_path)
|
28
|
+
|
29
|
+
# faster than require, rescue
|
30
|
+
if Dir.glob("#{full_path}{.rb,}").empty?
|
31
|
+
next
|
32
|
+
end
|
33
|
+
|
34
|
+
# try loading the shadowed file
|
35
|
+
begin
|
36
|
+
return require(full_path)
|
37
|
+
rescue LoadError
|
38
|
+
next
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# raise LoadError when no shadowed file was found
|
44
|
+
e = LoadError.new("no such file to load - #{relative_caller_path} [:shadow]")
|
45
|
+
e.set_backtrace caller_list
|
46
|
+
raise e
|
47
|
+
|
48
|
+
else
|
49
|
+
require_without_shadow(filename, *args, &block)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.included(base)
|
54
|
+
base.class_eval do
|
55
|
+
alias_method :require_without_shadow, :require
|
56
|
+
alias_method :require, :require_with_shadow
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
Kernel.send :include, self
|
61
|
+
|
62
|
+
end
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
eval %{ require :shadow }, nil, __FILE__
|
@@ -0,0 +1 @@
|
|
1
|
+
eval %{ require :shadow }
|
@@ -0,0 +1 @@
|
|
1
|
+
require :shadow
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ShadowsTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
should "#require :shadow if there is a file in the load path with exactly the same relative path" do
|
6
|
+
assert_nothing_raised do
|
7
|
+
assert require('one')
|
8
|
+
assert !require('one')
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
should "raise LoadError if no :shadow file is found" do
|
13
|
+
assert_raise LoadError do
|
14
|
+
require('two')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
should "raise LoadError if #require :shadow was executed from #eval without specifing a file" do
|
19
|
+
assert_raise LoadError do
|
20
|
+
require('three')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
should "#require :shadow from #eval if file was specified" do
|
25
|
+
assert_nothing_raised do
|
26
|
+
assert require('four')
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
|
5
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
|
+
|
8
|
+
$LOAD_PATH.unshift File.expand_path("../fixtures/a", __FILE__)
|
9
|
+
$LOAD_PATH.unshift File.expand_path("../fixtures/b", __FILE__)
|
10
|
+
|
11
|
+
require 'shadows'
|
12
|
+
|
13
|
+
class Test::Unit::TestCase
|
14
|
+
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shadows
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Simon Menke
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-04-15 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: thoughtbot-shoulda
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :development
|
31
|
+
version_requirements: *id001
|
32
|
+
description: "Like #include and #extend but for ruby source files."
|
33
|
+
email: simon.menke@gmail.com
|
34
|
+
executables: []
|
35
|
+
|
36
|
+
extensions: []
|
37
|
+
|
38
|
+
extra_rdoc_files:
|
39
|
+
- LICENSE
|
40
|
+
- README.rdoc
|
41
|
+
files:
|
42
|
+
- .document
|
43
|
+
- .gitignore
|
44
|
+
- LICENSE
|
45
|
+
- README.rdoc
|
46
|
+
- Rakefile
|
47
|
+
- VERSION
|
48
|
+
- lib/shadows.rb
|
49
|
+
- test/fixtures/a/four.rb
|
50
|
+
- test/fixtures/a/one.rb
|
51
|
+
- test/fixtures/b/four.rb
|
52
|
+
- test/fixtures/b/one.rb
|
53
|
+
- test/fixtures/b/three.rb
|
54
|
+
- test/fixtures/b/two.rb
|
55
|
+
- test/shadows_test.rb
|
56
|
+
- test/test_helper.rb
|
57
|
+
has_rdoc: true
|
58
|
+
homepage: http://github.com/fd/shadows
|
59
|
+
licenses: []
|
60
|
+
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options:
|
63
|
+
- --charset=UTF-8
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
segments:
|
71
|
+
- 0
|
72
|
+
version: "0"
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.3.6
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: Recursively require files.
|
87
|
+
test_files:
|
88
|
+
- test/fixtures/a/four.rb
|
89
|
+
- test/fixtures/a/one.rb
|
90
|
+
- test/fixtures/b/four.rb
|
91
|
+
- test/fixtures/b/one.rb
|
92
|
+
- test/fixtures/b/three.rb
|
93
|
+
- test/fixtures/b/two.rb
|
94
|
+
- test/shadows_test.rb
|
95
|
+
- test/test_helper.rb
|