fancy_require 1.0

Sign up to get free protection for your applications and to get access to all the features.
Binary file
@@ -0,0 +1,8 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'autotest/restart'
4
+
5
+ Autotest.add_hook :initialize do |at|
6
+ at.testlib = 'minitest/unit'
7
+ end
8
+
@@ -0,0 +1,6 @@
1
+ === 1.0 / 2010-08-20
2
+
3
+ * Major enhancement
4
+
5
+ * Birthday!
6
+
@@ -0,0 +1,8 @@
1
+ .autotest
2
+ History.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ lib/fancy_require.rb
7
+ test/lookup/toad.rb
8
+ test/test_fancy_require.rb
@@ -0,0 +1,46 @@
1
+ = fancy_require
2
+
3
+ * http://seattlerb.rubyforge.org/fancy_require
4
+
5
+ == DESCRIPTION:
6
+
7
+ Perform fancy requiring by adding a custom object to the load path. This
8
+ allows you to escape the harsh strictures directory-based lookup provided by
9
+ $LOAD_PATH.
10
+
11
+ == SYNOPSIS:
12
+
13
+ See FancyRequire
14
+
15
+ == REQUIREMENTS:
16
+
17
+ None
18
+
19
+ == INSTALL:
20
+
21
+ gem install fancy_require
22
+
23
+ == LICENSE:
24
+
25
+ (The MIT License)
26
+
27
+ Copyright (c) 2010 Eric Hodel
28
+
29
+ Permission is hereby granted, free of charge, to any person obtaining
30
+ a copy of this software and associated documentation files (the
31
+ 'Software'), to deal in the Software without restriction, including
32
+ without limitation the rights to use, copy, modify, merge, publish,
33
+ distribute, sublicense, and/or sell copies of the Software, and to
34
+ permit persons to whom the Software is furnished to do so, subject to
35
+ the following conditions:
36
+
37
+ The above copyright notice and this permission notice shall be
38
+ included in all copies or substantial portions of the Software.
39
+
40
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
41
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
42
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
43
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
44
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
45
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
46
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,15 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+
6
+ Hoe.spec 'fancy_require' do
7
+ developer 'Eric Hodel', 'drbrain@segment7.net'
8
+
9
+ self.testlib = :minitest
10
+ self.rubyforge_name = 'seattlerb'
11
+
12
+ extra_dev_deps << ['minitest', '~> 1.5']
13
+ end
14
+
15
+ # vim: syntax=Ruby
@@ -0,0 +1,118 @@
1
+ require 'rubygems'
2
+
3
+ ##
4
+ # FancyRequire allows you to push an object onto $LOAD_PATH that will find
5
+ # files to load. You can use this to implement fancier require behavior
6
+ # without overriding Kernel#require.
7
+ #
8
+ # The lookup object you push onto $LOAD_PATH must respond to #path_for. The
9
+ # feature being required (file name) will be passed in by FancyRequire.
10
+ #
11
+ # The lookup object must return the path to the feature, true, false or nil.
12
+ #
13
+ # If nil is returned FancyRequire will continue to search down the load path.
14
+ #
15
+ # If true or false are returned the lookup object handled loading the file and
16
+ # recording it in $LOADED_FEATURES. The boolean will be returned as plain
17
+ # require.
18
+ #
19
+ # == Example
20
+ #
21
+ # The easiest way to use FancyRequire is to require it everywhere (which
22
+ # includes it in Object):
23
+ #
24
+ # require 'fancy_require/everywhere'
25
+ #
26
+ # Then create a LookUp object and add it to the load path. This LookUp object
27
+ # looks in a 'lookup' directory under the current path.
28
+ #
29
+ # class LookUp
30
+ # def initialize directory
31
+ # @directory = directory
32
+ # end
33
+ #
34
+ # def path_for feature
35
+ # Dir["#{Dir.pwd}/#{directory}/lookup/#{feature}{#{FancyRequire::SUFFIX_GLOB}}"].first
36
+ # end
37
+ # end
38
+ #
39
+ # $LOAD_PATH.unshift LookUp.new 'test'
40
+ #
41
+ # Then require works like normal.
42
+ #
43
+ # require 'toad' # looks for ./test/lookup/toad.rb
44
+
45
+ module FancyRequire
46
+
47
+ ##
48
+ # The version of FancyRequire you're using... not that I know why you're
49
+ # using FancyRequire
50
+
51
+ VERSION = '1.0'
52
+
53
+ ##
54
+ # Dir#glob list of ruby file suffixes
55
+
56
+ SUFFIX_GLOB = Gem.suffixes.join ','
57
+
58
+ ##
59
+ # Regexp list of ruby file suffixes
60
+
61
+ SUFFIX_RE = Regexp.union Gem.suffixes
62
+
63
+ ##
64
+ # Replacement for Kernel#require
65
+ #
66
+ # NOTE: this method is not 100% compatible with ruby
67
+
68
+ def require feature
69
+ return false if loaded? feature # 1.8
70
+
71
+ path = nil
72
+
73
+ $LOAD_PATH.each do |obj|
74
+ path = case obj
75
+ when String then
76
+ Dir["#{obj}/#{feature}{#{FancyRequire::SUFFIX_GLOB}}"].find do |item|
77
+ File.file? item
78
+ end
79
+ else
80
+ obj.path_for feature
81
+ end
82
+
83
+ case path
84
+ when true, false
85
+ return path
86
+ else
87
+ break if path
88
+ end
89
+ end
90
+
91
+ raise LoadError, "no such file to load -- #{feature}" unless path
92
+
93
+ return false if loaded? path # 1.9
94
+
95
+ load path
96
+
97
+ if RUBY_VERSION > '1.9' then
98
+ $LOADED_FEATURES << path
99
+ else
100
+ $LOADED_FEATURES << File.basename(path)
101
+ end
102
+
103
+ true
104
+ end
105
+
106
+ ##
107
+ # Is +feature+ already in $LOADED_FEATURES?
108
+ #
109
+ # NOTE: this method is not 100% compatible with ruby
110
+
111
+ def loaded? feature
112
+ $LOADED_FEATURES.any? do |path|
113
+ path =~ /^#{feature}#{SUFFIX_RE}$/
114
+ end
115
+ end
116
+
117
+ end
118
+
File without changes
@@ -0,0 +1,65 @@
1
+ require "minitest/autorun"
2
+ require "fancy_require"
3
+
4
+ class LookUp
5
+
6
+ def path_for feature
7
+ case feature
8
+ when 'true' then true
9
+ when 'false' then false
10
+ when 'nil' then nil
11
+ else
12
+ Dir["#{Dir.pwd}/test/lookup/#{feature}{#{FancyRequire::SUFFIX_GLOB}}"].first
13
+ end
14
+ end
15
+
16
+ end
17
+
18
+ class TestFancyRequire < MiniTest::Unit::TestCase
19
+
20
+ include FancyRequire
21
+
22
+ def setup
23
+ @loaded_features = $LOADED_FEATURES.dup
24
+ @load_path = $LOAD_PATH.dup
25
+ end
26
+
27
+ def teardown
28
+ $LOADED_FEATURES.replace @loaded_features
29
+ $LOAD_PATH.replace @load_path
30
+ end
31
+
32
+ def test_require
33
+ $LOAD_PATH.unshift LookUp.new
34
+
35
+ assert require 'toad'
36
+
37
+ assert_match %r%toad\.rb$%, $LOADED_FEATURES.last
38
+ end
39
+
40
+ def test_require_false
41
+ $LOAD_PATH.unshift LookUp.new
42
+
43
+ assert_equal false, require('false')
44
+
45
+ refute_match %r%false$%, $LOADED_FEATURES.last
46
+ end
47
+
48
+ def test_require_nil
49
+ $LOAD_PATH.unshift LookUp.new
50
+
51
+ assert_raises LoadError do
52
+ require 'nil'
53
+ end
54
+ end
55
+
56
+ def test_require_true
57
+ $LOAD_PATH.unshift LookUp.new
58
+
59
+ assert_equal true, require('true')
60
+
61
+ refute_match %r%true$%, $LOADED_FEATURES.last
62
+ end
63
+
64
+ end
65
+
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fancy_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
+ - Eric Hodel
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain:
16
+ - |
17
+ -----BEGIN CERTIFICATE-----
18
+ MIIDNjCCAh6gAwIBAgIBADANBgkqhkiG9w0BAQUFADBBMRAwDgYDVQQDDAdkcmJy
19
+ YWluMRgwFgYKCZImiZPyLGQBGRYIc2VnbWVudDcxEzARBgoJkiaJk/IsZAEZFgNu
20
+ ZXQwHhcN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
38
+ date: 2010-08-27 00:00:00 +09:00
39
+ default_executable:
40
+ dependencies:
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubyforge
43
+ prerelease: false
44
+ requirement: &id001 !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ hash: 7
50
+ segments:
51
+ - 2
52
+ - 0
53
+ - 4
54
+ version: 2.0.4
55
+ type: :development
56
+ version_requirements: *id001
57
+ - !ruby/object:Gem::Dependency
58
+ name: minitest
59
+ prerelease: false
60
+ requirement: &id002 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ hash: 5
66
+ segments:
67
+ - 1
68
+ - 5
69
+ version: "1.5"
70
+ type: :development
71
+ version_requirements: *id002
72
+ - !ruby/object:Gem::Dependency
73
+ name: hoe
74
+ prerelease: false
75
+ requirement: &id003 !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 21
81
+ segments:
82
+ - 2
83
+ - 6
84
+ - 1
85
+ version: 2.6.1
86
+ type: :development
87
+ version_requirements: *id003
88
+ description: |-
89
+ Perform fancy requiring by adding a custom object to the load path. This
90
+ allows you to escape the harsh strictures directory-based lookup provided by
91
+ $LOAD_PATH.
92
+ email:
93
+ - drbrain@segment7.net
94
+ executables: []
95
+
96
+ extensions: []
97
+
98
+ extra_rdoc_files:
99
+ - History.txt
100
+ - Manifest.txt
101
+ - README.txt
102
+ files:
103
+ - .autotest
104
+ - History.txt
105
+ - Manifest.txt
106
+ - README.txt
107
+ - Rakefile
108
+ - lib/fancy_require.rb
109
+ - test/lookup/toad.rb
110
+ - test/test_fancy_require.rb
111
+ has_rdoc: true
112
+ homepage: http://seattlerb.rubyforge.org/fancy_require
113
+ licenses: []
114
+
115
+ post_install_message:
116
+ rdoc_options:
117
+ - --main
118
+ - README.txt
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ hash: 3
127
+ segments:
128
+ - 0
129
+ version: "0"
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ hash: 3
136
+ segments:
137
+ - 0
138
+ version: "0"
139
+ requirements: []
140
+
141
+ rubyforge_project: seattlerb
142
+ rubygems_version: 1.3.7
143
+ signing_key:
144
+ specification_version: 3
145
+ summary: Perform fancy requiring by adding a custom object to the load path
146
+ test_files:
147
+ - test/test_fancy_require.rb
@@ -0,0 +1,2 @@
1
+ QK�����%����!]�܏T���V�j\���v�[�L��E9G�ۜ�$p��lo���Jn��Ata���C�RE�r™�;� ��. �(Zp%������m� 3���iM]*vcA���\KM`��,�`�+"�@�G[kK�\�6�!ɖ�4HX�!��]����Nz�D
2
+ �D$g�q�<�u�#�#C��APZ��l�|