sweetloader 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -7,7 +7,7 @@ gem "i18n"
7
7
  # Include everything needed to run rake, tests, features, etc.
8
8
  group :development do
9
9
  gem "rspec", ">= 2.5.0"
10
- gem "bundler", "~> 1.0.10"
10
+ gem "bundler", ">= 1.1.rc"
11
11
  gem "jeweler", ">= 1.6.4"
12
12
  gem "rcov", ">= 0"
13
13
  end
@@ -1,32 +1,32 @@
1
- h1. sweetloader
1
+ # Sweetloader
2
2
 
3
- A Sweet autoloader that can autoload your modules and classes more easily, by taking advantage of a standard directory structure approach for the placement of your classes and modules.
3
+ A 'sweet' autoloader that can autoload your modules and classes more easily, by taking advantage of a standard directory structure approach for the placement of your classes and modules. You can also configure Sweetloader to override the defaults in various ways...
4
4
 
5
- h2. Install
5
+ ## Install
6
6
 
7
- In your Gemfile:
7
+ Insert in your Gemfile:
8
8
 
9
- <pre>
10
- gem 'sweetload'
11
- </pre>
9
+ ```ruby
10
+ gem 'sweetloader'
11
+ ```
12
12
 
13
13
  Run bundler to install
14
14
 
15
15
  $bundle$
16
16
 
17
- h2. Usage
17
+ ## Usage
18
18
 
19
19
  Simply use the autoload_modules macro in your Module and Class definitions.
20
20
 
21
- <pre>
21
+ ```ruby
22
22
  CanTango
23
23
  autoload_module :Configuration, :Factory, :Permit
24
24
  end
25
- </pre>
25
+ ```
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
- h3. Configuration
29
+ ## Configuration
30
30
 
31
31
  The following global config vars are available in AutoLoader:
32
32
  * root
@@ -35,14 +35,46 @@ The following global config vars are available in AutoLoader:
35
35
  Set a specific root which the dir will be calculated relative to, using root.
36
36
  Override namespace to directory auto-mapping using the namespaces hash.
37
37
 
38
- <pre>
38
+ ```ruby
39
39
  AutoLoader.root = ''
40
40
  AutoLoader.namespaces= {:CanTango => 'cantango', :Permithelper => 'permit_helper'}
41
- </pre>
41
+ ```
42
42
 
43
43
  Normally the namespace/constant CanTango will be translated to the dir 'can_tango', here we override this so it will instead be translated to 'cantango'.
44
44
 
45
- h2. Contributing to sweetload
45
+ You can also specify these options directly as an option hash on the `#autoload_modules` call
46
+
47
+ ```ruby
48
+ autoload_modules :Configuration, :Factory, :Permit, :root => 'helpers'
49
+ autoload_modules :Configuration, :Factory, :Permit, :ns => {:CanTango => 'cantango'}
50
+ ```
51
+
52
+ ## Mutate path option
53
+
54
+ Can be used to execute more general substitution logic on the generated file path
55
+
56
+ ```ruby
57
+ autoload_modules :Configuration, :Factory, :Permit, :mutate_path => Proc.new {|path| path.sub(/(ies)/, 'y') }
58
+ ```
59
+
60
+ ## Scopes
61
+
62
+ ```ruby
63
+ module AutoloadModules
64
+ module Configuration
65
+ # the scope options only have effect for autoload_modules statements within it!
66
+ autoload_scope
67
+ :ns => { :AutoloadModules => 'fixtures/autoload_modulez'},
68
+ :mutate_path => Proc.new {|path| path.sub(/(ies)/, 'y') } do
69
+ autoload_modules :Admin
70
+ end
71
+
72
+ autoload_modules :Editor
73
+ end
74
+ end
75
+ ```
76
+
77
+ ## Contributing to sweetloader
46
78
 
47
79
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
48
80
  * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
@@ -52,7 +84,7 @@ h2. Contributing to sweetload
52
84
  * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
53
85
  * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
54
86
 
55
- h2. Copyright
87
+ ## Copyright
56
88
 
57
89
  Copyright (c) 2011 Kristian Mandrup. See LICENSE.txt for
