required 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README +1 -5
  2. data/lib/required.rb +4 -4
  3. data/tests/required_test.rb +182 -0
  4. metadata +30 -16
data/README CHANGED
@@ -89,9 +89,5 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
89
89
 
90
90
  == Links
91
91
 
92
- Required:: <http://blog.codesherpas.com/>
93
- Required's Rubyforge project:: <http://rubyforge.org/projects/required>
92
+ Required's RubyGems.org project:: <http://rubygems.org/gems/required>
94
93
  Official Required repository:: <http://github.com/required>
95
-
96
- Arild Shirazi:: <http://setup-a-personal-site-quick.com/>
97
-
data/lib/required.rb CHANGED
@@ -4,11 +4,11 @@ module Kernel
4
4
  # `require` all ruby files found. Returns an array of files that were
5
5
  # actually loaded in the order which they were loaded.
6
6
  # Available options:
7
- # :recurse => [true|false] # Descend into subdirectories or not
8
- # :include => /filename_pattern/ # Only require files matching this regex
9
- # :exclude => /filename_pattern/ # Don't require files matching this regex
7
+ # :recurse => true # Descend into subdirectories (default is false)
8
+ # :include => /pattern/ # Only require files with names matching this regex
9
+ # :exclude => /pattern/ # Don't require files matching this regex
10
10
  # :sort => lambda { |x, y| y <=> x }
11
- # # Specify a custom sort order for files within each directory
11
+ # # Specify a custom file sort order within each directory
12
12
  #
13
13
  # Note: required files are loaded only once.
14
14
  def required(directories, options={})
