impromptu 1.1.0 → 1.2.0

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.
data/CHANGELOG CHANGED
@@ -1,3 +1,12 @@
1
+ 1.2.0 - New method 'root?' which tests is symbols are prefixed with two colons
2
+ - New 'namespace' option for folders allows you to override a components
3
+ namespace with a namespace specific to a folder.
4
+ - Root symbols (::A) are now respected in provides options on files.
5
+ This allows files to implement resources which are not relative to the
6
+ component or folder namespace. For instance a folder with a namespace
7
+ :A containing a file which provides :B and ::C would create resources
8
+ for A::B and C.
9
+
1
10
  1.1.0 - Added support for resources which extend stdlib classes and modules
2
11
  - Added the ability to define exceptional files in a folder. Previously
3
12
  folders were either implicitly loaded (all files were loaded), or you
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0
1
+ 1.2.0
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.1.0"
8
+ s.version = "1.2.0"
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"]
@@ -40,6 +40,7 @@ Gem::Specification.new do |s|
40
40
  "test/framework/copies/original_klass.rb",
41
41
  "test/framework/ext/extensions.rb",
42
42
  "test/framework/ext/extensions/blog.rb",
43
+ "test/framework/folder_namespace/stream.rb",
43
44
  "test/framework/lib/group/klass2.rb",
44
45
  "test/framework/lib/klass.rb",
45
46
  "test/framework/other/also.rb",
@@ -75,6 +76,7 @@ Gem::Specification.new do |s|
75
76
  "test/framework/copies/original_klass.rb",
76
77
  "test/framework/ext/extensions.rb",
77
78
  "test/framework/ext/extensions/blog.rb",
79
+ "test/framework/folder_namespace/stream.rb",
78
80
  "test/framework/lib/group/klass2.rb",
79
81
  "test/framework/lib/klass.rb",
80
82
  "test/framework/other/also.rb",
@@ -96,18 +96,19 @@ module Impromptu
96
96
  # Mark a component as 'frozen'. Modification of the component
97
97
  # requirements or list of folders is not allowed after this.
98
98
  def freeze
99
+ # create a blank namespace module if required
100
+ unless namespace.nil?
101
+ Impromptu.root_resource.get_or_create_child(namespace).namespace!
102
+ end
103
+
99
104
  # freeze files
100
105
  @folders.each do |folder|
106
+ folder.create_namespace
101
107
  folder.files.each do |file|
102
108
  file.freeze
103
109
  end
104
110
  end
105
111
 
106
- # create a blank namespace module if required
107
- unless namespace.nil?
108
- Impromptu.root_resource.get_or_create_child(namespace).namespace!
109
- end
110
-
111
112
  @frozen = true
112
113
  end
113
114
 
@@ -210,11 +210,14 @@ module Impromptu
210
210
  end
211
211
 
212
212
  def combine_symbol_with_namespace(symbol)
213
- if @folder.component.namespace
214
- "#{@folder.component.namespace}::#{symbol}".to_sym
215
- else
216
- symbol.to_sym
213
+ unless symbol.root?
214
+ if @folder.namespace
215
+ return "#{@folder.namespace}::#{symbol}".to_sym
216
+ elsif @folder.component.namespace
217
+ return "#{@folder.component.namespace}::#{symbol}".to_sym
218
+ end
217
219
  end
220
+ symbol.without_leading_colons.to_sym
218
221
  end
219
222
  end
220
223
  end
@@ -1,7 +1,7 @@
1
1
  module Impromptu
2
2
  class Folder
3
- attr_accessor :folder, :files, :component, :namespace
4
- DEFAULT_OPTIONS = {nested_namespaces: true, reloadable: true, implicitly_loaded: true}
3
+ attr_accessor :folder, :files, :component
4
+ DEFAULT_OPTIONS = {nested_namespaces: true, reloadable: true, implicitly_loaded: true, namespace: nil}
5
5
  SOURCE_EXTENSIONS = %w{rb so bundle}
6
6
 
7
7
  # Register a new folder containing source files for a