58
90
  further details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
data/lib/sweetloader.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'active_support/core_ext/string/inflections'
2
2
 
3
- class Module
3
+
4
+ module SweetLoader
4
5
  def include_and_extend(the_module, options={})
5
6
  options[:instance_methods] ||= :InstanceMethods
6
7
  options[:class_methods] ||= :ClassMethods
@@ -12,21 +13,54 @@ class Module
12
13
  end
13
14
 
14
15
  def autoload_modules *args
16
+ alm_options = args.extract_options!
17
+ alm_options.merge!(autoload_options) if respond_to? :autoload_options
15
18
 
16
- options = args.extract_options!
17
- root = options[:root] || AutoLoader.root || ''
19
+ root = alm_options[:root] || AutoLoader.root || ''
18
20
  path = root.strip.empty? ? self.name.to_s.underscore : [root, self.name.to_s.underscore].join('/')
19
- from = options[:from] || path
20
-
21
+ from = alm_options[:from] || path
22
+ proc = alm_options[:mutate_path]
23
+ from = proc.call(from) if proc
24
+
25
+ the_module = send(:the_module) if respond_to? :the_module
26
+ the_module ||= self
27
+
21
28
  # Here also could be adding of the file in top of load_paths like: $:.unshift File.dirname(__FILE__)
22
29
  # It is very useful for situations of having not load_paths built Rails or Gems way.
23
30
  args.each do |req_name|
24
31
  ruby_file = req_name.to_s.underscore
25
-
26
- send :autoload, req_name, AutoLoader.translate("#{from}/#{ruby_file}")
32
+ require_file = AutoLoader.translate("#{from}/#{ruby_file}", alm_options)
33
+ the_module.send :autoload, req_name, require_file
27
34
  end
28
35
  end
36
+ alias_method :autoload_module, :autoload_modules
37
+
38
+ def autoload_scope options = {}, &block
39
+ if block_given?
40
+ block.arity == 1 ? yield(self) : SweetLoader::Scope.new(self, options).instance_eval(&block)
41
+ end
42
+ end
43
+ end
44
+
45
+ module SweetLoader
46
+ class Scope
47
+ include SweetLoader
48
+
49
+ attr_reader :autoload_options, :the_module
50
+
51
+ def initialize the_module, options = {}
52
+ @the_module = the_module
53
+ @autoload_options = options
54
+ end
55
+
56
+ def name
57
+ the_module.name
58
+ end
59
+ end
60
+ end
29
61
 
62
+ class Module
63
+ include SweetLoader
30
64
  end
31
65
 
32
66
  module AutoLoader
@@ -49,11 +83,13 @@ module AutoLoader
49
83
  @@namespaces = namespaces
50
84
  end
51
85
 
52
- def self.translate name
86
+ def self.translate name, options = {}
53
87
  names = name.split('/')
88
+ ns = namespaces.merge(options[:namespaces] || options[:ns] || {})
54
89
  names.map do |name|
55
90
  clazz_name = name.to_s.camelize
56
- namespaces[clazz_name.to_sym] ? namespaces[clazz_name.to_sym] : name
91
+ folder = ns[clazz_name.to_sym] ? ns[clazz_name.to_sym] : name
92
+ folder.sub /\/$/, ''
57
93
  end.join('/')
58
94
  end
59
95
  end
@@ -0,0 +1,7 @@
1
+ module AutoloadModules
2
+ module Configuration
3
+ module Editor
4
+ end
5
+ end
6
+ end
7
+
@@ -0,0 +1,4 @@
1
+ module AutoloadModules
2
+ module Abc
3
+ end
4
+ end
@@ -5,3 +5,12 @@ module AutoloadModulez
5
5
  end
6
6
  end
7
7
  end
