impromptu 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ 1.2.1 - Fix bug where namespaces were not removed after camelcasing a file name
2
+ - Simplify autoloading behaviour and fix a bug which affected autoloading
3
+ constants while in the middle of an existing autoload.
4
+
1
5
  1.2.0 - New method 'root?' which tests is symbols are prefixed with two colons
2
6
  - New 'namespace' option for folders allows you to override a components
3
7
  namespace with a namespace specific to a folder.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.0
1
+ 1.2.1
data/impromptu.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{impromptu}
8
- s.version = "1.2.0"
8
+ s.version = "1.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Will Cannings"]
@@ -41,6 +41,7 @@ Gem::Specification.new do |s|
41
41
  "test/framework/ext/extensions.rb",
42
42
  "test/framework/ext/extensions/blog.rb",
43
43
  "test/framework/folder_namespace/stream.rb",
44
+ "test/framework/folder_namespace/two_names.rb",
44
45
  "test/framework/lib/group/klass2.rb",
45
46
  "test/framework/lib/klass.rb",
46
47
  "test/framework/other/also.rb",
@@ -77,6 +78,7 @@ Gem::Specification.new do |s|
77
78
  "test/framework/ext/extensions.rb",
78
79
  "test/framework/ext/extensions/blog.rb",
79
80
  "test/framework/folder_namespace/stream.rb",
81
+ "test/framework/folder_namespace/two_names.rb",
80
82
  "test/framework/lib/group/klass2.rb",
81
83
  "test/framework/lib/klass.rb",
82
84
  "test/framework/other/also.rb",
@@ -1,41 +1,33 @@
1
- module Impromptu
2
- module Autoload
3
- # Impromptu implements the autoloading behaviour using this module
4
- # and this method. Object is extended with this module so we can
5
- # catch references to objects which don't exist. It first tries
6
- # to determine if we know about the resource corresponding to name
7
- # and if so loads and returns a reference to it. Otherwise, the usual
8
- # NameError exception will be raised by Object. We also test to make
9
- # sure a resource isn't already loaded before returning a reference
10
- # to it. If it is, then something very screwy has gone on and Ruby
11
- # cannot locate an already loaded resource.
12
- def const_missing(symbol)
13
- # namespace the missing resource with the name of the
14
- # current class or module
15
- if self == Object
16
- namespaced_symbol = symbol
17
- else
18
- namespaced_symbol = "#{self.name}::#{symbol}".to_sym
19
- end
20
-
21
- # walk the resource tree and get a reference to the
22
- # resource or nil if we're not tracking it
23
- resource = Impromptu.root_resource.child(namespaced_symbol)
24
-
25
- # if we don't know about the symbol, send the method to
26
- # Object which will raise an exception
27
- super(symbol) if resource.nil?
28
-
29
- # ensure the resource hasn't already been loaded
30
- raise "Illegal condition: const_missing called after a resource has been loaded" if resource.loaded?
31
-
32
- # load the resource and return a reference to it. this
33
- # assumes that the source files will correctly define
34
- # the resource. otherwise nil will be returned.
35
- resource.reload
36
- resource.reference
37
- end
1
+ # Impromptu implements the autoloading behaviour using const_missing.
2
+ # Module's const_missing definition is replaced with the one below to
3
+ # catch references to objects which don't exist. It first tries
4
+ # to determine if we know about the resource corresponding to name
5
+ # and if so loads and returns a reference to it. Otherwise, the usual
6
+ # NameError exception will be raised. We also test to make sure a
7
+ # resource isn't already loaded before returning a reference to it. If
8
+ # it is, then something very screwy has gone on and Ruby cannot locate
9
+ # an already loaded resource.
10
+ Module.send(:remove_method, :const_missing)
11
+ Module.send(:define_method, :const_missing) do |symbol|
12
+ # namespace the missing resource with the name of the
13
+ # current class or module
14
+ if self == Object
15
+ namespaced_symbol = symbol
16
+ else
17
+ namespaced_symbol = "#{self.name}::#{symbol}".to_sym
38
18
  end
19
+
20
+ # walk the resource tree and get a reference to the
21
+ # resource or nil if we're not tracking it
22
+ resource = Impromptu.root_resource.child(namespaced_symbol)
23
+ raise NameError if resource.nil?
24
+
25
+ # ensure the resource hasn't already been loaded
26
+ raise "Illegal condition: const_missing called after a resource has been loaded" if resource.loaded?
27
+
28
+ # load the resource and return a reference to it. this
29
+ # assumes that the source files will correctly define
30
+ # the resource. otherwise nil will be returned.
31
+ resource.reload
32
+ resource.reference
39
33
  end
40
-
41
- Object.extend(Impromptu::Autoload)
@@ -206,7 +206,10 @@ module Impromptu
206
206
  name.gsub!(/\/(.?)/) {|character| "::#{character[1].upcase}" }
207
207
 
208
208
  # upcase the first character, and any characters following an underscore
209
- name.gsub(/(?:^|_)(.)/) {|character| character.upcase}.to_sym
209
+ name.gsub!(/(?:^|_)(.)/) {|character| character.upcase}
210
+
211
+ # remove underscores
212
+ name.gsub('_', '').to_sym
210
213
  end
211
214
 
212
215
  def combine_symbol_with_namespace(symbol)
@@ -53,7 +53,6 @@ module Impromptu
53
53
  else
54
54
  @files.first.reload
55
55
  end
56
- self.reference.extend(Impromptu::Autoload)
57
56
  end
58
57
 
