awesome_loader 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/awesome_loader/autoloader.rb +54 -3
- data/lib/awesome_loader/utils.rb +10 -0
- data/lib/awesome_loader/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff578e319ae7955c039a49a12e1a7dd41f4af2db
|
4
|
+
data.tar.gz: 1044693107960f3dbfd2b1f78f3247316ae01502
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 116775922f484ed6eca46c5d5655f72bae1c5234e6b039c1e4f69734c195f0faffa1ccd4759538dc9c3a476fb41a4d607c8862dc0874c8cd529c255b7670b425
|
7
|
+
data.tar.gz: 71ed372da0aaeba0719423c971d9399027adf980ffb5472dc63611c8cf16797fb05e1af2557601e04b1e1398b0caff2a1262a2963dcca4f8d955f43118c88771
|
@@ -1,29 +1,77 @@
|
|
1
1
|
require 'set'
|
2
|
+
require 'pathname'
|
2
3
|
|
4
|
+
#
|
5
|
+
# A module that holds all the awesomeness.
|
6
|
+
#
|
3
7
|
module AwesomeLoader
|
8
|
+
#
|
9
|
+
# Set up an AwesomeLoader::Autoloader instance.
|
10
|
+
#
|
11
|
+
# AwesomeLoader.autoload root_depth: 2 do
|
12
|
+
# paths %w(app models ** *.rb)
|
13
|
+
# paths %w(app helpers ** *.rb)
|
14
|
+
# paths %w(app routes ** *.rb), root_depth: 1
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# @param root_depth [String] Tells AwesomeLoader to start creating Modules for dirs *after* this level
|
18
|
+
# @param root [String] Path to root of the application (default Dir.cwd)
|
19
|
+
# @param eager_load [Boolean] Make sure all files get loaded by the time the block finishes (default false)
|
20
|
+
# @return [AwesomeLoader::Autoloader]
|
21
|
+
#
|
4
22
|
def self.autoload(root_depth:, root: Dir.cwd, eager_load: false, &block)
|
5
23
|
autoloader = Autoloader.new(root: root, root_depth: root_depth, eager_load: eager_load)
|
6
24
|
if block
|
7
25
|
autoloader.instance_eval(&block)
|
8
|
-
autoloader.finialize
|
26
|
+
autoloader.finialize!
|
9
27
|
end
|
10
28
|
autoloader
|
11
29
|
end
|
12
30
|
|
31
|
+
#
|
32
|
+
# The autoloader. Normally it's used indirectly through AwesomeLoader.autoload, but you can use it directly if you like:
|
33
|
+
#
|
34
|
+
# AwesomeLoader::Autoloader.new(root_depth: 2).
|
35
|
+
# paths(%w(app models ** *.rb)).
|
36
|
+
# paths(%w(app helpers *.rb)).
|
37
|
+
# paths(%w(app routes ** *.rb), root_depth: 1).
|
38
|
+
# finialize!
|
39
|
+
#
|
13
40
|
class Autoloader
|
14
41
|
RB_EXT = /\.rb$/
|
15
42
|
|
43
|
+
# @return [Pathname] the application root
|
16
44
|
attr_reader :root
|
45
|
+
# @return [Integer] root depth used for all paths unless otherwise specified
|
17
46
|
attr_reader :default_root_depth
|
47
|
+
# @return [Boolean] whether or not to automatically load all files once they're defined
|
18
48
|
attr_reader :eager_load
|
49
|
+
# @return [Array<String>] all defined files, ready to be eager_loaded
|
19
50
|
attr_reader :all_files
|
51
|
+
private :all_files
|
20
52
|
|
53
|
+
#
|
54
|
+
# Initialize a new AwesomeLoader::Autoloader.
|
55
|
+
#
|
56
|
+
# @param root_depth [String] Tells AwesomeLoader to start creating Modules for dirs *after* this level
|
57
|
+
# @param root [String] Path to root of the application (default Dir.cwd)
|
58
|
+
# @param eager_load [Boolean] Make sure all files get loaded by the time the block finishes (default false)
|
59
|
+
#
|
21
60
|
def initialize(root_depth:, root: Dir.cwd, eager_load: false)
|
22
61
|
@root = Pathname.new(root.to_s)
|
23
62
|
@default_root_depth, @eager_load = root_depth, eager_load
|
24
63
|
@all_files = []
|
25
64
|
end
|
26
65
|
|
66
|
+
#
|
67
|
+
# Set a glob pattern of files to be autoloaded.
|
68
|
+
#
|
69
|
+
# autoloader.paths %w(app models ** *.rb)
|
70
|
+
#
|
71
|
+
# @param array [Array<String>] A glob pattern as an array.
|
72
|
+
# @paths root_depth [Integer] Depth at which to start creating modules for dirs. Defaults to whatever the AwesomeLoader::Autoloader instance was initialized with.
|
73
|
+
# @return [AwesomeLoader::Autoloader] returns self, so you can chain calls
|
74
|
+
#
|
27
75
|
def paths(array, root_depth: default_root_depth)
|
28
76
|
files = Dir.glob File.join *array
|
29
77
|
root_regex = Regexp.new "^([^/]+/){%d}" % root_depth
|
@@ -51,13 +99,16 @@ module AwesomeLoader
|
|
51
99
|
const_name = Utils.camelize File.basename(path).sub(RB_EXT, '')
|
52
100
|
mod = modules.fetch File.dirname path.sub(root_regex, '')
|
53
101
|
mod.autoload const_name, full_path
|
54
|
-
|
102
|
+
all_files << full_path if eager_load
|
55
103
|
end
|
56
104
|
|
57
105
|
self
|
58
106
|
end
|
59
107
|
|
60
|
-
|
108
|
+
#
|
109
|
+
# Perform any final operations or cleanup. If eager_load is true, this is where they're loaded.
|
110
|
+
#
|
111
|
+
def finialize!
|
61
112
|
all_files.each { |f| require f } if eager_load
|
62
113
|
all_files.clear
|
63
114
|
self
|
data/lib/awesome_loader/utils.rb
CHANGED
@@ -1,7 +1,17 @@
|
|
1
1
|
module AwesomeLoader
|
2
|
+
#
|
3
|
+
# Misc utilities.
|
4
|
+
#
|
2
5
|
module Utils
|
6
|
+
# Regex to match "snake name" paths
|
3
7
|
SNAKE = /_([a-z])/
|
4
8
|
|
9
|
+
#
|
10
|
+
# Converts a snake_case_name to a CamelCaseName.
|
11
|
+
#
|
12
|
+
# @param name [String] the snake_case version
|
13
|
+
# @return [String] the CamelCase version
|
14
|
+
#
|
5
15
|
def self.camelize(name)
|
6
16
|
name.capitalize.gsub(SNAKE) { |match| match[1].capitalize }
|
7
17
|
end
|