8
+
9
+ # for mutate_path test
10
+ module AutoloadModules
11
+ module First
12
+ def self.test
13
+ puts "here is the test"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,7 @@
1
+ module AutoloadModules
2
+ module Configuration
3
+ module Admin
4
+ end
5
+ end
6
+ end
7
+
@@ -0,0 +1,4 @@
1
+ module AutoloadModules
2
+ module Xyz
3
+ end
4
+ end
@@ -0,0 +1,20 @@
1
+ module AutoloadModules
2
+ autoload_modules :First, :mutate_path => Proc.new {|path| 'fixtures/autoload_modulez'}
3
+ end
4
+
5
+ module AutoloadModules
6
+ autoload_scope :mutate_path => Proc.new {|path| 'fixtures/autoload_modulez'} do
7
+ autoload_modules :Abc
8
+ autoload_modules :Xyz
9
+ end
10
+ end
11
+
12
+ module AutoloadModules
13
+ module Configuration
14
+ autoload_scope :ns => { :AutoloadModules => 'fixtures/autoload_modulez', :Configuration => 'roles/config/'} do
15
+ autoload_modules :Admin
16
+ end
17
+
18
+ autoload_modules :Editor
19
+ end
20
+ end
@@ -0,0 +1,37 @@
1
+ class Trial
2
+ include ClassExt
3
+ end
4
+
5
+ class Hello
6
+ end
7
+
8
+ module GoodBye
9
+ module Alpha
10
+ module Beta
11
+ end
12
+ end
13
+ end
14
+
15
+ module First
16
+ module ClassMethods
17
+ def class_method
18
+ end
19
+ end
20
+
21
+ module InstanceMethods
22
+ def instance_method
23
+ end
24
+ end
25
+ end
26
+
27
+ module Second
28
+ include_and_extend First
29
+ end
30
+
31
+ class Third
32
+ include_and_extend Second
33
+ end
34
+
35
+ def trial
36
+ @trial ||= Trial.new
37
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'rspec'
2
2
  require 'sweetloader'
3
+ require 'fixtures/models'
3
4
 
4
5
  RSpec.configure do |config|
5
6
 
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+ require 'fixtures/autoload_mutate_path'
3
+
4
+ describe 'Sweetloader options' do
5
+ describe "#autoload_modules" do
6
+
7
+ describe ':mutate_path argument' do
8
+ specify do
9
+ lambda { AutoloadModules::First.should respond_to(:test) }.should_not raise_error
10
+ end
11
+ end
12
+
13
+ describe "#autoload_scope with :mutate_path options" do
14
+ specify do
15
+ lambda { AutoloadModules::Abc }.should_not raise_error
16
+ end
17
+ end
18
+
19
+ describe "#autoload_scope with :namespace option" do
20
+ specify do
21
+ lambda { AutoloadModules::Configuration::Admin }.should_not raise_error
22
+ lambda { AutoloadModules::Configuration::Editor }.should_not raise_error
23
+ end
24
+ end
25
+ end
26
+ end
@@ -1,43 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
- class Trial
4
- include ClassExt
5
- end
6
-
7
- class Hello
8
- end
9
-
10
- module GoodBye
11
- module Alpha
12
- module Beta
13
- end
14
- end
15
- end
16
-
17
- module First
18
- module ClassMethods
19
- def class_method
20
- end
21
- end
22
-
23
- module InstanceMethods
24
- def instance_method
25
- end
26
- end
27
- end
28
-
29
- module Second
30
- include_and_extend First
31
- end
32
-
33
- class Third
34
- include_and_extend Second
35
- end
36
-
37
- def trial
38
- @trial ||= Trial.new
39
- end
40
-
41
3
  describe Module do
42
4
 
43
5
  describe "#include_and_extend" do
data/sweetloader.gemspec CHANGED
@@ -4,24 +4,24 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = "sweetloader"
8
- s.version = "0.1.1"
7
+ s.name = %q{sweetloader}
8
+ s.version = "0.1.2"
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-10-12"
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-28}
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
- "README.textile"
17
+ "README.mdown"
18
18
  ]
