auto_autoloader 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 23a7863f796dd2d4e7fbe80e4213716523a96304
4
- data.tar.gz: 8db05b5d76a989407acadb404951995dfc86ce9a
3
+ metadata.gz: 3f47e3ec5e81116303c2f5df5593162a4d8f74f3
4
+ data.tar.gz: 508ad961d54d3f11aba4bb8c80bc3a96cdd11679
5
5
  SHA512:
6
- metadata.gz: 117b336e2683e9202dd4b42a4f7fcfd0710209c9f896a84220950d8c92bf374a44bb47f33654f0c8cdac4289620ab182b585155e0863b7bf7b2dcf40f76062a8
7
- data.tar.gz: b36d77e1ae057e65795b77988505c16b120953282654be66e0cac9b4775aa0b87caf266e3650495ac5304f526a4a9a93fa841078e211bc0069d2b8cf4f41e280
6
+ metadata.gz: 85ec2aedc6b7ec077738599acbb85797f7780c4321302107729cb0e52af58d842e9cb3c0710bce227d91783bd4c05128ea79d3660d598984b8c7548778065a9f
7
+ data.tar.gz: 71428878d872f89523e0bf829085065cb5047b74fe3c334adb95738e71aeb32d619f00fc81c8efb85bba3191687f5197786924f3dc6bd79b9a611e8dc24f31c6
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: auto_autoloader 0.0.1 ruby lib
5
+ # stub: auto_autoloader 0.0.2 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "auto_autoloader"
9
- s.version = "0.0.1"
9
+ s.version = "0.0.2"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["kaspernj"]
14
- s.date = "2016-01-03"
14
+ s.date = "2016-01-13"
15
15
  s.description = "Autoload constants in natural subfolders from the original class in Ruby with a single line of code."
16
16
  s.email = "k@spernj.org"
17
17
  s.extra_rdoc_files = [
@@ -30,6 +30,7 @@ Gem::Specification.new do |s|
30
30
  "VERSION",
31
31
  "auto_autoloader.gemspec",
32
32
  "lib/auto_autoloader.rb",
33
+ "lib/auto_autoloader/sub_class_autoloader.rb",
33
34
  "shippable.yml",
34
35
  "spec/auto_autoloader_spec.rb",
35
36
  "spec/spec_helper.rb",
@@ -38,7 +39,7 @@ Gem::Specification.new do |s|
38
39
  ]
39
40
  s.homepage = "http://github.com/kaspernj/auto_autoloader"
40
41
  s.licenses = ["MIT"]
41
- s.rubygems_version = "2.2.2"
42
+ s.rubygems_version = "2.4.0"
42
43
  s.summary = "Autoload constants in natural subfolders from the original class in Ruby with a single line of code."
43
44
 
44
45
  if s.respond_to? :specification_version then
@@ -0,0 +1,24 @@
1
+ class AutoAutoloader::SubClassAutoloader
2
+ module ClassMethods
3
+ def const_missing(const_name)
4
+ require "string-cases"
5
+
6
+ last_name = const_name.to_s.split("::").last
7
+ last_class_name = name.to_s.split("::").last
8
+
9
+ path = "#{@autoload_path}/#{::StringCases.camel_to_snake(last_class_name)}/#{::StringCases.camel_to_snake(last_name)}.rb"
10
+
11
+ if File.exist?(path)
12
+ require path
13
+
14
+ if const_defined?(last_name)
15
+ return const_get(last_name)
16
+ else
17
+ raise LoadError, "Expected path to define #{const_name} but it didnt: #{path}"
18
+ end
19
+ end
20
+
21
+ super
22
+ end
23
+ end
24
+ end
@@ -1,32 +1,42 @@
1
1
  class AutoAutoloader
2
2
  def self.autoload_sub_classes(base, path)
3
- base.extend AutoAutoloader::ClassMethods
3
+ loader = ::AutoAutoloader.new(base: base, path: path)
4
4
 
5
- base.class_eval do
6
- @autoload_path = File.dirname(path)
5
+ if loader.rails?
6
+ loader.autoload_by_active_support_and_file_scan
7
+ else
8
+ loader.autoload_with_sub_class_autoloader
7
9
  end
8
10
  end
9
11
 
10
- module ClassMethods
11
- def const_missing(const_name)
12
- require "string-cases"
12
+ def initialize(args)
13
+ @base = args.fetch(:base)
14
+ @path = args.fetch(:path)
15
+ end
13
16
 
14
- last_name = const_name.to_s.split("::").last
15
- last_class_name = name.to_s.split("::").last
17
+ def autoload_with_sub_class_autoloader
18
+ require_relative "auto_autoloader/sub_class_autoloader"
19
+ @base.extend ::AutoAutoloader::SubClassAutoloader::ClassMethods
20
+ path = @path
16
21
 
17
- path = "#{@autoload_path}/#{::StringCases.camel_to_snake(last_class_name)}/#{::StringCases.camel_to_snake(last_name)}.rb"
22
+ @base.class_eval do
23
+ @autoload_path = ::File.dirname(path)
24
+ end
25
+ end
18
26
 
19
- if File.exist?(path)
20
- require path
27
+ def autoload_by_active_support_and_file_scan
28
+ dir_path = "#{::File.dirname(@path)}/#{::StringCases.camel_to_snake(@base.name.split("::").last)}"
21
29
 
22
- if const_defined?(last_name)
23
- return const_get(last_name)
24
- else
25
- raise LoadError, "Expected path to define #{const_name} but it didnt: #{path}"
26
- end
27
- end
30
+ ::Dir.foreach(dir_path) do |file|
31
+ next unless (match = file.match(/\A(.+)\.rb\Z/))
32
+ const_snake_case = match[1]
33
+ const_camel_case = ::StringCases.snake_to_camel(const_snake_case)
28
34
 
29
- super
35
+ base.autoload(const_camel_case, "#{dir_path}/#{file}")
30
36
  end
31
37
  end
38
+
39
+ def rails?
40
+ @rails ||= ::Object.const_defined?("Rails")
41
+ end
32
42
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auto_autoloader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-03 00:00:00.000000000 Z
11
+ date: 2016-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: string-cases
@@ -128,6 +128,7 @@ files:
128
128
  - VERSION
129
129
  - auto_autoloader.gemspec
130
130
  - lib/auto_autoloader.rb
131
+ - lib/auto_autoloader/sub_class_autoloader.rb
131
132
  - shippable.yml
132
133
  - spec/auto_autoloader_spec.rb
133
134
  - spec/spec_helper.rb
@@ -153,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
153
154
  version: '0'
154
155
  requirements: []
155
156
  rubyforge_project:
156
- rubygems_version: 2.2.2
157
+ rubygems_version: 2.4.0
157
158
  signing_key:
158
159
  specification_version: 4
159
160
  summary: Autoload constants in natural subfolders from the original class in Ruby