awesome_loader 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a5dc03dba0d176271e0c6c434bebb77c37839d6b
4
+ data.tar.gz: 7a0d3cffefadf3428d07a00284de1ec1cc85939d
5
+ SHA512:
6
+ metadata.gz: 2467eb61030682522223291cb5b6f1f43cbb446600a7831852375ed7cc0108798f99ca5cb1b46f442930d05d644c4f7cb72f30835fde00e541ea27bf7521a20c
7
+ data.tar.gz: 2dbeec359709d42235a86935ebb74982c95b7a602e37fb329934e837803bcb8ff52449e3eba1594b089d52052f2c25206d8cc4afcea2536de26d8bf952f72d2e
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2017 Jordan Hollinger
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ 'Software'), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # awesome_loader
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.)
4
+
5
+ ## Install
6
+
7
+ Just add to your Gemfile.
8
+
9
+ gem 'awesome_loader'
10
+
11
+ ## Basic Usage
12
+
13
+ Let's say your (non-Rails) app has a very Rails-like layout. `app/models`, `app/helpers`, etc. And those might have some subdirectories that correspond to module names. This is all you have to do:
14
+
15
+ # The root_depth argument is saying "Start creating modules for dirs after the first 2 levels"
16
+ AwesomeLoader.autoload root_depth: 2 do
17
+ paths %w(app ** *.rb)
18
+ end
19
+
20
+ ## Advanced Usage
21
+
22
+ Maybe your app structure is more complicated. That's fine too.
23
+
24
+ AwesomeLoader.autoload root_depth: 2 do
25
+ # These first few work just like above
26
+ paths %w(app models ** *.rb)
27
+ paths %w(app helpers ** *.rb)
28
+
29
+ # But the files in these dirs have top-level Routes and Entities modules
30
+ paths %w(app routes ** *.rb), root_depth: 1
31
+ paths %w(app entities ** *.rb), root_depth: 1
32
+ end
33
+
34
+ ## Eager Loading
35
+
36
+ If you're running a threaded server like Puma or Thin, it's often considered best practice to load everything
37
+ up-front, instead of possibly loading something during a request Thread. At least in production.
38
+
39
+ AwesomeLoader.autoload root_depth: 2, eager_load: true do
40
+ ...
41
+ end
42
+
43
+ ## License
44
+
45
+ MIT License. See LICENSE for details.
46
+
47
+ ## Copyright
48
+
49
+ Copyright (c) 2017 Jordan Hollinger
@@ -0,0 +1,67 @@
1
+ require 'set'
2
+
3
+ module AwesomeLoader
4
+ def self.autoload(root_depth:, root: Dir.cwd, eager_load: false, &block)
5
+ autoloader = Autoloader.new(root: root, root_depth: root_depth, eager_load: eager_load)
6
+ if block
7
+ autoloader.instance_eval(&block)
8
+ autoloader.finialize
9
+ end
10
+ autoloader
11
+ end
12
+
13
+ class Autoloader
14
+ RB_EXT = /\.rb$/
15
+
16
+ attr_reader :root
17
+ attr_reader :default_root_depth
18
+ attr_reader :eager_load
19
+ attr_reader :all_files
20
+
21
+ def initialize(root_depth:, root: Dir.cwd, eager_load: false)
22
+ @root = Pathname.new(root.to_s)
23
+ @default_root_depth, @eager_load = root_depth, eager_load
24
+ @all_files = []
25
+ end
26
+
27
+ def paths(array, root_depth: default_root_depth)
28
+ files = Dir.glob File.join *array
29
+ root_regex = Regexp.new "^#{File.join *array[0, root_depth]}/?"
30
+
31
+ # Get an array of nested dirs (parent dir always comes before child dir)
32
+ nested_dirs = files.
33
+ map { |path| File.dirname(path).sub(root_regex, '') }.uniq.
34
+ reduce(Set.new) { |dirs, leaf_dir|
35
+ dirs + leaf_dir.split('/').reduce([]) { |a, dir|
36
+ a << File.join(*a, dir)
37
+ }
38
+ }
39
+
40
+ # Create modules for each dir. Set a Hash of dir path => Module.
41
+ modules = nested_dirs.reduce({'.' => Object}) { |a, dir|
42
+ parent_module = a.fetch File.dirname dir
43
+ new_module = parent_module.const_set Utils.camelize(File.basename dir), Module.new
44
+ a[dir] = new_module
45
+ a
46
+ }
47
+
48
+ # For each file, look up it's dir module and set autoload on the class/module in the file.
49
+ files.each do |full_path|
50
+ rel_dir = File.dirname full_path.sub(root_regex, '')
51
+ abs_path = self.root.join(full_path)
52
+ file_const_name = Utils.camelize File.basename(full_path).sub(RB_EXT, '')
53
+ mod = modules.fetch rel_dir
54
+ mod.autoload file_const_name, abs_path
55
+ self.all_files << abs_path if eager_load
56
+ end
57
+
58
+ self
59
+ end
60
+
61
+ def finialize
62
+ all_files.each { |f| require f } if eager_load
63
+ all_files.clear
64
+ self
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,9 @@
1
+ module AwesomeLoader
2
+ module Utils
3
+ SNAKE = /_([a-z])/
4
+
5
+ def self.camelize(name)
6
+ name.capitalize.gsub(SNAKE) { |match| match[1].capitalize }
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ module AwesomeLoader
2
+ # Library version
3
+ VERSION = '0.0.1'.freeze
4
+ end
@@ -0,0 +1,3 @@
1
+ require 'awesome_loader/version'
2
+ require 'awesome_loader/autoloader'
3
+ require 'awesome_loader/utils'
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: awesome_loader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jordan Hollinger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: An awesome wrapper for Ruby's built-in autoload
14
+ email: jordan.hollinger@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE
20
+ - README.md
21
+ - lib/awesome_loader.rb
22
+ - lib/awesome_loader/autoloader.rb
23
+ - lib/awesome_loader/utils.rb
24
+ - lib/awesome_loader/version.rb
25
+ homepage: https://github.com/jhollinger/awesome_loader
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: 2.0.0
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.6.8
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: An awesome way to autoload your Ruby application
49
+ test_files: []