require-magic 0.5.1 → 0.5.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.textile +186 -0
  2. data/lib/require-magic.rb +16 -13
  3. metadata +3 -3
  4. data/README.rdoc +0 -122
@@ -0,0 +1,186 @@
1
+ h1. Require-Me
2
+ Includes a DSL for requiring files and folders and some also some static utility functions which can be used in combination.
3
+ These tools in combination facilitates managing requiring various subfolder structures.
4
+
5
+ h2. Require DSL
6
+ The following example code demonstrates how to use the Require DSL
7
+
8
+ <pre>
9
+ require 'require_me' # include both the static require helpers and the DSL require language
10
+ require 'require_dsl' # alternatively only include require_dsl (the DSL language)
11
+
12
+ # enter subfolder 'mira'
13
+ Folder.enter 'mira' do |folder|
14
+ # from new location, enter a subdir
15
+ folder.enter 'subdir' do |path| # mira/subdir
16
+ folder.all('**/*.rb').except(/sound\/*.rb/).require
17
+ end
18
+
19
+ # from new location, enter a subdir
20
+ folder.enter 'another/subdir' do |path|
21
+ # use file blobs here
22
+ folder.all('**/*.rb').require
23
+ end
24
+
25
+ # from new location, enter a subdir
26
+ folder.enter 'a_subdir' do |path|
27
+ # matching and except are to be used as include and exclude filters
28
+ # they each take a list containing regular expressions and strings
29
+ # string arguments are postfixed with .rb internally if not present
30
+ folder.all('blip/**/*.rb').matching(/_mixin.rb/, /.*\/power/).except(/sound/, /disco/).require
31
+
32
+ folder.enter 'sub_a' do |path|
33
+ folder.enter 'sub_b' do |path| # a_subdir/sub_a/sub_b
34
+ folder.all('grusch/**/*.rb').require
35
+ end
36
+
37
+ end
38
+ folder.all.require
39
+ end
40
+ end
41
+ </pre>
42
+
43
+ If no argument, current path is used as initial folder
44
+ <pre>
45
+ # use current path as folder
46
+ Folder.enter do |folder|
47
+ folder.all('**/*.rb').require
48
+ folder.enter 'game' do |path|
49
+
50
+ # use static require functions
51
+ Require.base_path path # set base path to use for Require
52
+
53
+ # include .rb files within data1, data2 but not within their subfolders (use recursive instead)
54
+ Require.folders('data1', 'data2')
55
+
56
+ list = path.all('**/*.rb')
57
+ puts list.matching('sound', 'network').except(/sound/).show_require(:relative)
58
+ list.matching('sound', 'network').except(/sound/).require
59
+ end
60
+ end
61
+ </pre>
62
+
63
+ h2. Static helpers
64
+ Unit tests demonstrations how to use the static helpers (tests currently broken due to missing data files!):
65
+
66
+ h3. Setting the base path
67
+
68
+ Setting global base_path
69
+ <pre>
70
+ # Set basepath to use for require
71
+ Require.base_path = File.dirname(__FILE__)
72
+ </pre>
73
+
74
+ Set basepath to use within block
75
+ <pre>
76
+ # Set basepath to use within block
77
+ Require.enter 'sound' do |path|
78
+ Require.folders 'data' # can be used for any number of folders
79
+ Require.folder 'data2' # for one folder only
80
+ end
81
+ </pre>
82
+
83
+ Override base_path
84
+ <pre>
85
+ # Override base_path
86
+ Require.folders 'data', {:base_path => File.dirname(__FILE__) + '/../my/path}
87
+ </pre>
88
+
89
+ h3. Simple usage examples
90
+ Require .rb files from a folder
91
+ <pre>
92
+ # To require .rb files in the folders 'data' and 'data2' (non-recursively)
93
+ Require.folders 'data', 'data2'
94
+
95
+ # Same but recursively (recurse subtrees of folders)
96
+ Require.recursive 'data', 'data2'
97
+ </pre>
98
+
99
+ h3. Simple debugging
100
+ Get list of required files
101
+ <pre>
102
+ # To require all files within the top level folder 'data' (non-recursively)
103
+ # The required_files returned is a list of the paths of the files that were required
104
+ required_files = Require.folder 'data'
105
+ puts required_files
106
+ </pre>
107
+
108
+ h3. Tracing mode (for debugging)
109
+ Apply tracing to see output for the process of requiring the files
110
+ <pre>
111
+ # turn on tracing globally
112
+ Require.tracing = :on
113
+ Require.folder 'data'
114
+ # turn off tracing globally
115
+ Require.tracing = :off
116
+ </pre>
117
+
118
+ Alternatively pass tracing as an option
119
+ <pre>
120
+ Require.folder 'data', {:tracing => :on}
121
+ </pre>
122
+
123
+ h3. Verbose mode (for detailed debugging)
124
+ Set verbose mode on to see full path of each required file
125
+ <pre>
126
+ # turn on tracing
127
+ Require.tracing = :on
128
+ # turn on verbose globally
129
+ Require.verbose = :on
130
+ Require.folder 'data'
131
+ # turn off verbose globally
132
+ Require.verbose = :off
133
+ </pre>
134
+
135
+ h3. Require.recursive
136
+ To require all files within the top level folder 'data' recursively
137
+ <pre>
138
+ # To require all files within the top level folder 'data' recursively
139
+ required_files = Require.recursive 'data'
140
+ </pre>
141
+
142
+ To require all files within the top level folders 'data' and 'data2' (non-recursively)
143
+ <pre>
144
+ # To require all files within the top level folders 'data' and 'data2' (non-recursively)
145
+ required_files = Require.recursive 'data', 'data2'
146
+ </pre>
147
+
148
+ To require all files within the top level folders 'data' and 'data2' recursively
149
+ <pre>
150
+ # To require all files within the top level folders 'data' and 'data2' recursively
151
+ required_files = Require.recursive 'data', 'data2'
152
+ </pre>
153
+
154
+ h3. Require.folders
155
+ To require files within the top level folders 'data' and 'data2' and also files within the subdirectory 'blip' if it exists
156
+ <pre>
157
+ # To require files within the top level folders 'data' and 'data2' and also files within the subdirectory 'blip' if it exists
158
+ required_files = Require.folders 'data', 'data2', {:folders => 'blip'}
159
+ </pre>
160
+
161
+ To require files within 'data/blip' and 'data2/blip' only, NOT including the root files
162
+ <pre>
163
+ # To require files within 'data/blip' and 'data2/blip' only, NOT including the root files
164
+ required_files = Require.folders 'data', 'data2', {:folders => 'blip', :ignore_root_files => true}
165
+ </pre>
166
+
167
+ To require files within 'data' and 'data2' first and then AFTER any files within the subdirectory 'blip' (default order)
168
+ <pre>
169
+ # To require files within 'data' and 'data2' first and then AFTER any files within the subdirectory 'blip' (default order)
170
+ required_files = Require.folders 'data', 'data2', {:folders => 'blip', :root_files => :before}
171
+ </pre>
172
+
173
+
174
+ To require files within 'data/blip' and 'data2/blip' first and then AFTER any files within 'data' and 'data2' folders (the root files)
175
+ <pre>
176
+ required_files = Require.folders(['data', 'data2'], {:folders => 'blip', :root_files => :after})
177
+ </pre>
178
+
179
+ To require files within 'data' and 'data2' (the root files) first (BEFORE) and then any files within the subdirectories 'blip' and blap
180
+ <pre>
181
+ required_files = Require.folders(['data', 'data2'], {:folders => ['blip', 'blap'], :root_files => :before})
182
+ </pre>
183
+
184
+ h2. Copyright
185
+
186
+ Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.
@@ -26,8 +26,9 @@ module Require
26
26
 
27
27
  def self.enter(name, options = {}, &block)
28
28
  options[:recursive] = true
29
- file folder(name, options)
30
- yield File.dirname(file)
29
+ file = folder(name, options)
30
+ base_path = File.dirname(file)
31
+ yield base_path
31
32
  end
32
33
 
33
34
 
@@ -123,18 +124,20 @@ protected
123
124
  end
124
125
  end
125
126
 
126
- def self.match(f, rexp)
127
- if rexp.kind_of? String
128
- rexp = /#{Regexp.escape(rexp)}/
129
- end
130
-
131
- if rexp.kind_of? Array
132
- rexp.each{|e| return true if match(f, rexp)}
127
+ def self.match(f, *rexp)
128
+ rexp.each{|e| return true if match_single(f, rexp)}
133
129
  false
134
- elsif rexp.kind_of? Regexp
135
- match_res = (f =~ rexp)
136
- !match_res.nil?
130
+ end
131
+
132
+ def self.match_single(f, rexp)
133
+ if rexp.kind_of? String
134
+ str = Regexp.escape(str)
135
+ str.gsub! '\*', '[a-zA-Z0-9\s_-]*'
136
+ str.gsub! '/', '\/'
137
137
  end
138
+ if rexp.kind_of? Regexp
139
+ return f.match(re)
140
+ end
141
+ false
138
142
  end
139
-
140
143
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: require-magic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kristian Mandrup
@@ -21,13 +21,13 @@ extensions: []
21
21
 
22
22
  extra_rdoc_files:
23
23
  - LICENSE
24
- - README.rdoc
24
+ - README.textile
25
25
  files:
26
26
  - lib/require-dsl.rb
27
27
  - lib/require-magic.rb
28
28
  - lib/util/util.rb
29
29
  - LICENSE
30
- - README.rdoc
30
+ - README.textile
31
31
  has_rdoc: true
32
32
  homepage: http://github.com/kristianmandrup/require-magic
33
33
  licenses: []
@@ -1,122 +0,0 @@
1
- = require-magic
2
-
3
- Utility functions to facilitate importing (require) ruby files in complex Ruby project, such as gem with complex folder hierarchies.
4
- See unit tests in /test directory to see how to use the tools for the best effect.
5
- Using this toolset should really simplify your require statements and make your application more flexible to change.
6
-
7
- == USAGE
8
-
9
- <code>
10
- require 'require_magic' # include both the old static require helpers and the DSL require language
11
- require 'require_dsl' # alternatively only include require_dsl (the DSL language)
12
-
13
- Folder.enter [path relative to current file location] do |folder|
14
- # from new location, enter a subdir
15
- folder.enter [subdir] do |path|
16
- folder.all('**/*.rb').except(/sound\/*.rb/).require
17
- end
18
-
19
- # from new location, enter a subdir
20
- folder.enter [another subdir] do |path|
21
- # use file blobs here
22
- folder.all('**/*.rb').require
23
- end
24
-
25
- # from new location, enter a subdir
26
- folder.enter [yet another subdir] do |path|
27
- # matching and except are to be used as include and exclude filters
28
- # they each take a list containing regular expressions and strings
29
- # string arguments are postfixed with .rb internally if not present
30
- folder.all('blip/**/*.rb').matching(/_mixin.rb/, /.*\/power/).except(/sound/, /disco/).require
31
-
32
- folder.enter [subdir] do |path|
33
- folder.enter [subdir] do |path|
34
- folder.all('grusch/**/*.rb').require
35
- end
36
-
37
- end
38
- folder.all.require
39
- end
40
-
41
- end
42
-
43
- # use current path as folder
44
- Folder.enter do |folder|
45
- folder.all('**/*.rb').require
46
- folder.enter 'game' do |path|
47
- list = folder.all('**/*.rb')
48
- # puts list.matching('sound', 'network').except(/sound/).show_require(:relative)
49
- list.matching('sound', 'network').except(/sound/).require
50
- end
51
- end
52
- </code>
53
-
54
- == Static helpers
55
- See unit tests for demonstrations of how to use it:
56
-
57
- # Set basepath to use for require
58
- <code>
59
- required_files = Require.base_path = File.dirname(__FILE__)
60
- </code>
61
-
62
- To require all files within the top level folder 'data' (non-recursively)
63
- <code>
64
- required_files = Require.folders('data')
65
- </code>
66
-
67
- Override base_path
68
- <code>
69
- required_files = Require.folders('data', {:base_path => File.dirname(__FILE__) + '/../my/path})
70
- </code>
71
-
72
- The required_files returned is a list of the paths of the files that were required
73
-
74
- To require all files within the top level folder 'data' (non-recursively) and apply tracing to see output for the process of requiring the files
75
- <code>
76
- required_files = Require.folder 'data'
77
- required_files = Require.folders 'data'
78
- </code>
79
-
80
- To require all files within the top level folder 'data' recursively
81
- <code>
82
- required_files = Require.recursive('data')
83
- </code>
84
-
85
- To require all files within the top level folders 'data' and 'data2' (non-recursively)
86
- <code>
87
- required_files = Require.recursive(['data', 'data2'])
88
- </code>
89
-
90
- To require all files within the top level folders 'data' and 'data2' recursively
91
- <code>
92
- required_files = Require.recursive(['data', 'data2'])
93
- </code>
94
-
95
- To require files within the top level folders 'data' and 'data2' and also files within the subdirectory 'blip' if it exists
96
- <code>
97
- required_files = Require.folders(['data', 'data2'], {:folders => ['blip]})
98
- </code>
99
-
100
- To require files within 'data/blip' and 'data2/blip' only, NOT including the root files
101
- <code>
102
- required_files = Require.folders(['data', 'data2'], {:folders => ['blip], :ignore_root_files => true})
103
- </code>
104
-
105
- To require files within 'data' and 'data2' first and then AFTER any files within the subdirectory 'blip' (default order)
106
- <code>
107
- required_files = Require.folders(['data', 'data2'], {:folders => ['blip], :root_files => :before})
108
- </code>
109
-
110
- To require files within 'data/blip' and 'data2/blip' first and then AFTER any files within 'data' and 'data2' folders (the root files)
111
- <code>
112
- required_files = Require.folders(['data', 'data2'], {:folders => ['blip], :root_files => :after})
113
- </code>
114
-
115
- To require files within 'data' and 'data2' (the root files) first (BEFORE) and then any files within the subdirectory 'blip'
116
- <code>
117
- required_files = Require.folders(['data', 'data2'], {:folders => ['blip], :root_files => :before})
118
- </code>
119
-
120
- == Copyright
121
-
122
- Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.