required 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +97 -0
- data/lib/required.rb +51 -0
- metadata +63 -0
data/README
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
= Required, a utility to require all files in a directory
|
2
|
+
|
3
|
+
Required provides the ability to <b>require</b> all files within a
|
4
|
+
<b>d</b>irectory, with options for file inclusion/exclusion based on pattern
|
5
|
+
matching, as well as recursive descent. An array of all the files that were
|
6
|
+
loaded ('require' only loads a file the first time) are returned.
|
7
|
+
|
8
|
+
== Quick start
|
9
|
+
|
10
|
+
First pull in the 'required' module:
|
11
|
+
require 'required'
|
12
|
+
|
13
|
+
To pull in all ruby files in a directory (this excludes subdirectories):
|
14
|
+
|
15
|
+
required "some/path/to/dir"
|
16
|
+
|
17
|
+
Same as before, but require the files in reverse alphanumeric order:
|
18
|
+
|
19
|
+
required "some/path/to/dir", :sort => lambda { |x, y| y <=> x}
|
20
|
+
|
21
|
+
To pull in files from multiple directories:
|
22
|
+
|
23
|
+
required ["some/path/to/dir", "another/path/another/dir"]
|
24
|
+
|
25
|
+
Or to recurse through subdirectories, requiring all files along the way:
|
26
|
+
|
27
|
+
required "lib", {:recurse => true}
|
28
|
+
|
29
|
+
Same as before, but exclude ruby files tagged as "_old":
|
30
|
+
|
31
|
+
required "lib", {:recurse => true, :exclude => /_old/}
|
32
|
+
# Will not require "lib/extensions/string_old.rb"
|
33
|
+
|
34
|
+
Same as before, but only require ruby files tagged with "_new":
|
35
|
+
|
36
|
+
required "lib", {:recurse => true, :include => /_new/}
|
37
|
+
|
38
|
+
== Installing with RubyGems
|
39
|
+
|
40
|
+
A Gem of Required is available at gemcutter.org. You can install it with:
|
41
|
+
|
42
|
+
gem install required
|
43
|
+
|
44
|
+
== Running the tests
|
45
|
+
|
46
|
+
Testing Required requires the shoulda testing framework:
|
47
|
+
|
48
|
+
gem install shoulda
|
49
|
+
|
50
|
+
There is one rake-based test task:
|
51
|
+
|
52
|
+
rake test runs all the tests
|
53
|
+
|
54
|
+
== History
|
55
|
+
|
56
|
+
* 2010-02-23: First public release 0.1.
|
57
|
+
|
58
|
+
== Contact
|
59
|
+
|
60
|
+
Git repository (send Git patches to the mailing list):
|
61
|
+
* http://github.com/required
|
62
|
+
|
63
|
+
== Thanks
|
64
|
+
|
65
|
+
I would like to thank Ryan Wilcox for coming up with the initial idea and
|
66
|
+
implementation of a convenience method for requiring everything in a directory
|
67
|
+
for a project we work on.
|
68
|
+
|
69
|
+
== Copyright
|
70
|
+
|
71
|
+
Copyright (C) 2010 Arild Shirazi
|
72
|
+
|
73
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
74
|
+
of this software and associated documentation files (the "Software"), to
|
75
|
+
deal in the Software without restriction, including without limitation the
|
76
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
77
|
+
sell copies of the Software, and to permit persons to whom the Software is
|
78
|
+
furnished to do so, subject to the following conditions:
|
79
|
+
|
80
|
+
The above copyright notice and this permission notice shall be included in
|
81
|
+
all copies or substantial portions of the Software.
|
82
|
+
|
83
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
84
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
85
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
86
|
+
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
87
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
88
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
89
|
+
|
90
|
+
== Links
|
91
|
+
|
92
|
+
Required:: <http://blog.codesherpas.com/>
|
93
|
+
Required's Rubyforge project:: <http://rubyforge.org/projects/required>
|
94
|
+
Official Required repository:: <http://github.com/required>
|
95
|
+
|
96
|
+
Arild Shirazi:: <http://setup-a-personal-site-quick.com/>
|
97
|
+
|
data/lib/required.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
module Kernel
|
2
|
+
|
3
|
+
# Searches through the given directory or array of directories, and will
|
4
|
+
# `require` all ruby files found. Returns an array of files that were
|
5
|
+
# actually loaded in the order which they were loaded.
|
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
|
10
|
+
# :sort => lambda { |x, y| y <=> x }
|
11
|
+
# # Specify a custom sort order for files within each directory
|
12
|
+
#
|
13
|
+
# Note: required files are loaded only once.
|
14
|
+
def required(directories, options={})
|
15
|
+
validate_options(options)
|
16
|
+
loaded_files = []
|
17
|
+
directories.each do |directory|
|
18
|
+
next unless File.directory? directory
|
19
|
+
loaded_files << require_within(directory, options)
|
20
|
+
|
21
|
+
next unless options[:recurse]
|
22
|
+
sub_dirs = Dir["#{directory}/*"].select { |d| File.directory? d }
|
23
|
+
options[:sort] ? sub_dirs.sort!(&options[:sort]) : sub_dirs.sort!
|
24
|
+
# TODO AS: Reject sub_dir for includes and excludes
|
25
|
+
sub_dirs.each { |sub_dir| loaded_files << required(sub_dir, options) }
|
26
|
+
end
|
27
|
+
loaded_files.flatten
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
def require_within(directory, opt)
|
32
|
+
next unless File.directory? directory
|
33
|
+
loaded_files = []
|
34
|
+
ruby_files = Dir["#{directory}/*.rb"].reject{ |f| File.directory? f }
|
35
|
+
ruby_files = ruby_files.select { |f| f =~ opt[:include] } if opt[:include]
|
36
|
+
ruby_files = ruby_files.reject { |f| f =~ opt[:exclude] } if opt[:exclude]
|
37
|
+
opt[:sort] ? ruby_files.sort!(&opt[:sort]) : ruby_files.sort!
|
38
|
+
ruby_files.each { |file| loaded_files << file if require file }
|
39
|
+
loaded_files
|
40
|
+
end
|
41
|
+
|
42
|
+
def validate_options(options)
|
43
|
+
valid = [:recurse, :include, :exclude, :sort]
|
44
|
+
given = options.keys
|
45
|
+
unless valid.size - given.size == (valid - given).size
|
46
|
+
puts "warn: required recognizes only #{valid.inspect}, " +
|
47
|
+
"but was given #{given.inspect}\n#{caller[1].sub(/:in.*/, '')}"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: required
|
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
|
+
- Arild Shirazi
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-03-31 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description:
|
22
|
+
email: ashirazi@codesherpas.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README
|
29
|
+
files:
|
30
|
+
- lib/required.rb
|
31
|
+
- README
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: http://blog.codesherpas.com/
|
34
|
+
licenses: []
|
35
|
+
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
segments:
|
53
|
+
- 0
|
54
|
+
version: "0"
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.3.6
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: Provides the ability to 'require' many or all files within directories, using a simple statement.
|
62
|
+
test_files: []
|
63
|
+
|