sweetloader 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.mdown CHANGED
@@ -26,19 +26,21 @@ end
26
26
 
27
27
  Each of these modules are then expected to be in @catango/configuration.rb@ and similar relative to a load path.
28
28
 
29
- ## AutoLoader Configuration
29
+ ## SweetLoader Configuration
30
30
 
31
- The following global config vars are available in AutoLoader:
31
+ The following global config vars are available in SweetLoader:
32
32
  * root
33
33
  * namespaces
34
34
  * mode
35
35
 
36
+ Note: AutoLoader is available as a deprecated alias for SweetLoader (versions < 1.5)
37
+
36
38
  ### Root
37
39
 
38
40
  Set a specific root which the dir will be calculated relative to, using root.
39
41
 
40
42
  ```ruby
41
- AutoLoader.root = 'fixtures'
43
+ SweetLoader.root = 'fixtures'
42
44
  ```
43
45
 
44
46
  Now the constant `::CanTango` will be resolved as `'fixtures/can_tango'`
@@ -48,20 +50,18 @@ Now the constant `::CanTango` will be resolved as `'fixtures/can_tango'`
48
50
  Normally the constant CanTango will be translated to the dir `'can_tango'`, here we override this so it will instead be translated to `'cantango'`.
49
51
 
50
52
  ```ruby
51
- AutoLoader.namespaces= {:CanTango => 'cantango', :Permithelper => 'permit_helper'}
53
+ SweetLoader.namespaces= {:CanTango => 'cantango', :Permithelper => 'permit_helper'}
52
54
  ```
53
55
 
54
56
  ### Modes
55
57
 
56
- AutoLoader supports the following modes: `:autoload, :require`
58
+ SweetLoader supports the following modes: `:autoload, :require`
57
59
 
58
- The mode `:require` is used to execute require statements directly. The `:autoload` mode is used to do autoloading.
59
- Note that autoloading is not stable in multi-threaded environments and will likely be deprecated in later version of Ruby.
60
- However autoloading does perform lazy loading and can be advantageous when you want to speed up initial load time and don't run in a multi-threaded environment.
60
+ The mode `:require` is used to execute require statements directly. The `:autoload` mode is used to do autoloading. Note that autoloading is not stable in multi-threaded environments and will likely be deprecated in later version of Ruby. However autoloading does perform lazy loading and can be advantageous when you want to speed up initial load time and don't run in a multi-threaded environment.
61
61
 
62
62
  ## autoload_modules Configuration
63
63
 
64
- You can also specify the AutoLoader options directly as an option hash on the `#autoload_modules` call:
64
+ You can also specify the SweetLoader options directly as an option hash on the `#autoload_modules` call:
65
65
 
66
66
  ```ruby
67
67
  autoload_modules :Configuration, :mode => :require
@@ -86,8 +86,8 @@ module AutoloadModules
86
86
  module Configuration
87
87
  # the scope options only have effect for autoload_modules statements within it!
88
88
  autoload_scope
89
- :ns => { :AutoloadModules => 'fixtures/autoload_modulez'},
90
- :mutate_path => Proc.new {|path| path.sub(/(ies)/, 'y') } do
89
+ :ns => { :AutoloadModules => 'fixtures/autoload_modulez'},
90
+ :mutate_path => Proc.new {|path| path.sub(/(ies)/, 'y') } do
91
91
  autoload_modules :Admin
92
92
  end
93
93
 
@@ -96,6 +96,31 @@ module AutoloadModules
96
96
  end
97
97
  ```
98
98
 
99
+ ## ClassExt
100
+
101
+ Sweetloader also encludes a convenient extension called `ClassExt`, which can be used to find a Module or Class based on a name.
102
+
103
+ ```ruby
104
+ module First
105
+ module Blip
106
+ def self.blip
107
+ end
108
+ end
109
+ end
110
+
111
+ module Second
112
+ include_and_extend ClassExt
113
+ include_and_extend First
114
+ end
115
+
116
+ Second.new.try_module('Blip').blip
117
+ Second.try_module('Blip').blip
118
+ Second.try_class('Blip') == nil # => ClassNotFoundError
119
+
120
+ Second.find_first_class(:Blop, :blip_blop, 'Blip') # => ClassNotFoundError
121
+ Second.find_first_module('Blop', :blip) # => Blip:Class
122
+ ```
123
+
99
124
  ## Contributing to sweetloader
100
125
 