@@ -20,6 +20,9 @@ module Impromptu
20
20
  # * implicitly_loaded: true by default. When true, reloads
21
21
  # and the initial load of this folder will scan for source
22
22
  # files and automatically infer resource definitions.
23
+ # * namespace: override a component's default namespace with
24
+ # a namespace specific to this folder. The normal rules
25
+ # with nested namespaces apply.
23
26
  def initialize(path, component, options={}, block)
24
27
  @folder = path.realpath
25
28
  @component = component
@@ -69,6 +72,19 @@ module Impromptu
69
72
  @options[:implicitly_loaded]
70
73
  end
71
74
 
75
+ # A string or symbol for this folder which overrides the components
76
+ # default namespace. Nil if no namespace has been defined.
77
+ def namespace
78
+ @options[:namespace]
79
+ end
80
+
81
+ # If an overriding namespace has been defined for this folder,
82
+ # create a resource representing it and define it as a namespace.
83
+ def create_namespace
84
+ return if self.namespace.nil?
85
+ Impromptu.root_resource.get_or_create_child(self.namespace).namespace!
86
+ end
87
+
72
88
  # Return the 'base' folder for a file contained within this
73
89
  # folder. For instance, a folder with nested namespaces would
74
90
  # return the path to a file from the root folder. Without
@@ -30,6 +30,11 @@ class Symbol
30
30
  self.nested_symbols.first
31
31
  end
32
32
 
33
+ # True if the symbol starts with two colons
34
+ def root?
35
+ self.to_s.start_with?('::')
36
+ end
37
+
33
38
  # Iterate through a namespaced symbol by visiting each
34
39
  # name in turn, including its parent names. e.g calling
35
40
  # on A::B::C would yield :A, :A::B, and :A::B::C
@@ -0,0 +1,4 @@
1
+ module Namespace
2
+ class Stream
3
+ end
4
+ end
@@ -1,4 +1,2 @@
1
- module Framework
2
- module Another
3
- end
1
+ module Another
4
2
  end
@@ -20,6 +20,10 @@ end
20
20
  component 'private' do
21
21
  namespace :Framework
22
22
  folder 'private' do
23
- file 'other.rb', provides: :Another
23
+ file 'other.rb', provides: :'::Another'
24
24
  end
25
25
  end
26
+
27
+ component 'folder_namespace' do
28
+ folder 'folder_namespace', namespace: :Namespace
29
+ end
@@ -13,8 +13,8 @@ class TestIntegration < Test::Unit::TestCase
13
13
  # ----------------------------------------
14
14
  # Loading component definitions
15
15
  # ----------------------------------------
16
- should "01 create four components" do
17
- assert_equal 4, Impromptu.components.size
16
+ should "01 create five components" do
17
+ assert_equal 5, Impromptu.components.size
18
18
  end
19
19
 
20
20
  should "02 have a single folder per component" do
@@ -22,6 +22,7 @@ class TestIntegration < Test::Unit::TestCase
22
22
  assert_equal 1, Impromptu.components['framework.extensions'].folders.size
23
23
  assert_equal 1, Impromptu.components['other'].folders.size
24
24
  assert_equal 1, Impromptu.components['private'].folders.size
25
+ assert_equal 1, Impromptu.components['folder_namespace'].folders.size
25
26
  end
26
27
 
27
28
  should "03 have a single require in the framework component" do
@@ -34,14 +35,15 @@ class TestIntegration < Test::Unit::TestCase
34
35
  assert_equal nil, Impromptu.components['other'].namespace
35
36
  end
36
37
 
37
- should "05 start tracking 8 files" do
38
+ should "05 start tracking 10 files" do
38
39
  assert_equal 2, Impromptu.components['framework'].folders.first.files.size
39
40
  assert_equal 2, Impromptu.components['framework.extensions'].folders.first.files.size
40
41
  assert_equal 3, Impromptu.components['other'].folders.first.files.size
41
42
  assert_equal 2, Impromptu.components['private'].folders.first.files.size
43
+ assert_equal 1, Impromptu.components['folder_namespace'].folders.first.files.size
42
44
  end
43
45
 