19
19
  s.files = [
20
20
  ".document",
21
21
  ".rspec",
22
22
  "Gemfile",
23
23
  "LICENSE.txt",
24
- "README.textile",
24
+ "README.mdown",
25
25
  "Rakefile",
26
26
  "VERSION",
27
27
  "lib/sweetloader.rb",
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
29
29
  "spec/autoload_blank_root.rb",
30
30
  "spec/autoload_blank_root/hello.rb",
31
31
  "spec/autoload_blank_root/sailor.rb",
32
+ "spec/autoload_modules/configuration/editor.rb",
32
33
  "spec/class_ext_spec.rb",
33
34
  "spec/fixtures/autoload_modules.rb",
34
35
  "spec/fixtures/autoload_modules/subdir/first.rb",
@@ -39,18 +40,24 @@ Gem::Specification.new do |s|
39
40
  "spec/fixtures/autoload_modules_root/second.rb",
40
41
  "spec/fixtures/autoload_modules_root/third.rb",
41
42
  "spec/fixtures/autoload_modulez.rb",
43
+ "spec/fixtures/autoload_modulez/abc.rb",
42
44
  "spec/fixtures/autoload_modulez/first.rb",
45
+ "spec/fixtures/autoload_modulez/roles/config/admin.rb",
43
46
  "spec/fixtures/autoload_modulez/second.rb",
44
47
  "spec/fixtures/autoload_modulez/third_one_here.rb",
48
+ "spec/fixtures/autoload_modulez/xyz.rb",
49
+ "spec/fixtures/autoload_mutate_path.rb",
50
+ "spec/fixtures/models.rb",
45
51
  "spec/spec_helper.rb",
52
+ "spec/sweetloader/sweetloader_options_spec.rb",
46
53
  "spec/sweetloader/sweetloader_spec.rb",
47
54
  "sweetloader.gemspec"
48
55
  ]
