impromptu 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.1
1
+ 1.3.0
data/impromptu.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{impromptu}
8
- s.version = "1.2.1"
8
+ s.version = "1.3.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"]
12
- s.date = %q{2010-11-16}
12
+ s.date = %q{2011-01-21}
13
13
  s.description = %q{Component and dependency manager for Ruby}
14
14
  s.email = %q{me@willcannings.com}
15
15
  s.extra_rdoc_files = [
@@ -48,6 +48,7 @@ Gem::Specification.new do |s|
48
48
  "test/framework/other/ignore.rb",
49
49
  "test/framework/other/load.rb",
50
50
  "test/framework/other/two.rb",
51
+ "test/framework/preload/preload.rb",
51
52
  "test/framework/private/klass.rb",
52
53
  "test/framework/private/other.rb",
53
54
  "test/framework/stdlib/string.rb",
@@ -85,6 +86,7 @@ Gem::Specification.new do |s|
85
86
  "test/framework/other/ignore.rb",
86
87
  "test/framework/other/load.rb",
87
88
  "test/framework/other/two.rb",
89
+ "test/framework/preload/preload.rb",
88
90
  "test/framework/private/klass.rb",
89
91
  "test/framework/private/other.rb",
90
92
  "test/framework/stdlib/string.rb",
@@ -1,7 +1,7 @@
1
1
  module Impromptu
2
2
  class Folder
3
3
  attr_accessor :folder, :files, :component
4
- DEFAULT_OPTIONS = {nested_namespaces: true, reloadable: true, implicitly_loaded: true, namespace: nil}
4
+ DEFAULT_OPTIONS = {nested_namespaces: true, reloadable: true, implicitly_loaded: true, namespace: nil, preload: false}
5
5
  SOURCE_EXTENSIONS = %w{rb so bundle}
6
6
 
7
7
  # Register a new folder containing source files for a
@@ -23,6 +23,9 @@ module Impromptu
23
23
  # * namespace: override a component's default namespace with
24
24
  # a namespace specific to this folder. The normal rules
25
25
  # with nested namespaces apply.
26
+ # * preload: forces the loading of all files within a folder.
27
+ # The resources defined can still be reloaded, but loading
28
+ # won't wait until the resource is referenced.
26
29
  def initialize(path, component, options={}, block)
27
30
  @folder = path.realpath
28
31
  @component = component
@@ -72,6 +75,21 @@ module Impromptu
72
75
  @options[:implicitly_loaded]
73
76
  end
74
77
 
78
+ # True if the resources of the folder are loaded immediately on
79
+ # startup, and won't be autoloaded.
80
+ def preload?
81
+ @options[:preload]
82
+ end
83
+
84
+ # Preload the resources defined by this folder. Should only be called
85
+ # by the Impromptu module, and only once (at app startup).
86
+ def preload
87
+ return unless preload?
88
+ @files.each do |file|
89
+ file.reload
90
+ end
91
+ end
92
+
75
93
  # A string or symbol for this folder which overrides the components
76
94
  # default namespace. Nil if no namespace has been defined.
77
95
  def namespace
@@ -160,6 +178,16 @@ module Impromptu
160
178
  file.refreeze
161
179
  end
162
180
  end
181
+
182
+ # ensure all resources defined by this folder
183
+ # are marked to be preloaded if required
184
+ if preload?
185
+ @files.each do |file|
186
+ file.resources.each do |resource|
187
+ resource.preload = true
188
+ end
189
+ end
190
+ end
163
191
  end
164
192
 
165
193
 
@@ -60,6 +60,13 @@ module Impromptu
60
60
  # modules or classes. we can't catch uses of these resources
61
61
  # using const_missing, so we need to load them now.
62
62
  @root_resource.load_if_extending_stdlib
63
+
64
+ # load any folders which are to be preloaded
65
+ components.each do |component|
66
+ component.folders.each do |folder|
67
+ folder.preload if folder.preload?
68
+ end
69
+ end
63
70
  end
64
71
 
65
72
  # Open and run a file defining components. The folder containing
@@ -4,7 +4,7 @@ module Impromptu
4
4
  # resources yourself - use component definitions to define folders
5
5
  # of files which will implement resources.
6
6
  class Resource
7
- attr_reader :name, :base_symbol, :files, :children, :parent
7
+ attr_reader :name, :base_symbol, :files, :children, :parent, :preload
8
8
 
9
9
  def initialize(name, parent)
10
10
  @name = name.to_sym
@@ -15,6 +15,7 @@ module Impromptu
15
15
  @reference = nil
16
16
  @namespace = false
17
17
  @dont_undef = self.loaded? # existing constants, such as 'String', should never be unloaded
18
+ @preload = false # true if any folder defining this resource is marked to be preloaded
18
19
  @implicitly_defined = true
19
20
  end
20
21
 
@@ -32,6 +33,16 @@ module Impromptu
32
33
  @name.hash
33
34
  end
34
35
 
36
+ # Define this resource as preloaded
37
+ def preload=(preload)
38
+ @preload = preload
39
+ end
40
+
41
+ # True if this resource is defined as preloaded
42
+ def preload?
43
+ @preload
44
+ end
45
+
35
46
  # Attempts to retrieve a reference to the object represented by
36
47
  # this resource. If the resource is unloaded, nil is returned.
37
48
  def reference
@@ -48,11 +59,12 @@ module Impromptu
48
59
  def reload
49
60
  @parent.reload unless @parent.loaded?
50
61
  if @implicitly_defined
51
- self.unload
62
+ unload
52
63
  Object.module_eval "module #{@name}; end"
53
64
  else
54
65
  @files.first.reload
55
66
  end
67
+ reload_preloaded_resources
56
68
  end
57
69
 
58
70
  # Unload the resource by undefining the constant representing it.
@@ -203,5 +215,10 @@ module Impromptu
203
215
  reload if loaded? && !self.root?
204
216
  @children.each_value(&:load_if_extending_stdlib)
205
217
  end
218
+
219
+ def reload_preloaded_resources
220
+ reload if !loaded? && preload?
221
+ @children.each_value(&:reload_preloaded_resources)
222
+ end
206
223
  end
207
224
  end
@@ -0,0 +1,7 @@
1
+ module Framework
2
+ module Preload
3
+ def self.method
4
+ true
5
+ end
6
+ end
7
+ end
@@ -2,6 +2,7 @@ component 'framework' do
2
2
  namespace :Framework
3
3
  requires 'mathn'
4
4
  folder 'lib', nested_namespaces: false
5
+ folder 'preload', preload: true
5
6
  end
6
7
 
7
8
  component 'framework.extensions' do
@@ -17,8 +17,8 @@ class TestIntegration < Test::Unit::TestCase
17
17
  assert_equal 5, Impromptu.components.size
18
18
  end
19
19
 
20
- should "02 have a single folder per component" do
21
- assert_equal 1, Impromptu.components['framework'].folders.size
20
+ should "02 have the correct number of folders per component" do
21
+ assert_equal 2, Impromptu.components['framework'].folders.size
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
@@ -49,6 +49,7 @@ class TestIntegration < Test::Unit::TestCase
49
49
  assert Impromptu.root_resource.child(:Framework).child(:Extensions).child?(:Blog)
50
50
  assert Impromptu.root_resource.child(:Framework).child?(:Klass)
51
51
  assert Impromptu.root_resource.child(:Framework).child?(:Klass2)
52
+ assert Impromptu.root_resource.child(:Framework).child?(:Preload)
52
53
  assert Impromptu.root_resource.child?(:Load)
53
54
  assert Impromptu.root_resource.child?(:OtherName)
54
55
  assert Impromptu.root_resource.child?(:ModOne)
@@ -70,13 +71,13 @@ class TestIntegration < Test::Unit::TestCase
70
71
  assert_equal false, Impromptu.root_resource.child(:ModOne).namespace?
71
72
  assert_equal false, Impromptu.root_resource.child(:ModTwo).namespace?
72
73
  assert_equal false, Impromptu.root_resource.child(:Another).namespace?
73
- assert_equal true, Impromptu.root_resource.child(:Namespace).namespace?
74
+ assert_equal true, Impromptu.root_resource.child(:Namespace).namespace?
74
75
  assert_equal false, Impromptu.root_resource.child(:Namespace).child(:Stream).namespace?
75
76
  assert_equal false, Impromptu.root_resource.child(:Namespace).child(:TwoNames).namespace?
76
77
  end
77
78
 
78
- should "08 keep all resources unloaded to start with" do
79
- assert_equal false, Impromptu.root_resource.child(:'Framework').loaded?
79
+ should "08 keep all non-preloaded resources unloaded to start with, and preload otherwise" do
80
+ # normal, non preloaded resources
80
81
  assert_equal false, Impromptu.root_resource.child(:'Framework::Extensions').loaded?
81
82
  assert_equal false, Impromptu.root_resource.child(:'Framework::Extensions::Blog').loaded?
82
83
  assert_equal false, Impromptu.root_resource.child(:'Framework::Klass').loaded?
@@ -89,14 +90,19 @@ class TestIntegration < Test::Unit::TestCase
89
90
  assert_equal false, Impromptu.root_resource.child(:'Namespace').loaded?
90
91
  assert_equal false, Impromptu.root_resource.child(:'Namespace::Stream').loaded?
91
92
  assert_equal false, Impromptu.root_resource.child(:'Namespace::TwoNames').loaded?
93
+
94
+ # preloaded resources are the opposite of this
95
+ assert Impromptu.root_resource.child(:'Framework').loaded?
96
+ assert Impromptu.root_resource.child(:'Framework::Preload').loaded?
92
97
  end
93
-
98
+
94
99
  should "09 have all resources specified by the correct number of files" do
95
100
  assert_equal 0, Impromptu.root_resource.child(:'Framework').files.size
96
101
  assert_equal 1, Impromptu.root_resource.child(:'Framework::Extensions').files.size
97
102
  assert_equal 1, Impromptu.root_resource.child(:'Framework::Extensions::Blog').files.size
98
103
  assert_equal 2, Impromptu.root_resource.child(:'Framework::Klass').files.size
99
104
  assert_equal 1, Impromptu.root_resource.child(:'Framework::Klass2').files.size
105
+ assert_equal 1, Impromptu.root_resource.child(:'Framework::Preload').files.size
100
106
  assert_equal 1, Impromptu.root_resource.child(:'Framework::Klass2').files.size
101
107
  assert_equal 1, Impromptu.root_resource.child(:'Load').files.size
102
108
  assert_equal 2, Impromptu.root_resource.child(:'OtherName').files.size
@@ -114,7 +120,6 @@ class TestIntegration < Test::Unit::TestCase
114
120
  # Loading/unloading
115
121
  # ----------------------------------------
116
122
  should "10 allow loading the implicitly namespace modules" do
117
- assert_equal false, Impromptu.root_resource.child(:'Framework').loaded?
118
123
  Impromptu.root_resource.child(:'Framework').reload
119
124
  assert_equal true, Impromptu.root_resource.child(:'Framework').loaded?
120
125
  assert_nothing_raised do
@@ -130,6 +135,13 @@ class TestIntegration < Test::Unit::TestCase
130
135
  end
131
136
 
132
137
  should "11 load resources using associated files when required" do
138
+ # preloaded resource
139
+ assert_nothing_raised do
140
+ Framework::Preload
141
+ end
142
+ assert_respond_to Framework::Preload, :method
143
+ assert Framework::Preload.method
144
+
133
145
  # ext
134
146
  Impromptu.root_resource.child(:'Framework::Extensions::Blog').reload
135
147
  assert_equal true, Impromptu.root_resource.child(:'Framework::Extensions').loaded?
@@ -245,6 +257,23 @@ class TestIntegration < Test::Unit::TestCase
245
257
  assert_equal true, Impromptu.root_resource.child(:'Framework::Extensions::Blog').loaded?
246
258
  end
247
259
 
260
+ should "15 automatically reload preloaded resources when their parent is reloaded" do
261
+ # ensure the preloaded resource is in fact preloaded
262
+ assert Impromptu.root_resource.child(:'Framework').loaded?
263
+ assert Impromptu.root_resource.child(:'Framework::Preload').loaded?
264
+ assert_nothing_raised do
265
+ Framework::Preload
266
+ end
267
+
268
+ # reload the parent resource and ensure the preloaded resource is still available
269
+ Impromptu.root_resource.child(:'Framework').reload
270
+ assert Impromptu.root_resource.child(:'Framework').loaded?
271
+ assert Impromptu.root_resource.child(:'Framework::Preload').loaded?
272
+ assert_nothing_raised do
273
+ Framework::Preload
274
+ end
275
+ end
276
+
248
277
 
249
278
  # ----------------------------------------
250
279
  # Updating files
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 1
7
- - 2
8
- - 1
9
- version: 1.2.1
7
+ - 3
8
+ - 0
9
+ version: 1.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Will Cannings
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-11-16 00:00:00 +11:00
17
+ date: 2011-01-21 00:00:00 +11:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -71,6 +71,7 @@ files:
71
71
  - test/framework/other/ignore.rb
72
72
  - test/framework/other/load.rb
73
73
  - test/framework/other/two.rb
74
+ - test/framework/preload/preload.rb
74
75
  - test/framework/private/klass.rb
75
76
  - test/framework/private/other.rb
76
77
  - test/framework/stdlib/string.rb
@@ -135,6 +136,7 @@ test_files:
135
136
  - test/framework/other/ignore.rb
136
137
  - test/framework/other/load.rb
137
138
  - test/framework/other/two.rb
139
+ - test/framework/preload/preload.rb
138
140
  - test/framework/private/klass.rb
139
141
  - test/framework/private/other.rb
140
142
  - test/framework/stdlib/string.rb