awesome_loader 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 362e0c1826dd0bcde66b8e466799b0b37743f2f2
4
- data.tar.gz: 0d981dffe720657efa76c056b37b4d4f09ad8ae3
3
+ metadata.gz: 251b662a3b3fa4fbb57b49ba2221129c156f8844
4
+ data.tar.gz: bfa7481ad5aaddabb1f936f37a0cf7fede8fcf8c
5
5
  SHA512:
6
- metadata.gz: e7d754fce596bd375ed6dec3f11341b10e76e8f7320b5ba6ae3aed577cff50f6ef4614b17358054cd784c48974623af38672c0c288b80c07970d2274b59fe6cb
7
- data.tar.gz: 905b71a2043b2334334f1472eab79d2d4bf0709892c7d691568410e815318ac76f805bbce5ad566ae2198b2176a3624a6d6c0b7d846ea276a7dfac77b15b78f1
6
+ metadata.gz: f5a3019008b0bd2ca68d58a3625603606bdeeb3ed1c9f352dbd05c80c0bcd2b8f3b4aaf165af9e1b61028c79f3fcfa52013303dd2e5918f30e494c75e6d8c712
7
+ data.tar.gz: 2e198a51f1ebced488274f322971a21e64375086bfd53600714d26e95bfb33abe668063b56c917dec3a25a046480d9431e1cbee06cf16edb68f280268c0865c1
data/README.md CHANGED
@@ -1,33 +1,30 @@
1
1
  # awesome_loader
2
2
 
3
- An awesome way to load your (non-Rails) Ruby application! Tired of tons of manual requires? Me too! So I made this little gem to augment Ruby's built-in `autoload` functionality. Works pretty much the same as in Rails. (NOTE Don't confuse this with auto-**re**loading.)
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
- Just add to your Gemfile.
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/user.rb` contains `User`
18
- * `app/models/widget.rb` contains `Widget`
19
- * `app/models/billing/line_item.rb` contains `Billing::LineItem`
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`. 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.
19
+ Given those files and their contents, this is all you have to tell `awesome_loader`. Awesome, right?
23
20
 
24
- AwesomeLoader.autoload root_depth: 2 do
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 root_depth: 2, eager_load: true do
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 [String] Tells AwesomeLoader to start creating Modules for dirs *after* this level
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:, root_path: Dir.pwd, root_module: Object, eager_load: false, &block)
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, but you can use it directly if you like:
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 [String] Tells AwesomeLoader to start creating Modules for dirs *after* this level
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:, root_path: Dir.pwd, root_module: Object, eager_load: false)
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 (building if necessary) the Module represented by the dir path. The path should be relative
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 that should contain the constant in the file.
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
@@ -1,4 +1,4 @@
1
1
  module AwesomeLoader
2
2
  # Library version
3
- VERSION = '1.0.0'.freeze
3
+ VERSION = '1.1.0'.freeze
4
4
  end
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.0.0
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-09 00:00:00.000000000 Z
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