59
58
  # Unload the resource by undefining the constant representing it.
@@ -0,0 +1,4 @@
1
+ module Namespace
2
+ module TwoNames
3
+ end
4
+ end
@@ -35,15 +35,15 @@ class TestIntegration < Test::Unit::TestCase
35
35
  assert_equal nil, Impromptu.components['other'].namespace
36
36
  end
37
37
 
38
- should "05 start tracking 10 files" do
38
+ should "05 start tracking 11 files" do
39
39
  assert_equal 2, Impromptu.components['framework'].folders.first.files.size
40
40
  assert_equal 2, Impromptu.components['framework.extensions'].folders.first.files.size
41
41
  assert_equal 3, Impromptu.components['other'].folders.first.files.size
42
42
  assert_equal 2, Impromptu.components['private'].folders.first.files.size
43
- assert_equal 1, Impromptu.components['folder_namespace'].folders.first.files.size
43
+ assert_equal 2, Impromptu.components['folder_namespace'].folders.first.files.size
44
44
  end
45
45
 
46
- should "06 load definitions for 12 resources" do
46
+ should "06 load definitions for 13 resources" do
47
47
  assert Impromptu.root_resource.child?(:Framework)
48
48
  assert Impromptu.root_resource.child(:Framework).child?(:Extensions)
49
49
  assert Impromptu.root_resource.child(:Framework).child(:Extensions).child?(:Blog)
@@ -56,6 +56,7 @@ class TestIntegration < Test::Unit::TestCase
56
56
  assert Impromptu.root_resource.child?(:Another)
57
57
  assert Impromptu.root_resource.child(:Namespace)
58
58
  assert Impromptu.root_resource.child(:Namespace).child?(:Stream)
59
+ assert Impromptu.root_resource.child(:Namespace).child?(:TwoNames)
59
60
  end
60
61
 
61
62
  should "07 correctly mark namespace resources" do
@@ -71,6 +72,7 @@ class TestIntegration < Test::Unit::TestCase
71
72
  assert_equal false, Impromptu.root_resource.child(:Another).namespace?
72
73
  assert_equal true, Impromptu.root_resource.child(:Namespace).namespace?
73
74
  assert_equal false, Impromptu.root_resource.child(:Namespace).child(:Stream).namespace?
75
+ assert_equal false, Impromptu.root_resource.child(:Namespace).child(:TwoNames).namespace?
74
76
  end
75
77
 
76
78
  should "08 keep all resources unloaded to start with" do
@@ -86,6 +88,7 @@ class TestIntegration < Test::Unit::TestCase
86
88
  assert_equal false, Impromptu.root_resource.child(:'Another').loaded?
87
89
  assert_equal false, Impromptu.root_resource.child(:'Namespace').loaded?
88
90
  assert_equal false, Impromptu.root_resource.child(:'Namespace::Stream').loaded?
91
+ assert_equal false, Impromptu.root_resource.child(:'Namespace::TwoNames').loaded?
89
92
  end
90
93
 
91
94
  should "09 have all resources specified by the correct number of files" do
@@ -101,6 +104,7 @@ class TestIntegration < Test::Unit::TestCase
101
104
  assert_equal 1, Impromptu.root_resource.child(:'ModTwo').files.size
102
105
  assert_equal 1, Impromptu.root_resource.child(:'Another').files.size
103
106
  assert_equal 1, Impromptu.root_resource.child(:'Namespace::Stream').files.size
107
+ assert_equal 1, Impromptu.root_resource.child(:'Namespace::TwoNames').files.size
104
108
  assert_equal true, Impromptu.root_resource.child(:'Framework').implicitly_defined?
105
109
  assert_equal true, Impromptu.root_resource.child(:'Namespace').implicitly_defined?
106
110
  end
@@ -180,10 +184,15 @@ class TestIntegration < Test::Unit::TestCase
180
184
 
181
185
  # folder namespace
182
186
  Impromptu.root_resource.child(:'Namespace::Stream').reload
187
+ Impromptu.root_resource.child(:'Namespace::TwoNames').reload
183
188
  assert_equal true, Impromptu.root_resource.child(:'Namespace::Stream').loaded?
189
+ assert_equal true, Impromptu.root_resource.child(:'Namespace::TwoNames').loaded?
184
190
  assert_nothing_raised do
185
191
  Namespace::Stream
186
192
  end
193
+ assert_nothing_raised do
194
+ Namespace::TwoNames
195
+ end
187
196
  end
188
197
 
189
198
  should "12 load multiple files for a resource when required" do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 2
8
- - 0
9
- version: 1.2.0
8
+ - 1
9
+ version: 1.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Will Cannings
@@ -64,6 +64,7 @@ files:
64
64
  - test/framework/ext/extensions.rb
65
65
  - test/framework/ext/extensions/blog.rb
66
66
  - test/framework/folder_namespace/stream.rb
67
+ - test/framework/folder_namespace/two_names.rb
67
68
  - test/framework/lib/group/klass2.rb
68
69
  - test/framework/lib/klass.rb
69
70
  - test/framework/other/also.rb
@@ -127,6 +128,7 @@ test_files:
127
128
  - test/framework/ext/extensions.rb
128
129
  - test/framework/ext/extensions/blog.rb
129
130
  - test/framework/folder_namespace/stream.rb
131
+ - test/framework/folder_namespace/two_names.rb
130
132
  - test/framework/lib/group/klass2.rb
131
133
  - test/framework/lib/klass.rb
132
134
  - test/framework/other/also.rb