awesome_loader 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +11 -12
- data/lib/awesome_loader/autoloader.rb +7 -5
- data/lib/awesome_loader/module_builder.rb +5 -2
- data/lib/awesome_loader/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 251b662a3b3fa4fbb57b49ba2221129c156f8844
|
4
|
+
data.tar.gz: bfa7481ad5aaddabb1f936f37a0cf7fede8fcf8c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f5a3019008b0bd2ca68d58a3625603606bdeeb3ed1c9f352dbd05c80c0bcd2b8f3b4aaf165af9e1b61028c79f3fcfa52013303dd2e5918f30e494c75e6d8c712
|
7
|
+
data.tar.gz: 2e198a51f1ebced488274f322971a21e64375086bfd53600714d26e95bfb33abe668063b56c917dec3a25a046480d9431e1cbee06cf16edb68f280268c0865c1
|
data/README.md
CHANGED
@@ -1,33 +1,30 @@
|
|
1
1
|
# awesome_loader
|
2
2
|
|
3
|
-
|
3
|
+
So you've created your bespoke Ruby application without Rails. Then you thought, "Bollocks, I have to manually require all my application files, and in a certain order! And I have to explicitly define all my submodules - they don't magically appear based on the directory structure like they did in Rails. There has to be a better way!" Well now there is.
|
4
4
|
|
5
5
|
## Install
|
6
6
|
|
7
|
-
|
7
|
+
Add to your Gemfile.
|
8
8
|
|
9
9
|
gem 'awesome_loader'
|
10
10
|
|
11
11
|
## Basic Usage
|
12
12
|
|
13
|
-
`awesome_loader` assumes that your directories and files are all in "snake case" (my_dir/my_file.rb), and that your Ruby Modules and Classes are all in "camel case" (MyDir::MyFile). Additionally, it assumes that your directory structure matches your Module structure. In fact it will traverse your directory tree and make sure all those Modules are created for you.
|
14
|
-
|
15
13
|
Let's say you have a fairly simple layout, somewhat inspired by Rails.
|
16
14
|
|
17
|
-
* `app/models/
|
18
|
-
* `app/models/
|
19
|
-
* `app/
|
20
|
-
* `app/helpers/app_helpers.rb` contains `AppHelpers`
|
15
|
+
* `app/models/widget.rb` contains `class Widget`
|
16
|
+
* `app/models/billing/line_item.rb` contains `class Billing::LineItem`
|
17
|
+
* `app/helpers/app_helpers.rb` contains `module AppHelpers`
|
21
18
|
|
22
|
-
Given those files and their contents, this is all you have to tell `awesome_loader`.
|
19
|
+
Given those files and their contents, this is all you have to tell `awesome_loader`. Awesome, right?
|
23
20
|
|
24
|
-
AwesomeLoader.autoload
|
21
|
+
AwesomeLoader.autoload do
|
25
22
|
paths %w(app ** *.rb)
|
26
23
|
end
|
27
24
|
|
28
25
|
## Advanced Usage
|
29
26
|
|
30
|
-
Maybe your app structure is more complicated. That's fine too.
|
27
|
+
Maybe your app structure is more complicated. That's fine too. Note the `root_depth: 2` argument. That's saying, "Only start creating modules for dirs after the first 2 levels." That means `app` and `app/*` won't get any modules, but deeper directories, like `app/models/billing`, will. `2` is the default, as you can see in the above example.
|
31
28
|
|
32
29
|
AwesomeLoader.autoload root_depth: 2 do
|
33
30
|
# These first few work just like above
|
@@ -39,11 +36,13 @@ Maybe your app structure is more complicated. That's fine too.
|
|
39
36
|
paths %w(app entities ** *.rb), root_depth: 1
|
40
37
|
end
|
41
38
|
|
39
|
+
For more details and options, [check out the documentation](http://www.rubydoc.info/gems/awesome_loader).
|
40
|
+
|
42
41
|
## Eager Loading
|
43
42
|
|
44
43
|
If you're running a threaded server like Puma or Thin, it's usually considered best practice to load everything up-front (at least in production), instead of lettings things load while other threads might be running. The `eager_load` option will ensure that all files are loaded before the block exits.
|
45
44
|
|
46
|
-
AwesomeLoader.autoload
|
45
|
+
AwesomeLoader.autoload eager_load: is_prod? do
|
47
46
|
paths %w(app ** *.rb)
|
48
47
|
end
|
49
48
|
|
@@ -14,13 +14,13 @@ module AwesomeLoader
|
|
14
14
|
# paths %w(app routes ** *.rb), root_depth: 1
|
15
15
|
# end
|
16
16
|
#
|
17
|
-
# @param root_depth [
|
17
|
+
# @param root_depth [Integer] Tells AwesomeLoader to start creating Modules for dirs *after* this level (default 2)
|
18
18
|
# @param root_path [String] Path to root of the application (default Dir.pwd)
|
19
19
|
# @param root_module [Module] Module to load your modules into (default Object). You'll probably always want to keep the default.
|
20
20
|
# @param eager_load [Boolean] Make sure all files get loaded by the time the block finishes (default false)
|
21
21
|
# @return [AwesomeLoader::Autoloader]
|
22
22
|
#
|
23
|
-
def self.autoload(root_depth
|
23
|
+
def self.autoload(root_depth: Autoloader::DEFAULT_ROOT_DEPTH, root_path: Dir.pwd, root_module: Object, eager_load: false, &block)
|
24
24
|
autoloader = Autoloader.new(root_depth: root_depth, root_path: root_path, root_module: root_module, eager_load: eager_load)
|
25
25
|
if block
|
26
26
|
autoloader.instance_eval(&block)
|
@@ -30,7 +30,7 @@ module AwesomeLoader
|
|
30
30
|
end
|
31
31
|
|
32
32
|
#
|
33
|
-
# The autoloader. Normally it's used indirectly through AwesomeLoader.autoload
|
33
|
+
# The autoloader. Normally it's used indirectly through `AwesomeLoader.autoload`, but you can use it directly if you like:
|
34
34
|
#
|
35
35
|
# AwesomeLoader::Autoloader.new(root_depth: 2).
|
36
36
|
# paths(%w(app models ** *.rb)).
|
@@ -39,6 +39,8 @@ module AwesomeLoader
|
|
39
39
|
# finalize!
|
40
40
|
#
|
41
41
|
class Autoloader
|
42
|
+
# The default root_dept for new instances
|
43
|
+
DEFAULT_ROOT_DEPTH = 2
|
42
44
|
RB_EXT = /\.rb$/
|
43
45
|
|
44
46
|
# @return [Integer] root depth used for all paths unless otherwise specified
|
@@ -56,12 +58,12 @@ module AwesomeLoader
|
|
56
58
|
#
|
57
59
|
# Initialize a new AwesomeLoader::Autoloader.
|
58
60
|
#
|
59
|
-
# @param root_depth [
|
61
|
+
# @param root_depth [Integer] Tells AwesomeLoader to start creating Modules for dirs *after* this level (default 2)
|
60
62
|
# @param root_path [String] Path to root of the application (default Dir.pwd)
|
61
63
|
# @param root_module [Module] Module to load your modules into (default Object). You'll probably always want to keep the default.
|
62
64
|
# @param eager_load [Boolean] Make sure all files get loaded by the time the block finishes (default false)
|
63
65
|
#
|
64
|
-
def initialize(root_depth
|
66
|
+
def initialize(root_depth: DEFAULT_ROOT_DEPTH, root_path: Dir.pwd, root_module: Object, eager_load: false)
|
65
67
|
@root_path, @root_module = Pathname.new(root_path.to_s), root_module
|
66
68
|
@default_root_depth, @eager_load = root_depth, eager_load
|
67
69
|
@all_files = Set.new
|
@@ -20,7 +20,7 @@ module AwesomeLoader
|
|
20
20
|
end
|
21
21
|
|
22
22
|
#
|
23
|
-
# Returns (
|
23
|
+
# Returns (recursively creating if necessary) the Module represented by the dir path. The path should be relative
|
24
24
|
# to your application root/working directory.
|
25
25
|
#
|
26
26
|
# # Since root_depth is 2, the first 2 dirs in any path will be ignored
|
@@ -36,7 +36,7 @@ module AwesomeLoader
|
|
36
36
|
# => Billing::Foo
|
37
37
|
#
|
38
38
|
# @param rel_filepath [String] The path, relative to your application root, to the file you want the module for.
|
39
|
-
# @return [Module] The module
|
39
|
+
# @return [Module] The module
|
40
40
|
#
|
41
41
|
def module(rel_path)
|
42
42
|
module_names(rel_path).reduce(root_module) { |parent_mod, mod_name|
|
@@ -63,6 +63,9 @@ module AwesomeLoader
|
|
63
63
|
# builder.nested_dirs('src/features/billing/foo')
|
64
64
|
# => ['Billing', 'Foo']
|
65
65
|
#
|
66
|
+
# @param rel_filepath [String] The path, relative to your application root, to the file you want the module for.
|
67
|
+
# @return [Array<String>] Array of nested Module names
|
68
|
+
#
|
66
69
|
def module_names(rel_path)
|
67
70
|
dir_names = Utils.clean_path(rel_path).split '/'
|
68
71
|
return [] if rel_path == '.' or root_depth > dir_names.size
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: awesome_loader
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jordan Hollinger
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-04-
|
11
|
+
date: 2017-04-10 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: An awesome wrapper for Ruby's built-in autoload
|
14
14
|
email: jordan.hollinger@gmail.com
|