101
126
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.1.5
data/lib/sweetloader.rb CHANGED
@@ -1,8 +1,11 @@
1
1
  require 'active_support/core_ext/string/inflections'
2
2
 
3
+ require 'sweetloader/class_methods'
3
4
 
4
5
  module SweetLoader
5
6
  class InvalidAutoloadMode < StandardError; end
7
+
8
+ extend ClassMethods
6
9
 
7
10
  def include_and_extend(the_module, options={})
8
11
  options[:instance_methods] ||= :InstanceMethods
@@ -38,7 +41,7 @@ module SweetLoader
38
41
  args.each do |module_name|
39
42
  ruby_file = module_name.to_s.underscore
40
43
  module_name = module_name.to_s.camelize.to_sym
41
- require_file = AutoLoader.translate("#{from}/#{ruby_file}", alm_options)
44
+ require_file = SweetLoader.translate("#{from}/#{ruby_file}", alm_options)
42
45
  logic.call(the_module, module_name, require_file)
43
46
  end
44
47
  end
@@ -66,7 +69,8 @@ class Module
66
69
  include SweetLoader
67
70
  end
68
71
 
72
+ # backwards (deprecated) alias
73
+ AutoLoader = SweetLoader
74
+
69
75
  require 'sweetloader/scope'
70
- require 'sweetloader/auto_loader'
71
76
  require 'sweetloader/class_ext'
72
-
@@ -1,7 +1,17 @@
1
- module AutoLoader
1
+ module SweetLoader
2
2
  module ClassMethods
3
3
  attr_writer :default_mode
4
4
 
5
+ def translate name, options = {}
6
+ names = name.split('/')
7
+ ns = namespaces.merge(options[:namespaces] || options[:ns] || {})
8
+ names.map do |name|
9
+ clazz_name = name.to_s.camelize
10
+ folder = ns[clazz_name.to_sym] ? ns[clazz_name.to_sym] : name
11
+ folder.sub /\/$/, ''
12
+ end.join('/')
13
+ end
14
+
5
15
  def root
6
16
  @root ||= ''
7
17
  end
@@ -35,20 +45,10 @@ module AutoLoader
35
45
  def valid_modes
36
46
  [:autoload, :require]
37
47
  end
38
-
48
+
39
49
  def default_mode
40
50
  valid_modes.first
41
51
  end
42
52
  end
43
53
  extend ClassMethods
44
-
45
- def self.translate name, options = {}
46
- names = name.split('/')
47
- ns = namespaces.merge(options[:namespaces] || options[:ns] || {})
48
- names.map do |name|
49
- clazz_name = name.to_s.camelize
50
- folder = ns[clazz_name.to_sym] ? ns[clazz_name.to_sym] : name
51
- folder.sub /\/$/, ''
52
- end.join('/')
53
- end
54
54
  end
@@ -30,13 +30,13 @@ describe Module do
30
30
 
31
31
  context 'using AutoLoader.root' do
32
32
  it 'empty root' do
33
- AutoLoader.root = ''
33
+ SweetLoader.root = ''
34
34
  require 'autoload_blank_root'
35
35
  AutoloadBlankRoot::Hello.should respond_to(:test)
36
36
  end
37
37
 
38
38
  it 'should autoload modules using ClassExt#autoload_root' do
39
- AutoLoader.root = 'fixtures'
39
+ SweetLoader.root = 'fixtures'
40
40
  require 'fixtures/autoload_modules_root'
41
41
  AutoloadModulesRoot::Third.should respond_to(:test)
42
42
  end
@@ -44,8 +44,8 @@ describe Module do
44
44
 
45
45
  context 'using AutoLoader.namespaces' do
46
46
  it 'empty root' do
47
- AutoLoader.root = ''
48
- AutoLoader.namespaces= {:AutoLoadBlankRoot => 'autoload_blank_root', :HelloSailor => 'sailor'}
47
+ SweetLoader.root = ''
48
+ SweetLoader.namespaces= {:AutoLoadBlankRoot => 'autoload_blank_root', :HelloSailor => 'sailor'}
49
49
  require 'auto_load_blank_root'
50
50
  AutoLoadBlankRoot::HelloSailor.should respond_to(:test)
51
51
  end