@@ -0,0 +1,182 @@
1
+ Dir.chdir File.expand_path(File.dirname(__FILE__) + "/..")
2
+ require 'rubygems'
3
+ require 'shoulda'
4
+ require 'lib/required'
5
+
6
+ class RequiredTest < Test::Unit::TestCase
7
+
8
+ @@req_dir = "tests/required_test"
9
+
10
+ def reset_required_test_files
11
+ my_test_files = $".select { |r| r =~ /required_test/ }
12
+ my_test_files.each { |r| $".delete r }
13
+ $REQUIRED = []
14
+ # $REQUIRED global will be populated with name of each test file required
15
+ end
16
+
17
+ context "Non-recursive required" do
18
+ setup do
19
+ reset_required_test_files
20
+ @required = required @@req_dir
21
+ end
22
+
23
+ should "accurately report all files that have been required" do
24
+ assert_equal(
25
+ %W(#{@@req_dir}/required-1.rb
26
+ #{@@req_dir}/required-2.rb
27
+ #{@@req_dir}/required\ space\ 3.rb
28
+ ).sort, @required.sort)
29
+ end
30
+
31
+ should "require all ruby files (those having a .rb extension)" do
32
+ %W(#{@@req_dir}/required-1.rb
33
+ #{@@req_dir}/required-2.rb
34
+ #{@@req_dir}/required\ space\ 3.rb
35
+ ).each {|f| assert $REQUIRED.include?(f),
36
+ "#{f} not found in #{$REQUIRED.inspect}"}
37
+ end
38
+
39
+ should "ignore non-ruby files" do
40
+ %W(#{@@req_dir}/junk space 4.rake
41
+ #{@@req_dir}/junk-2
42
+ #{@@req_dir}/junk-1.rbt
43
+ #{@@req_dir}/junk-3rb
44
+ ).each {|f| assert ! $REQUIRED.include?(f),
45
+ "#{f} found in #{$REQUIRED.inspect}"}
46
+ end
47
+
48
+ should "ignore hidden files (files with a . prefix)" do
49
+ %W(#{@@req_dir}/.required-4.rb
50
+ ).each {|f| assert ! $REQUIRED.include?(f),
51
+ "#{f} found in #{$REQUIRED.inspect}"}
52
+ end
53
+
54
+ should "ignore ruby files in subdirectories" do
55
+ %W(#{@@req_dir}/sub1
56
+ #{@@req_dir}/sub1/required-1.rb
57
+ #{@@req_dir}/sub1/required-2.rb
58
+ #{@@req_dir}/sub1/required\ space\ 3.rb
59
+ #{@@req_dir}/sub1/junk space 4.rake
60
+ #{@@req_dir}/sub1/junk-2
61
+ #{@@req_dir}/sub1/junk-1.rbt
62
+ #{@@req_dir}/sub1/junk-3rb
63
+ ).each {|f| assert ! $REQUIRED.include?(f),
64
+ "#{f} found in #{$REQUIRED.inspect}"}
65
+ end
66
+ end
67
+
68
+ context "Non-recursive, exclude pattern required" do
69
+ setup do
70
+ reset_required_test_files
71
+ @required = required(@@req_dir, :exclude => /-2/)
72
+ end
73
+
74
+ should "require only ruby files matching an include pattern" do
75
+ %W(#{@@req_dir}/required-2.rb
76
+ ).each {|f| assert ! $REQUIRED.include?(f),
77
+ "#{f} unexpectedly found in #{$REQUIRED.inspect}"}
78
+ end
79
+
80
+ should "not require ruby files that do not match the include pattern" do
81
+ %W(#{@@req_dir}/required-1.rb
82
+ #{@@req_dir}/required\ space\ 3.rb
83
+ ).each {|f| assert $REQUIRED.include?(f),
84
+ "#{f} not found in #{$REQUIRED.inspect}"}
85
+ end
86
+ end
87
+
88
+ context "Non-recursive, include pattern required" do
89
+ setup do
90
+ reset_required_test_files
91
+ @required = required(@@req_dir, :include => /-2/)
92
+ end
93
+
94
+ should "require only ruby files matching an include pattern" do
95
+ %W(#{@@req_dir}/required-2.rb
96
+ ).each {|f| assert $REQUIRED.include?(f),
97
+ "#{f} not found in #{$REQUIRED.inspect}"}
98
+ end
99
+
100
+ should "not require ruby files that do not match the include pattern" do
101
+ %W(#{@@req_dir}/required-1.rb
102
+ #{@@req_dir}/required\ space\ 3.rb
103
+ ).each {|f| assert ! $REQUIRED.include?(f),
104
+ "#{f} unexcpectedly found in #{$REQUIRED.inspect}"}
105
+ end
106
+ end
107
+
108
+ context "Non-recursive, exclude and include pattern required" do
109
+ setup do
110
+ reset_required_test_files
111
+ @required = required(@@req_dir,
112
+ {:include => /required-/, :exclude => /2/})
113
+ end
114
+
115
+ should "require only ruby files matching an include pattern" do
116
+ %W(#{@@req_dir}/required-1.rb
117
+ ).each {|f| assert $REQUIRED.include?(f),
118
+ "#{f} not found in #{$REQUIRED.inspect}"}
119
+ end
120
+
121
+ should "not require ruby files that do not match the include pattern" do
122
+ %W(#{@@req_dir}/required-2.rb
123
+ #{@@req_dir}/required\ space\ 3.rb
124
+ ).each {|f| assert ! $REQUIRED.include?(f),
125
+ "#{f} unexcpectedly found in #{$REQUIRED.inspect}"}
126
+ end
127
+ end
128
+
129
+ context "Recursive required" do
130
+ setup do
131
+ reset_required_test_files
132
+ @required = required(@@req_dir, :recurse => true)
133
+ end
134
+
135
+ should "accurately report all files that have been required" do
136
+ assert_equal(
137
+ %W(#{@@req_dir}/required\ space\ 3.rb
138
+ #{@@req_dir}/required-1.rb
139
+ #{@@req_dir}/required-2.rb
140
+ #{@@req_dir}/sub1/required-1.rb
141
+ #{@@req_dir}/sub1/required-2.rb
142
+ #{@@req_dir}/sub1/required\ space\ 3.rb
143
+ #{@@req_dir}/sub1/sub2/required-1.rb
144
+ #{@@req_dir}/sub1/sub2/required-2.rb
145
+ #{@@req_dir}/sub1/sub2/required\ space\ 3.rb
146
+ ).sort, @required.sort)
147
+ end
148
+
149
+ should "require all ruby files in subdirectories" do
150
+ %W(#{@@req_dir}/sub1/required-1.rb
151
+ #{@@req_dir}/sub1/required-2.rb
152
+ #{@@req_dir}/sub1/required\ space\ 3.rb
153
+ #{@@req_dir}/sub1/sub2/required-1.rb
154
+ #{@@req_dir}/sub1/sub2/required-2.rb
155
+ #{@@req_dir}/sub1/sub2/required\ space\ 3.rb
156
+ ).each {|f| assert $REQUIRED.include?(f),
157
+ "#{f} not found in #{$REQUIRED.inspect}"}
158
+ end
159
+
160
+ should "ignore non-ruby files in subdirectories" do
161
+ %W(#{@@req_dir}/sub1/junk space 4.rake
162
+ #{@@req_dir}/sub1/junk-2
163
+ #{@@req_dir}/sub1/junk-1.rbt
164
+ #{@@req_dir}/sub1/junk-3rb
165
+ #{@@req_dir}/sub1/sub2/junk space 4.rake
166
+ #{@@req_dir}/sub1/sub2/junk-2
167
+ #{@@req_dir}/sub1/sub2/junk-1.rbt
168
+ #{@@req_dir}/sub1/sub2/junk-3rb
169
+ ).each {|f| assert ! $REQUIRED.include?(f),
170
+ "#{f} found in #{$REQUIRED.inspect}"}
171
+ end
172
+
173
+ should "ignore hidden files (files with a . prefix) in subdirectories" do
174
+ %W(#{@@req_dir}/.required-4.rb
175
+ #{@@req_dir}/sub1/.required-4.rb
176
+ #{@@req_dir}/sub1/sub2/.required-4.rb
177
+ #{@@req_dir}/sub1/.sub3/required-1.rb
178
+ ).each {|f| assert ! $REQUIRED.include?(f),
179
+ "#{f} found in #{$REQUIRED.inspect}"}
180
+ end
181
+ end
182
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Arild Shirazi
@@ -14,20 +14,34 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-31 00:00:00 -05:00
17
+ date: 2010-04-01 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
21
- description: "Required, a utility to require all files in a directory\n\
22
- Required provides the ability to require all files within a directory, with options for file inclusion/exclusion based on pattern matching, as well as recursive descent. An array of all the files that were loaded (\xE2\x80\x98require\xE2\x80\x99 only loads a file the first time) are returned.\n\n\
23
- Quick start\n\
24
- First pull in the \xE2\x80\x98required\xE2\x80\x99 module:\n\n require 'required'\n\
25
- To pull in all ruby files in a directory (this excludes subdirectories):\n\n required \"some/path/to/dir\"\n\
26
- Same as before, but require the files in reverse alphanumeric order:\n\n required \"some/path/to/dir\", :sort => lambda { |x, y| y <=> x}\n\
27
- To pull in files from multiple directories:\n\n required [\"some/path/to/dir\", \"another/path/another/dir\"]\n\
28
- Or to recurse through subdirectories, requiring all files along the way:\n\n required \"lib\", {:recurse => true}\n\
29
- Same as before, but exclude ruby files tagged as \"_old\":\n\n required \"lib\", {:recurse => true, :exclude => /_old/}\n # Will not require \"lib/extensions/string_old.rb\"\n\
30
- Same as before, but only require ruby files tagged with \"_new\":\n\n required \"lib\", {:recurse => true, :include => /_new/}\n"
21
+ description: |
22
+ <p>Required is a utility to require all files in a directory.</p>
23
+ <p>
24
+ Why would one want to require a whole bunch of files at once? I have used this
25
+ gem on 2 projects to:
26
+ </p>
27
+ <ul>
28
+ <li>require dozens of jar files when working on a JRuby project</li>
29
+ <li>pull in all files before running code coverage (rcov), to find code that
30
+ is otherwise dead/untouched</li>
31
+ </ul>
32
+
33
+ <p>
34
+ Options for <strong>required</strong> include the ability to recursively
35
+ descend through subdirectories, include/exclude files based on pattern
36
+ matching, and to specify the order of requires based on filename. An array of
37
+ all the files that were loaded is returned.
38
+ </p>
39
+
40
+ <code>require 'required'
41
+ required "some/path/to/dir"</code>
42
+
43
+ <p>See the README for quick usage instructions</p>
44
+
31
45
  email: ashirazi@codesherpas.com
32
46
  executables: []
33
47
 
@@ -67,6 +81,6 @@ rubyforge_project:
67
81
  rubygems_version: 1.3.6
68
82
  signing_key:
69
83
  specification_version: 3
70
- summary: Provides the ability to 'require' many or all files within directories, using a simple statement.
71
- test_files: []
72
-
84
+ summary: Required is a utility to require all files in a directory.
85
+ test_files:
86
+ - tests/required_test.rb