require_pattern 1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +2 -0
- data/LICENSE +7 -0
- data/README.md +25 -0
- data/lib/require_pattern.rb +37 -0
- data/require_pattern.gemspec +16 -0
- data/spec/dummy/bad.rb +2 -0
- data/spec/dummy/colors/green.rb +1 -0
- data/spec/dummy/colors/red.rb +1 -0
- data/spec/dummy/dependancies/car.rb +2 -0
- data/spec/dummy/dependancies/golf.rb +2 -0
- data/spec/dummy/dependancies/vehicle.rb +2 -0
- data/spec/dummy/dependancies/volkswagen.rb +2 -0
- data/spec/dummy/loaded.rb +1 -0
- data/spec/dummy/shapes/cirlcle.rb +1 -0
- data/spec/dummy/shapes/square.rb +1 -0
- data/spec/dummy/sizes/short.rb +1 -0
- data/spec/dummy/sizes/tall.rb +1 -0
- data/spec/spec.rb +43 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d5b24c83c3d4175da64ffc63b964fbbee9410c38
|
4
|
+
data.tar.gz: 386c726201430bf9c08d13dbf2e59d7f3283dbec
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f7d74cab91a759eb53b0870a7f06b59564d09d9164747f86c8d1ba33dfd8a3f4e131fa2f5ce2882ffbb32cbcb33235921dfdc9932ad02b0a20248231c3ef06ae
|
7
|
+
data.tar.gz: c7b9a4283358588402e3a9c3f5d1a28475c61d6e61b3fa8486b9a49e77eb12187f2ac209f1b0723051d822688e5d7c014a3d125b567ece2803fa4c85c8641454
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2013 Tom Wardrop
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
Require Pattern
|
2
|
+
===============
|
3
|
+
This library provides a simple extension to the Kernel module that allows files to be conveniantly `require`'d based on a glob pattern.
|
4
|
+
|
5
|
+
This library does NOT use load paths. Instead, it provides methods that load files either relative to the current working directory (`require_pattern`), or relative to the current file (`require_relative_pattern`).
|
6
|
+
|
7
|
+
Why?
|
8
|
+
----
|
9
|
+
At this point you may be wondering why would I not just do this:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
Dir.glob('**/*.rb') { |f| require f }
|
13
|
+
```
|
14
|
+
|
15
|
+
In some instances you will get away with that, but what happens when file _x_ must be required before file _y_? This is what this library intends to resolve. It will load files in a loop, keeping track of what succeeds, and fails with an exception. This process continues until either all files have been required, or until a loop iteration fails to load any files at all.
|
16
|
+
|
17
|
+
Usage
|
18
|
+
-----
|
19
|
+
Usage is relatively intuitive. Simply call either `require_pattern` or `require_relative_pattern` with one or more glob patterns, and all files matching those patterns will be required.
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require_pattern 'lib/**/*.rb', 'models/**/*.rb'
|
23
|
+
```
|
24
|
+
|
25
|
+
Any files that refuse to load will be reported to STDERR with backtrace information.
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'term-colorizer'
|
2
|
+
|
3
|
+
module Kernel
|
4
|
+
|
5
|
+
# Requires all files matching the glob patterns provided.
|
6
|
+
# If a file fails to load, it keeps retrying until all remaining files fail to load, in which case the exception is
|
7
|
+
# output to STDERR for each failed file.
|
8
|
+
def require_pattern(*patterns)
|
9
|
+
loaded = false # Indicates whether any files were loaded at all
|
10
|
+
require_map = patterns.map { |pattern| Dir.glob(pattern) }.flatten.reduce({}) { |m,v| m[v] = nil; m }
|
11
|
+
loop do
|
12
|
+
remaining = require_map.reject! do |file,|
|
13
|
+
begin
|
14
|
+
loaded = true if require File.expand_path(file)
|
15
|
+
true
|
16
|
+
rescue => require_map[file]
|
17
|
+
false
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
if require_map.empty?
|
22
|
+
break loaded
|
23
|
+
elsif remaining.nil?
|
24
|
+
STDERR.puts "\nSome files failed:".red
|
25
|
+
require_map.each { |file, e| STDERR.puts " #{file}: ".red + "#{e.backtrace[0]} #{e}".yellow}
|
26
|
+
STDERR.puts
|
27
|
+
raise LoadError, "One or more files failed to load. See STDERR output for details."
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def require_relative_pattern(*patterns)
|
33
|
+
caller_path = caller_locations(1,1)[0].path
|
34
|
+
patterns.map! { |pattern| File.join(File.dirname(caller_path), pattern) }
|
35
|
+
require_pattern(*patterns)
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new 'require_pattern', '1.0' do |s|
|
4
|
+
s.summary = 'Requires files based on a pattern in a robust and optimistic manner.'
|
5
|
+
s.authors = ['Tom Wardrop']
|
6
|
+
s.email = 'tom@tomwardrop.com'
|
7
|
+
s.homepage = 'http://github.com/wardrop/RequirePattern'
|
8
|
+
s.license = 'MIT'
|
9
|
+
s.files = Dir.glob(`git ls-files`.split("\n") - %w[.gitignore])
|
10
|
+
s.test_files = Dir.glob('spec/spec.rb')
|
11
|
+
s.rdoc_options = %w[--line-numbers --inline-source --title Scorched --encoding=UTF-8]
|
12
|
+
|
13
|
+
s.required_ruby_version = '>= 1.9.3'
|
14
|
+
s.add_dependency 'term-colorizer'
|
15
|
+
s.add_development_dependency 'rspec'
|
16
|
+
end
|
data/spec/dummy/bad.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
$green = true
|
@@ -0,0 +1 @@
|
|
1
|
+
$red = true
|
@@ -0,0 +1 @@
|
|
1
|
+
$loaded = true
|
@@ -0,0 +1 @@
|
|
1
|
+
$circle = true
|
@@ -0,0 +1 @@
|
|
1
|
+
$square = true
|
@@ -0,0 +1 @@
|
|
1
|
+
$short = true
|
@@ -0,0 +1 @@
|
|
1
|
+
$tall = true
|
data/spec/spec.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require_relative '../lib/require_pattern'
|
2
|
+
Dir.chdir File.expand_path('../dummy', __FILE__)
|
3
|
+
|
4
|
+
describe 'require_pattern' do
|
5
|
+
it "requires relative to the current working directory by default" do
|
6
|
+
require_pattern 'colors/*.rb'
|
7
|
+
$red.should be_true
|
8
|
+
$green.should be_true
|
9
|
+
end
|
10
|
+
|
11
|
+
it "can require relative to the current file" do
|
12
|
+
require_relative_pattern 'dummy/sizes/*.rb'
|
13
|
+
$tall.should be_true
|
14
|
+
$short.should be_true
|
15
|
+
end
|
16
|
+
|
17
|
+
it "overcomes file ordering issues by intelligently retrying" do
|
18
|
+
require_relative_pattern 'dummy/dependancies/*.rb'
|
19
|
+
Golf
|
20
|
+
end
|
21
|
+
|
22
|
+
it "reports true only if a file was loaded" do
|
23
|
+
require_pattern('loaded.rb').should be_true
|
24
|
+
require_pattern('loaded.rb').should be_false
|
25
|
+
end
|
26
|
+
|
27
|
+
it "can take multiple patterns" do
|
28
|
+
require_pattern('colors/*.rb', 'shapes/*.rb').should be_true
|
29
|
+
$circle.should be_true
|
30
|
+
$square.should be_true
|
31
|
+
end
|
32
|
+
|
33
|
+
it "outputs to STDERR on failure" do
|
34
|
+
STDERR.should_receive(:puts).at_least(2).times
|
35
|
+
expect {
|
36
|
+
require_pattern 'bad.*'
|
37
|
+
}.to raise_error(LoadError)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "doesn't care if it doesn't match any files" do
|
41
|
+
require_pattern('good luck matching this').should be_false
|
42
|
+
end
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: require_pattern
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.0'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tom Wardrop
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: term-colorizer
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description:
|
42
|
+
email: tom@tomwardrop.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- Gemfile
|
48
|
+
- LICENSE
|
49
|
+
- README.md
|
50
|
+
- lib/require_pattern.rb
|
51
|
+
- require_pattern.gemspec
|
52
|
+
- spec/dummy/bad.rb
|
53
|
+
- spec/dummy/colors/green.rb
|
54
|
+
- spec/dummy/colors/red.rb
|
55
|
+
- spec/dummy/dependancies/car.rb
|
56
|
+
- spec/dummy/dependancies/golf.rb
|
57
|
+
- spec/dummy/dependancies/vehicle.rb
|
58
|
+
- spec/dummy/dependancies/volkswagen.rb
|
59
|
+
- spec/dummy/loaded.rb
|
60
|
+
- spec/dummy/shapes/cirlcle.rb
|
61
|
+
- spec/dummy/shapes/square.rb
|
62
|
+
- spec/dummy/sizes/short.rb
|
63
|
+
- spec/dummy/sizes/tall.rb
|
64
|
+
- spec/spec.rb
|
65
|
+
homepage: http://github.com/wardrop/RequirePattern
|
66
|
+
licenses:
|
67
|
+
- MIT
|
68
|
+
metadata: {}
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options:
|
71
|
+
- --line-numbers
|
72
|
+
- --inline-source
|
73
|
+
- --title
|
74
|
+
- Scorched
|
75
|
+
- --encoding=UTF-8
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.9.3
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 2.1.11
|
91
|
+
signing_key:
|
92
|
+
specification_version: 4
|
93
|
+
summary: Requires files based on a pattern in a robust and optimistic manner.
|
94
|
+
test_files:
|
95
|
+
- spec/spec.rb
|