data/sweetloader.gemspec CHANGED
@@ -4,14 +4,14 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = "sweetloader"
8
- s.version = "0.1.4"
7
+ s.name = %q{sweetloader}
8
+ s.version = "0.1.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Kristian Mandrup"]
12
- s.date = "2011-11-29"
13
- s.description = "sweet autoloading using file structure conventions while allowing configuration overrides for special cases"
14
- s.email = "kmandrup@gmail.com"
11
+ s.authors = [%q{Kristian Mandrup}]
12
+ s.date = %q{2011-11-29}
13
+ s.description = %q{sweet autoloading using file structure conventions while allowing configuration overrides for special cases}
14
+ s.email = %q{kmandrup@gmail.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE.txt",
17
17
  "README.mdown"
@@ -25,8 +25,8 @@ Gem::Specification.new do |s|
25
25
  "Rakefile",
26
26
  "VERSION",
27
27
  "lib/sweetloader.rb",
28
- "lib/sweetloader/auto_loader.rb",
29
28
  "lib/sweetloader/class_ext.rb",
29
+ "lib/sweetloader/class_methods.rb",
30
30
  "lib/sweetloader/scope.rb",
31
31
  "spec/auto_load_blank_root.rb",
32
32
  "spec/autoload_blank_root.rb",
@@ -57,11 +57,11 @@ Gem::Specification.new do |s|
57
57
  "spec/sweetloader/sweetloader_spec.rb",
58
58
  "sweetloader.gemspec"
59
59
  ]
60
- s.homepage = "http://github.com/kristianmandrup/sweetloader"
61
- s.licenses = ["MIT"]
62
- s.require_paths = ["lib"]
63
- s.rubygems_version = "1.8.10"
64
- s.summary = "sweetens up autoloading using file structure conventions"
60
+ s.homepage = %q{http://github.com/kristianmandrup/sweetloader}
61
+ s.licenses = [%q{MIT}]
62
+ s.require_paths = [%q{lib}]
63
+ s.rubygems_version = %q{1.8.6}
64
+ s.summary = %q{sweetens up autoloading using file structure conventions}
65
65
 
66
66
  if s.respond_to? :specification_version then
67
67
  s.specification_version = 3
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sweetloader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-11-29 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &899195900 !ruby/object:Gem::Requirement
16
+ requirement: &70281404735240 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.0.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *899195900
24
+ version_requirements: *70281404735240
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: i18n
27
- requirement: &899195210 !ruby/object:Gem::Requirement
27
+ requirement: &70281404734700 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *899195210
35
+ version_requirements: *70281404734700
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &899194770 !ruby/object:Gem::Requirement
38
+ requirement: &70281404733980 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 2.5.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *899194770
46
+ version_requirements: *70281404733980
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bundler
49
- requirement: &899194410 !ruby/object:Gem::Requirement
49
+ requirement: &70281404733120 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.1.rc
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *899194410
57
+ version_requirements: *70281404733120
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: jeweler
60
- requirement: &899194080 !ruby/object:Gem::Requirement
60
+ requirement: &70281404732360 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 1.6.4
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *899194080
68
+ version_requirements: *70281404732360
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rcov
71
- requirement: &899193050 !ruby/object:Gem::Requirement
71
+ requirement: &70281404731500 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *899193050
79
+ version_requirements: *70281404731500
80
80
  description: sweet autoloading using file structure conventions while allowing configuration
81
81
  overrides for special cases
82
82
  email: kmandrup@gmail.com
@@ -94,8 +94,8 @@ files:
94
94
  - Rakefile
95
95
  - VERSION
96
96
  - lib/sweetloader.rb
97
- - lib/sweetloader/auto_loader.rb
98
97
  - lib/sweetloader/class_ext.rb
98
+ - lib/sweetloader/class_methods.rb
99
99
  - lib/sweetloader/scope.rb
100
100
  - spec/auto_load_blank_root.rb
101
101
  - spec/autoload_blank_root.rb
@@ -140,7 +140,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
140
140
  version: '0'
141
141
  segments:
142
142
  - 0
143
- hash: -951501357
143
+ hash: 1025158195221417970
144
144
  required_rubygems_version: !ruby/object:Gem::Requirement
145
145
  none: false
146
146
  requirements:
@@ -149,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
149
  version: '0'
150
150
  requirements: []
151
151
  rubyforge_project:
152
- rubygems_version: 1.8.10
152
+ rubygems_version: 1.8.6
153
153
  signing_key:
154
154
  specification_version: 3
155
155
  summary: sweetens up autoloading using file structure conventions