44
- should "06 load definitions for 9 resources" do
46
+ should "06 load definitions for 12 resources" do
45
47
  assert Impromptu.root_resource.child?(:Framework)
46
48
  assert Impromptu.root_resource.child(:Framework).child?(:Extensions)
47
49
  assert Impromptu.root_resource.child(:Framework).child(:Extensions).child?(:Blog)
@@ -51,7 +53,9 @@ class TestIntegration < Test::Unit::TestCase
51
53
  assert Impromptu.root_resource.child?(:OtherName)
52
54
  assert Impromptu.root_resource.child?(:ModOne)
53
55
  assert Impromptu.root_resource.child?(:ModTwo)
54
- assert Impromptu.root_resource.child(:Framework).child?(:Another)
56
+ assert Impromptu.root_resource.child?(:Another)
57
+ assert Impromptu.root_resource.child(:Namespace)
58
+ assert Impromptu.root_resource.child(:Namespace).child?(:Stream)
55
59
  end
56
60
 
57
61
  should "07 correctly mark namespace resources" do
@@ -64,7 +68,9 @@ class TestIntegration < Test::Unit::TestCase
64
68
  assert_equal false, Impromptu.root_resource.child(:OtherName).namespace?
65
69
  assert_equal false, Impromptu.root_resource.child(:ModOne).namespace?
66
70
  assert_equal false, Impromptu.root_resource.child(:ModTwo).namespace?
67
- assert_equal false, Impromptu.root_resource.child(:Framework).child(:Another).namespace?
71
+ assert_equal false, Impromptu.root_resource.child(:Another).namespace?
72
+ assert_equal true, Impromptu.root_resource.child(:Namespace).namespace?
73
+ assert_equal false, Impromptu.root_resource.child(:Namespace).child(:Stream).namespace?
68
74
  end
69
75
 
70
76
  should "08 keep all resources unloaded to start with" do
@@ -77,7 +83,9 @@ class TestIntegration < Test::Unit::TestCase
77
83
  assert_equal false, Impromptu.root_resource.child(:'OtherName').loaded?
78
84
  assert_equal false, Impromptu.root_resource.child(:'ModOne').loaded?
79
85
  assert_equal false, Impromptu.root_resource.child(:'ModTwo').loaded?
80
- assert_equal false, Impromptu.root_resource.child(:Framework).child(:'Another').loaded?
86
+ assert_equal false, Impromptu.root_resource.child(:'Another').loaded?
87
+ assert_equal false, Impromptu.root_resource.child(:'Namespace').loaded?
88
+ assert_equal false, Impromptu.root_resource.child(:'Namespace::Stream').loaded?
81
89
  end
82
90
 
83
91
  should "09 have all resources specified by the correct number of files" do
@@ -91,21 +99,30 @@ class TestIntegration < Test::Unit::TestCase
91
99
  assert_equal 2, Impromptu.root_resource.child(:'OtherName').files.size
92
100
  assert_equal 1, Impromptu.root_resource.child(:'ModOne').files.size
93
101
  assert_equal 1, Impromptu.root_resource.child(:'ModTwo').files.size
94
- assert_equal 1, Impromptu.root_resource.child(:Framework).child(:'Another').files.size
102
+ assert_equal 1, Impromptu.root_resource.child(:'Another').files.size
103
+ assert_equal 1, Impromptu.root_resource.child(:'Namespace::Stream').files.size
95
104
  assert_equal true, Impromptu.root_resource.child(:'Framework').implicitly_defined?
105
+ assert_equal true, Impromptu.root_resource.child(:'Namespace').implicitly_defined?
96
106
  end
97
107
 
98
108
 
99
109
  # ----------------------------------------
100
110
  # Loading/unloading
101
111
  # ----------------------------------------
102
- should "10 allow loading the implicitly defined framework module" do
112
+ should "10 allow loading the implicitly namespace modules" do
103
113
  assert_equal false, Impromptu.root_resource.child(:'Framework').loaded?
104
114
  Impromptu.root_resource.child(:'Framework').reload
105
115
  assert_equal true, Impromptu.root_resource.child(:'Framework').loaded?