49
- s.homepage = "http://github.com/kristianmandrup/sweetloader"
50
- s.licenses = ["MIT"]
51
- s.require_paths = ["lib"]
52
- s.rubygems_version = "1.8.10"
53
- s.summary = "sweetens up autoloading using file structure conventions"
56
+ s.homepage = %q{http://github.com/kristianmandrup/sweetloader}
57
+ s.licenses = [%q{MIT}]
58
+ s.require_paths = [%q{lib}]
59
+ s.rubygems_version = %q{1.8.6}
60
+ s.summary = %q{sweetens up autoloading using file structure conventions}
54
61
 
55
62
  if s.respond_to? :specification_version then
56
63
  s.specification_version = 3
@@ -59,14 +66,14 @@ Gem::Specification.new do |s|
59
66
  s.add_runtime_dependency(%q<activesupport>, [">= 3.0.1"])
60
67
  s.add_runtime_dependency(%q<i18n>, [">= 0"])
61
68
  s.add_development_dependency(%q<rspec>, [">= 2.5.0"])
62
- s.add_development_dependency(%q<bundler>, ["~> 1.0.10"])
69
+ s.add_development_dependency(%q<bundler>, [">= 1.1.rc"])
63
70
  s.add_development_dependency(%q<jeweler>, [">= 1.6.4"])
64
71
  s.add_development_dependency(%q<rcov>, [">= 0"])
65
72
  else
66
73
  s.add_dependency(%q<activesupport>, [">= 3.0.1"])
67
74
  s.add_dependency(%q<i18n>, [">= 0"])
68
75
  s.add_dependency(%q<rspec>, [">= 2.5.0"])
69
- s.add_dependency(%q<bundler>, ["~> 1.0.10"])
76
+ s.add_dependency(%q<bundler>, [">= 1.1.rc"])
70
77
  s.add_dependency(%q<jeweler>, [">= 1.6.4"])
71
78
  s.add_dependency(%q<rcov>, [">= 0"])
72
79
  end
@@ -74,7 +81,7 @@ Gem::Specification.new do |s|
74
81
  s.add_dependency(%q<activesupport>, [">= 3.0.1"])
75
82
  s.add_dependency(%q<i18n>, [">= 0"])
76
83
  s.add_dependency(%q<rspec>, [">= 2.5.0"])
77
- s.add_dependency(%q<bundler>, ["~> 1.0.10"])
84
+ s.add_dependency(%q<bundler>, [">= 1.1.rc"])
78
85
  s.add_dependency(%q<jeweler>, [">= 1.6.4"])
79
86
  s.add_dependency(%q<rcov>, [">= 0"])
80
87
  end
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.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-12 00:00:00.000000000Z
12
+ date: 2011-11-28 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &2156227500 !ruby/object:Gem::Requirement
16
+ requirement: &70350317938260 !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: *2156227500
24
+ version_requirements: *70350317938260
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: i18n
27
- requirement: &2156226920 !ruby/object:Gem::Requirement
27
+ requirement: &70350317937740 !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: *2156226920
35
+ version_requirements: *70350317937740
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &2156226320 !ruby/object:Gem::Requirement
38
+ requirement: &70350317937200 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,21 +43,21 @@ dependencies:
43
43
  version: 2.5.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2156226320
46
+ version_requirements: *70350317937200
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bundler
49
- requirement: &2156225840 !ruby/object:Gem::Requirement
49
+ requirement: &70350317936580 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
- - - ~>
52
+ - - ! '>='
53
53
  - !ruby/object:Gem::Version
54
- version: 1.0.10
54
+ version: 1.1.rc
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2156225840
57
+ version_requirements: *70350317936580
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: jeweler
60
- requirement: &2156225240 !ruby/object:Gem::Requirement
60
+ requirement: &70350317933820 !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: *2156225240
68
+ version_requirements: *70350317933820
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rcov
71
- requirement: &2156224100 !ruby/object:Gem::Requirement
71
+ requirement: &70350317933120 !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: *2156224100
79
+ version_requirements: *70350317933120
80
80
  description: sweet autoloading using file structure conventions while allowing configuration
81
81
  overrides for special cases
82
82
  email: kmandrup@gmail.com
@@ -84,13 +84,13 @@ executables: []
84
84
  extensions: []
85
85
  extra_rdoc_files:
86
86
  - LICENSE.txt
87
- - README.textile
87
+ - README.mdown
88
88
  files:
89
89
  - .document
90
90
  - .rspec
91
91
  - Gemfile
92
92
  - LICENSE.txt
93
- - README.textile
93
+ - README.mdown
94
94
  - Rakefile
95
95
  - VERSION
96
96
  - lib/sweetloader.rb
@@ -98,6 +98,7 @@ files:
98
98
  - spec/autoload_blank_root.rb
99
99
  - spec/autoload_blank_root/hello.rb
100
100
  - spec/autoload_blank_root/sailor.rb
101
+ - spec/autoload_modules/configuration/editor.rb
101
102
  - spec/class_ext_spec.rb
102
103
  - spec/fixtures/autoload_modules.rb
103
104
  - spec/fixtures/autoload_modules/subdir/first.rb
@@ -108,10 +109,16 @@ files:
108
109
  - spec/fixtures/autoload_modules_root/second.rb
109
110
  - spec/fixtures/autoload_modules_root/third.rb
110
111
  - spec/fixtures/autoload_modulez.rb
112
+ - spec/fixtures/autoload_modulez/abc.rb
111
113
  - spec/fixtures/autoload_modulez/first.rb
114
+ - spec/fixtures/autoload_modulez/roles/config/admin.rb
112
115
  - spec/fixtures/autoload_modulez/second.rb
113
116
  - spec/fixtures/autoload_modulez/third_one_here.rb
117
+ - spec/fixtures/autoload_modulez/xyz.rb
118
+ - spec/fixtures/autoload_mutate_path.rb
119
+ - spec/fixtures/models.rb
114
120
  - spec/spec_helper.rb
121
+ - spec/sweetloader/sweetloader_options_spec.rb
115
122
  - spec/sweetloader/sweetloader_spec.rb
116
123
  - sweetloader.gemspec
117
124
  homepage: http://github.com/kristianmandrup/sweetloader
@@ -129,7 +136,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
129
136
  version: '0'
130
137
  segments:
131
138
  - 0
132
- hash: 406729261451437961
139
+ hash: 648157612907280241
133
140
  required_rubygems_version: !ruby/object:Gem::Requirement
134
141
  none: false
135
142
  requirements:
@@ -138,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
145
  version: '0'
139
146
  requirements: []
140
147
  rubyforge_project:
141
- rubygems_version: 1.8.10
148
+ rubygems_version: 1.8.6
142
149
  signing_key:
143
150
  specification_version: 3
144
151
  summary: sweetens up autoloading using file structure conventions