106
116
  assert_nothing_raised do
107
117
  Framework
108
118
  end
119
+
120
+ assert_equal false, Impromptu.root_resource.child(:'Namespace').loaded?
121
+ Impromptu.root_resource.child(:'Namespace').reload
122
+ assert_equal true, Impromptu.root_resource.child(:'Namespace').loaded?
123
+ assert_nothing_raised do
124
+ Namespace
125
+ end
109
126
  end
110
127
 
111
128
  should "11 load resources using associated files when required" do
@@ -155,10 +172,17 @@ class TestIntegration < Test::Unit::TestCase
155
172
  end
156
173
 
157
174
  # private
158
- Impromptu.root_resource.child(:Framework).child(:Another).reload
159
- assert_equal true, Impromptu.root_resource.child(:Framework).child(:Another).loaded?
175
+ Impromptu.root_resource.child(:Another).reload
176
+ assert_equal true, Impromptu.root_resource.child(:Another).loaded?
177
+ assert_nothing_raised do
178
+ Another
179
+ end
180
+
181
+ # folder namespace
182
+ Impromptu.root_resource.child(:'Namespace::Stream').reload
183
+ assert_equal true, Impromptu.root_resource.child(:'Namespace::Stream').loaded?
160
184
  assert_nothing_raised do
161
- Framework::Another
185
+ Namespace::Stream
162
186
  end
163
187
  end
164
188
 
data/test/test_symbol.rb CHANGED
@@ -27,6 +27,10 @@ class TestResource < Test::Unit::TestCase
27
27
  assert_respond_to @symbol, :root_symbol
28
28
  end
29
29
 
30
+ should "respond to root?" do
31
+ assert_respond_to @symbol, :root?
32
+ end
33
+
30
34
  should "respond to each_namespaced_symbol" do
31
35
  assert_respond_to @symbol, :each_namespaced_symbol
32
36
  end
@@ -56,6 +60,10 @@ class TestResource < Test::Unit::TestCase
56
60
  should "return itself for root_symbol" do
57
61
  assert_equal @symbol, @symbol.root_symbol
58
62
  end
63
+
64
+ should "return false for root?" do
65
+ assert_equal false, @symbol.root?
66
+ end
59
67
  end
60
68
 
61
69
  # ----------------------------------------
@@ -82,6 +90,10 @@ class TestResource < Test::Unit::TestCase
82
90
  assert_equal :TopLevel, @symbol.root_symbol
83
91
  end
84
92
 
93
+ should "return false for root?" do
94
+ assert_equal false, @symbol.root?
95
+ end
96
+
85
97
  should "return the last nested name for base_symbol" do
86
98
  assert_equal :BottomLevel, @symbol.base_symbol
87
99
  end
@@ -119,6 +131,10 @@ class TestResource < Test::Unit::TestCase
119
131
  assert_equal :A, @symbol.base_symbol
120
132
  end
121
133
 
134
+ should "return true for root?" do
135
+ assert_equal true, @symbol.root?
136
+ end
137
+
122
138
  should "return itself for root_symbol" do
123
139
  assert_equal :A, @symbol.root_symbol
124
140
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 1
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 1.1.0
9
+ version: 1.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Will Cannings
@@ -63,6 +63,7 @@ files:
63
63
  - test/framework/copies/original_klass.rb
64
64
  - test/framework/ext/extensions.rb
65
65
  - test/framework/ext/extensions/blog.rb
66
+ - test/framework/folder_namespace/stream.rb
66
67
  - test/framework/lib/group/klass2.rb
67
68
  - test/framework/lib/klass.rb
68
69
  - test/framework/other/also.rb
@@ -125,6 +126,7 @@ test_files:
125
126
  - test/framework/copies/original_klass.rb
126
127
  - test/framework/ext/extensions.rb
127
128
  - test/framework/ext/extensions/blog.rb
129
+ - test/framework/folder_namespace/stream.rb
128
130
  - test/framework/lib/group/klass2.rb
129
131
  - test/framework/lib/klass.rb
130
132
  - test/framework/other/also.rb