rapid-core 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/.rspec +2 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +53 -0
- data/Rakefile +61 -0
- data/cucumber.yml +3 -0
- data/doc/plan.txt +89 -0
- data/doc/scaffold/controller.rb +83 -0
- data/doc/scaffold/helper.rb +2 -0
- data/doc/scaffold/model.rb +5 -0
- data/doc/scaffold/rapid_scaffold.rb +5 -0
- data/doc/scaffold/views/_form.html.erb +25 -0
- data/doc/scaffold/views/edit.html.erb +6 -0
- data/doc/scaffold/views/index.html.erb +25 -0
- data/doc/scaffold/views/new.html.erb +5 -0
- data/doc/scaffold/views/show.html.erb +15 -0
- data/features/settings/boolean/default.feature +36 -0
- data/features/settings/double-nested/default.feature +50 -0
- data/features/settings/double-nested/if.feature +41 -0
- data/features/settings/double-nested/unless.feature +39 -0
- data/features/settings/double-nested/validates/exclusion_of.feature +30 -0
- data/features/settings/double-nested/validates/format_of.feature +30 -0
- data/features/settings/double-nested/validates/inclusion_of.feature +30 -0
- data/features/settings/double-nested/validates/length_of.feature +30 -0
- data/features/settings/double-nested/validates/presence_of.feature +37 -0
- data/features/settings/double-nested/validates/size_of.feature +30 -0
- data/features/settings/integer/default.feature +49 -0
- data/features/settings/nested/default.feature +50 -0
- data/features/settings/nested/if.feature +53 -0
- data/features/settings/nested/unless.feature +32 -0
- data/features/settings/nested/validates/exclusion_of.feature +30 -0
- data/features/settings/nested/validates/format_of.feature +30 -0
- data/features/settings/nested/validates/inclusion_of.feature +30 -0
- data/features/settings/nested/validates/length_of.feature +30 -0
- data/features/settings/nested/validates/presence_of.feature +37 -0
- data/features/settings/nested/validates/size_of.feature +30 -0
- data/features/settings/not_found.feature +33 -0
- data/features/settings/string/default.feature +36 -0
- data/features/settings/string/validates/exclusion_of.feature +30 -0
- data/features/settings/string/validates/format_of.feature +30 -0
- data/features/settings/string/validates/inclusion_of.feature +30 -0
- data/features/settings/string/validates/length_of.feature +30 -0
- data/features/settings/string/validates/presence_of.feature +37 -0
- data/features/settings/string/validates/size_of.feature +30 -0
- data/features/step_definitions/settings_steps.rb +92 -0
- data/features/step_definitions/skeleton_steps.rb +68 -0
- data/features/step_definitions/template_steps.rb +39 -0
- data/features/support/env.rb +6 -0
- data/features/templates/comment_if.feature +55 -0
- data/features/templates/comment_unless.feature +56 -0
- data/features/templates/for/for.feature +51 -0
- data/features/templates/if/else.feature +60 -0
- data/features/templates/if/elsif.feature +82 -0
- data/features/templates/if/if.feature +55 -0
- data/features/templates/namespaces/exist.feature +54 -0
- data/features/templates/static.feature +37 -0
- data/features/templates/variables.feature +61 -0
- data/lib/rapid/check.rb +162 -0
- data/lib/rapid/core.rb +37 -0
- data/lib/rapid/error.rb +84 -0
- data/lib/rapid/module.rb +35 -0
- data/lib/rapid/railtie.rb +18 -0
- data/lib/rapid/setting/base.rb +35 -0
- data/lib/rapid/setting/boolean_setting.rb +17 -0
- data/lib/rapid/setting/class_hash.rb +34 -0
- data/lib/rapid/setting/comments.rb +25 -0
- data/lib/rapid/setting/definer.rb +151 -0
- data/lib/rapid/setting/instance_hash.rb +132 -0
- data/lib/rapid/setting/instance_root.rb +107 -0
- data/lib/rapid/setting/integer_setting.rb +24 -0
- data/lib/rapid/setting/namespace/base.rb +113 -0
- data/lib/rapid/setting/namespace/instance.rb +84 -0
- data/lib/rapid/setting/nested_validations.rb +86 -0
- data/lib/rapid/setting/string_setting.rb +13 -0
- data/lib/rapid/settings.rb +129 -0
- data/lib/rapid/skeleton/base.rb +164 -0
- data/lib/rapid/skeleton/helpers/directory.rb +53 -0
- data/lib/rapid/skeleton/helpers/gem.rb +133 -0
- data/lib/rapid/skeleton/helpers/migration.rb +46 -0
- data/lib/rapid/skeleton/helpers/route.rb +115 -0
- data/lib/rapid/skeleton/helpers/script.rb +33 -0
- data/lib/rapid/skeleton/helpers/template.rb +73 -0
- data/lib/rapid/skeleton/helpers/view.rb +35 -0
- data/lib/rapid/spec/template.rb +104 -0
- data/lib/rapid/spec.rb +1 -0
- data/lib/rapid/tasks.rb +29 -0
- data/lib/rapid/template/base.rb +49 -0
- data/lib/rapid/template/node/base.rb +43 -0
- data/lib/rapid/template/node/comment_node.rb +42 -0
- data/lib/rapid/template/node/if_node.rb +109 -0
- data/lib/rapid/template/node/root.rb +51 -0
- data/lib/rapid/template/node/static.rb +29 -0
- data/lib/rapid/template/node/variable.rb +86 -0
- data/lib/rapid/template/parser.rb +167 -0
- data/lib/rapid/template/pulling/base.rb +91 -0
- data/lib/rapid/template/pulling/explicit.rb +92 -0
- data/lib/rapid/template/pulling/forgiving.rb +58 -0
- data/lib/rapid/version.rb +3 -0
- data/lib/rapid.rb +37 -0
- data/rapid-core.gemspec +26 -0
- data/spec/rapid/check_spec.rb +119 -0
- data/spec/rapid/error_spec.rb +14 -0
- data/spec/rapid/module_spec.rb +28 -0
- data/spec/rapid/setting/base_spec.rb +17 -0
- data/spec/rapid/setting/definer_spec.rb +318 -0
- data/spec/rapid/setting/instance_root_spec.rb +161 -0
- data/spec/rapid/setting/namespace/base_spec.rb +93 -0
- data/spec/rapid/setting/namespace/instance_spec.rb +12 -0
- data/spec/rapid/setting/nested_validations_spec.rb +72 -0
- data/spec/rapid/settings_spec.rb +51 -0
- data/spec/rapid/skeleton/base_spec.rb +224 -0
- data/spec/rapid/skeleton/helpers/directory_spec.rb +104 -0
- data/spec/rapid/skeleton/helpers/gem_spec.rb +180 -0
- data/spec/rapid/skeleton/helpers/migration_spec.rb +23 -0
- data/spec/rapid/skeleton/helpers/route_spec.rb +120 -0
- data/spec/rapid/skeleton/helpers/script_spec.rb +57 -0
- data/spec/rapid/skeleton/helpers/template_spec.rb +142 -0
- data/spec/rapid/skeleton/helpers/view_spec.rb +54 -0
- data/spec/rapid/spec/template_spec.rb +168 -0
- data/spec/rapid/template/base_spec.rb +290 -0
- data/spec/rapid/template/node/base_spec.rb +9 -0
- data/spec/rapid/template/node/comment_node_spec.rb +46 -0
- data/spec/rapid/template/node/if_node_spec.rb +28 -0
- data/spec/rapid/template/node/root_spec.rb +9 -0
- data/spec/rapid/template/node/static_spec.rb +17 -0
- data/spec/rapid/template/node/variable_spec.rb +87 -0
- data/spec/rapid/template/parser_spec.rb +187 -0
- data/spec/rapid/template/pulling/base_spec.rb +81 -0
- data/spec/rapid/template/pulling/explicit_spec.rb +33 -0
- data/spec/rapid/template/pulling/forgiving_spec.rb +132 -0
- data/spec/spec_helper.rb +10 -0
- metadata +325 -0
@@ -0,0 +1,318 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Rapid::Setting::Definer do
|
4
|
+
|
5
|
+
before do
|
6
|
+
Object.class_eval do
|
7
|
+
class MyClass
|
8
|
+
include Rapid::Settings
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
@definer = Rapid::Setting::Definer.new MyClass
|
13
|
+
end
|
14
|
+
|
15
|
+
after do
|
16
|
+
Object.send(:remove_const, :MyClass) rescue nil
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "boolean" do
|
20
|
+
|
21
|
+
it "should define a setting" do
|
22
|
+
@definer.boolean 'my_boolean'
|
23
|
+
|
24
|
+
@instance = MyClass.new
|
25
|
+
@instance.my_boolean.should == nil
|
26
|
+
@instance.my_boolean?.should == false
|
27
|
+
|
28
|
+
@instance.my_boolean = true
|
29
|
+
@instance.my_boolean.should == true
|
30
|
+
@instance.my_boolean?.should == true
|
31
|
+
|
32
|
+
@instance.my_boolean = false
|
33
|
+
@instance.my_boolean.should == false
|
34
|
+
@instance.my_boolean?.should == false
|
35
|
+
|
36
|
+
@instance.my_boolean = nil
|
37
|
+
@instance.my_boolean.should == nil
|
38
|
+
@instance.my_boolean?.should == false
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should define a setting with a default of true" do
|
42
|
+
@definer.boolean 'my_boolean', :default => true
|
43
|
+
|
44
|
+
@instance = MyClass.new
|
45
|
+
@instance.my_boolean.should == true
|
46
|
+
@instance.my_boolean?.should == true
|
47
|
+
|
48
|
+
@instance.my_boolean = false
|
49
|
+
@instance.my_boolean.should == false
|
50
|
+
@instance.my_boolean?.should == false
|
51
|
+
|
52
|
+
@instance.my_boolean = nil
|
53
|
+
@instance.my_boolean.should == nil
|
54
|
+
@instance.my_boolean?.should == false
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should define a setting with a default of false" do
|
58
|
+
@definer.boolean 'my_boolean', :default => false
|
59
|
+
|
60
|
+
@instance = MyClass.new
|
61
|
+
@instance.my_boolean.should == false
|
62
|
+
@instance.my_boolean?.should == false
|
63
|
+
|
64
|
+
@instance.my_boolean = true
|
65
|
+
@instance.my_boolean.should == true
|
66
|
+
@instance.my_boolean?.should == true
|
67
|
+
|
68
|
+
@instance.my_boolean = nil
|
69
|
+
@instance.my_boolean.should == nil
|
70
|
+
@instance.my_boolean?.should == false
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should defined within an existing namespace" do
|
74
|
+
@definer.namespace 'foo'
|
75
|
+
@definer.boolean 'foo.bar', :default => false
|
76
|
+
|
77
|
+
@instance = MyClass.new
|
78
|
+
@instance.foo.bar.should == false
|
79
|
+
@instance.foo.bar?.should == false
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
describe "string" do
|
85
|
+
|
86
|
+
it "should define a setting" do
|
87
|
+
@definer.string 'my_string'
|
88
|
+
|
89
|
+
@instance = MyClass.new
|
90
|
+
@instance.my_string.should == nil
|
91
|
+
@instance.my_string?.should == false
|
92
|
+
|
93
|
+
@instance.my_string = "foo"
|
94
|
+
@instance.my_string.should == "foo"
|
95
|
+
@instance.my_string?.should == true
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should define a setting with a default" do
|
99
|
+
@definer.string 'my_string', :default => "foo"
|
100
|
+
|
101
|
+
@instance = MyClass.new
|
102
|
+
@instance.my_string.should == "foo"
|
103
|
+
|
104
|
+
@instance.my_string = "bar"
|
105
|
+
@instance.my_string.should == "bar"
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should know the namespace is empty even when there is a default child value" do
|
109
|
+
@definer.namespace 'foo'
|
110
|
+
@definer.string 'foo.name', :default => 'Dan'
|
111
|
+
|
112
|
+
@instance = MyClass.new
|
113
|
+
@instance.foo = false
|
114
|
+
@instance.foo?.should == false
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
describe "integer" do
|
120
|
+
|
121
|
+
it "should define a setting" do
|
122
|
+
@definer.integer 'my_integer'
|
123
|
+
|
124
|
+
@instance = MyClass.new
|
125
|
+
@instance.my_integer.should == nil
|
126
|
+
@instance.my_integer?.should == false
|
127
|
+
@instance.my_integer = 1
|
128
|
+
@instance.my_integer.should == 1
|
129
|
+
@instance.my_integer?.should == true
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should define a setting with a default" do
|
133
|
+
@definer.integer 'my_integer', :default => 1
|
134
|
+
|
135
|
+
@instance = MyClass.new
|
136
|
+
@instance.my_integer.should == 1
|
137
|
+
@instance.my_integer = 2
|
138
|
+
@instance.my_integer.should == 2
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
|
143
|
+
describe "namespace" do
|
144
|
+
|
145
|
+
it "should define a setting" do
|
146
|
+
@definer.namespace 'my_namespace'
|
147
|
+
|
148
|
+
@instance = MyClass.new
|
149
|
+
@instance.my_namespace.should_not be_nil
|
150
|
+
end
|
151
|
+
|
152
|
+
it "should define a setting with default true" do
|
153
|
+
@definer.namespace 'my_namespace', :default => true
|
154
|
+
|
155
|
+
@instance = MyClass.new
|
156
|
+
@instance.my_namespace?.should be_true
|
157
|
+
@instance.my_namespace = nil
|
158
|
+
@instance.my_namespace?.should be_false
|
159
|
+
end
|
160
|
+
|
161
|
+
it "should define a setting with default false" do
|
162
|
+
@definer.namespace 'my_namespace', :default => false
|
163
|
+
|
164
|
+
@instance = MyClass.new
|
165
|
+
@instance.my_namespace?.should be_false
|
166
|
+
@instance.my_namespace = {}
|
167
|
+
@instance.my_namespace?.should be_true
|
168
|
+
end
|
169
|
+
|
170
|
+
it "should define a setting under a namespace" do
|
171
|
+
@definer.string 'app.name'
|
172
|
+
|
173
|
+
@instance = MyClass.new
|
174
|
+
@instance.app.name?.should == false
|
175
|
+
@instance.app.name.should == nil
|
176
|
+
@instance.app.name = "Hi"
|
177
|
+
@instance.app.name.should == "Hi"
|
178
|
+
end
|
179
|
+
|
180
|
+
it "should know nil" do
|
181
|
+
@definer.namespace 'app', :default => true
|
182
|
+
|
183
|
+
@instance = MyClass.new
|
184
|
+
@instance.app?.should == true
|
185
|
+
@instance.app = nil
|
186
|
+
@instance.app?.should == false
|
187
|
+
end
|
188
|
+
|
189
|
+
it "should know {}" do
|
190
|
+
@definer.namespace 'app', :default => false
|
191
|
+
|
192
|
+
@instance = MyClass.new
|
193
|
+
@instance.app?.should == false
|
194
|
+
@instance.app = {}
|
195
|
+
@instance.app?.should == true
|
196
|
+
end
|
197
|
+
|
198
|
+
it "should know true" do
|
199
|
+
@definer.namespace 'app', :default => false
|
200
|
+
|
201
|
+
@instance = MyClass.new
|
202
|
+
@instance.app?.should == false
|
203
|
+
@instance.app = true
|
204
|
+
@instance.app?.should == true
|
205
|
+
end
|
206
|
+
|
207
|
+
it "should know false" do
|
208
|
+
@definer.namespace 'app', :default => true
|
209
|
+
|
210
|
+
@instance = MyClass.new
|
211
|
+
@instance.app?.should == true
|
212
|
+
@instance.app = false
|
213
|
+
@instance.app?.should == false
|
214
|
+
end
|
215
|
+
|
216
|
+
end
|
217
|
+
|
218
|
+
describe "nested namespaces" do
|
219
|
+
|
220
|
+
it "should define a nested namespace" do
|
221
|
+
@definer.namespace 'parent.child'
|
222
|
+
|
223
|
+
@instance = MyClass.new
|
224
|
+
@instance.parent.child.should_not be_nil
|
225
|
+
|
226
|
+
@instance.parent.empty?.should == true
|
227
|
+
@instance.parent.child.empty?.should == true
|
228
|
+
end
|
229
|
+
|
230
|
+
it "should define a nested setting with default true" do
|
231
|
+
@definer.namespace 'parent.child', :default => true
|
232
|
+
|
233
|
+
@instance = MyClass.new
|
234
|
+
|
235
|
+
@instance.parent.empty?.should == false
|
236
|
+
@instance.parent.child.empty?.should == false
|
237
|
+
|
238
|
+
@instance.parent.child?.should be_true
|
239
|
+
@instance.parent.child = nil
|
240
|
+
@instance.parent.child?.should be_false
|
241
|
+
|
242
|
+
@instance.parent.empty?.should == true
|
243
|
+
@instance.parent.child.empty?.should == true
|
244
|
+
end
|
245
|
+
|
246
|
+
it "should define a nested setting with default false" do
|
247
|
+
@definer.namespace 'parent.child', :default => false
|
248
|
+
|
249
|
+
@instance = MyClass.new
|
250
|
+
|
251
|
+
@instance.parent.empty?.should == true
|
252
|
+
@instance.parent.child.empty?.should == true
|
253
|
+
|
254
|
+
@instance.parent.child?.should be_false
|
255
|
+
@instance.parent.child = {}
|
256
|
+
@instance.parent.child?.should be_true
|
257
|
+
|
258
|
+
@instance.parent.empty?.should == false
|
259
|
+
@instance.parent.child.empty?.should == false
|
260
|
+
end
|
261
|
+
|
262
|
+
end
|
263
|
+
|
264
|
+
describe "code" do
|
265
|
+
|
266
|
+
it "should define a setting" do
|
267
|
+
@definer.code 'my_code'
|
268
|
+
|
269
|
+
@instance = MyClass.new
|
270
|
+
@instance.my_code.should == nil
|
271
|
+
@instance.my_code = "1 + 1"
|
272
|
+
@instance.my_code.should == "1 + 1"
|
273
|
+
end
|
274
|
+
|
275
|
+
it "should define a setting with a default" do
|
276
|
+
@definer.code 'my_code', :default => "2"
|
277
|
+
|
278
|
+
@instance = MyClass.new
|
279
|
+
@instance.my_code.should == "2"
|
280
|
+
@instance.my_code = "1 + 1"
|
281
|
+
@instance.my_code.should == "1 + 1"
|
282
|
+
end
|
283
|
+
|
284
|
+
end
|
285
|
+
|
286
|
+
describe "class_eval" do
|
287
|
+
|
288
|
+
it "should expose class_eval to override namespaces" do
|
289
|
+
@definer.namespace 'my_namespace'
|
290
|
+
@definer.class_eval 'my_namespace' do
|
291
|
+
def foo
|
292
|
+
"bar"
|
293
|
+
end
|
294
|
+
end
|
295
|
+
|
296
|
+
@instance = MyClass.new
|
297
|
+
@instance.my_namespace.foo.should == "bar"
|
298
|
+
end
|
299
|
+
|
300
|
+
it "should raise an error when the namespace isn't defined yet" do
|
301
|
+
@definer.namespace 'my_namespace'
|
302
|
+
lambda { @definer.class_eval('other_namespace') {} }.should raise_error(ArgumentError)
|
303
|
+
end
|
304
|
+
|
305
|
+
it "should raise an error when the parent class doesn't allow custom instance classes" do
|
306
|
+
@definer.namespace 'my_namespace'
|
307
|
+
MyClass.settings["my_namespace"].instance_variable_set :@instance_class, nil
|
308
|
+
|
309
|
+
lambda { @definer.class_eval('my_namespace') {} }.should raise_error(ArgumentError)
|
310
|
+
end
|
311
|
+
|
312
|
+
end
|
313
|
+
|
314
|
+
it "should over-ride inspect" do
|
315
|
+
Rapid::Setting::Definer.new(Object).inspect.class.should == String
|
316
|
+
end
|
317
|
+
|
318
|
+
end
|
@@ -0,0 +1,161 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Rapid::Setting::InstanceRoot do
|
4
|
+
|
5
|
+
it "should expose its binding" do
|
6
|
+
@class_hash = Rapid::Setting::ClassHash.new
|
7
|
+
@instance_hash = Rapid::Setting::InstanceRoot.new @class_hash
|
8
|
+
|
9
|
+
@instance_hash.get_binding.should_not be_nil
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "namespaces" do
|
13
|
+
|
14
|
+
before do
|
15
|
+
@class_hash = Rapid::Setting::ClassHash.new
|
16
|
+
@app = Rapid::Setting::Namespace::Base.new(:app)
|
17
|
+
@name = Rapid::Setting::StringSetting.new(:name, :namespace => @app)
|
18
|
+
|
19
|
+
@class_hash[:app] = @app
|
20
|
+
@app[:name] = @name
|
21
|
+
|
22
|
+
@instance_hash = Rapid::Setting::InstanceRoot.new @class_hash
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should load the namespaces from nested hashes" do
|
26
|
+
@instance_hash.load_hash :app => {:name => "Rapid"}
|
27
|
+
@instance_hash[:app][:name].should == "Rapid"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should load the namespaces using dot-notation" do
|
31
|
+
@instance_hash.load_hash 'app.name' => "Rapid"
|
32
|
+
@instance_hash[:app][:name].should == "Rapid"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should output the yaml in dot-notation" do
|
36
|
+
@instance_hash.load_hash 'app.name' => "Rapid"
|
37
|
+
@instance_hash.to_yaml.should == %(app.name: Rapid)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should provide [] access to nested settings" do
|
41
|
+
@instance_hash.load_hash 'app.name' => "Rapid"
|
42
|
+
@instance_hash['app.name'].should == "Rapid"
|
43
|
+
|
44
|
+
@instance_hash['app.invalid'].should == nil
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "two-deep namespaces" do
|
50
|
+
|
51
|
+
before do
|
52
|
+
@class_hash = Rapid::Setting::ClassHash.new
|
53
|
+
@app = Rapid::Setting::Namespace::Base.new(:app)
|
54
|
+
@author = Rapid::Setting::Namespace::Base.new(:author)
|
55
|
+
@name = Rapid::Setting::StringSetting.new(:name, :namespace => @autor)
|
56
|
+
|
57
|
+
@class_hash[:app] = @app
|
58
|
+
@app[:author] = @author
|
59
|
+
@author[:name] = @name
|
60
|
+
|
61
|
+
@instance_hash = Rapid::Setting::InstanceRoot.new @class_hash
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should load the namespaces from nested hashes" do
|
65
|
+
@instance_hash.load_hash :app => {:author => {:name => "Dan"} }
|
66
|
+
@instance_hash[:app][:author][:name].should == "Dan"
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should load the namespaces using dot-notation" do
|
70
|
+
@instance_hash.load_hash 'app.author.name' => "Dan"
|
71
|
+
@instance_hash[:app][:author][:name].should == "Dan"
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should output the yaml in dot-notation" do
|
75
|
+
@instance_hash.load_hash 'app.author.name' => "Dan"
|
76
|
+
@instance_hash.to_yaml.should == %(app.author.name: Dan)
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should provide [] access" do
|
80
|
+
@instance_hash.load_hash 'app.author.name' => "Dan"
|
81
|
+
@instance_hash['app.author.name'].should == "Dan"
|
82
|
+
|
83
|
+
@instance_hash['app.author.invalid'].should == nil
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should know set?" do
|
87
|
+
@instance_hash.set?('app').should == false
|
88
|
+
@instance_hash.set?('app.author').should == false
|
89
|
+
@instance_hash.set?('app.author.name').should == false
|
90
|
+
@instance_hash.set?('app.author.invalid').should == false
|
91
|
+
|
92
|
+
@instance_hash.load_hash 'app.author.name' => "Dan"
|
93
|
+
|
94
|
+
@instance_hash.set?('app').should == true
|
95
|
+
@instance_hash.set?('app.author').should == true
|
96
|
+
@instance_hash.set?('app.author.name').should == true
|
97
|
+
@instance_hash.set?('app.author.invalid').should == false
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
|
102
|
+
describe "YAML" do
|
103
|
+
|
104
|
+
before do
|
105
|
+
@class_hash = Rapid::Setting::ClassHash.new
|
106
|
+
@class_hash[:name] = Rapid::Setting::StringSetting.new(:name)
|
107
|
+
@class_hash[:version] = Rapid::Setting::StringSetting.new(:version)
|
108
|
+
|
109
|
+
@instance_hash = Rapid::Setting::InstanceRoot.new @class_hash
|
110
|
+
end
|
111
|
+
|
112
|
+
describe "loading" do
|
113
|
+
|
114
|
+
def load_yaml content
|
115
|
+
@instance_hash.load_yaml content
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should load the hash with values" do
|
119
|
+
load_yaml %(name: Dan)
|
120
|
+
@instance_hash["name"].should == "Dan"
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should handle empty files" do
|
124
|
+
load_yaml %()
|
125
|
+
@instance_hash.empty?.should be_true
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should raise an error when an unknown setting is specified" do
|
129
|
+
load_yaml %(foo: bar)
|
130
|
+
@instance_hash.valid?.should be_false
|
131
|
+
@instance_hash.errors["foo"].should == ["can't be found"]
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should raise error when yaml is invalid" do
|
135
|
+
lambda { load_yaml %(foo: d\nea5#) }.should raise_error(Rapid::InvalidYamlError)
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
describe "to_yaml" do
|
141
|
+
|
142
|
+
def to_yaml
|
143
|
+
@instance_hash.to_yaml
|
144
|
+
end
|
145
|
+
|
146
|
+
it "should write current settings" do
|
147
|
+
@instance_hash["name"] = "Dan"
|
148
|
+
@instance_hash["version"] = "1.0"
|
149
|
+
|
150
|
+
to_yaml.should == %(name: Dan\nversion: "1.0")
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should handle empty settings" do
|
154
|
+
to_yaml.should == %()
|
155
|
+
end
|
156
|
+
|
157
|
+
end
|
158
|
+
|
159
|
+
end
|
160
|
+
|
161
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
|
2
|
+
|
3
|
+
describe Rapid::Setting::Namespace::Base do
|
4
|
+
|
5
|
+
before do
|
6
|
+
Object.class_eval do
|
7
|
+
class MySkeleton
|
8
|
+
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
@skeleton = MySkeleton.new
|
13
|
+
end
|
14
|
+
|
15
|
+
after do
|
16
|
+
Object.send(:remove_const, :MySkeleton) rescue nil
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "accessor class" do
|
20
|
+
|
21
|
+
before do
|
22
|
+
@namespace = Rapid::Setting::Namespace::Base.new :app, :class => MySkeleton
|
23
|
+
|
24
|
+
@name = Rapid::Setting::StringSetting.new :name, :namespace => @namespace
|
25
|
+
@version = Rapid::Setting::StringSetting.new :version, :namespace => @namespace
|
26
|
+
|
27
|
+
@namespace.define_setting @name
|
28
|
+
@namespace.define_setting @version
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should know the full name" do
|
32
|
+
@version.full_name.should == "app.version"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should initialize a class to hold the accessor methods" do
|
36
|
+
lambda { MySkeleton::Settings::App }.should_not raise_error
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should set the class's setting_name" do
|
40
|
+
MySkeleton::Settings::App.setting_name.should == "app"
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should generate the accessor methods" do
|
44
|
+
@instance = @namespace.load :name => "Dan", :version => "1.0"
|
45
|
+
@instance.name.should == "Dan"
|
46
|
+
@instance.version.should == "1.0"
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "two-deep class" do
|
52
|
+
|
53
|
+
before do
|
54
|
+
@namespace = Rapid::Setting::Namespace::Base.new :app, :class => MySkeleton
|
55
|
+
@author = Rapid::Setting::Namespace::Base.new :author, :namespace => @namespace, :class => MySkeleton
|
56
|
+
|
57
|
+
@name = Rapid::Setting::StringSetting.new :name, :namespace => @author
|
58
|
+
@version = Rapid::Setting::StringSetting.new :version, :namespace => @author
|
59
|
+
|
60
|
+
@namespace.define_setting @author
|
61
|
+
@author.define_setting @name
|
62
|
+
@author.define_setting @version
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should know the full name" do
|
66
|
+
@version.full_name.should == "app.author.version"
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should initialize a class to hold the accessor methods" do
|
70
|
+
lambda { MySkeleton::Settings::App }.should_not raise_error
|
71
|
+
lambda { MySkeleton::Settings::AppAuthor }.should_not raise_error
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should set the class's setting_name" do
|
75
|
+
MySkeleton::Settings::App.setting_name.should == "app"
|
76
|
+
MySkeleton::Settings::AppAuthor.setting_name.should == "author"
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should generate the accessor methods" do
|
80
|
+
@instance = @namespace.load :author => {:name => "Dan", :version => "1.0"}, :owner => "hi"
|
81
|
+
@instance.author.name.should == "Dan"
|
82
|
+
@instance.author.version.should == "1.0"
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should provide access to the owner" do
|
86
|
+
@instance = @namespace.load :owner => "hi"
|
87
|
+
@instance._root.should == "hi"
|
88
|
+
@instance.author._root.should == "hi"
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
|
2
|
+
|
3
|
+
describe Rapid::Setting::Namespace::Instance do
|
4
|
+
|
5
|
+
it "should alias root as _root by default" do
|
6
|
+
obj = mock(:default_empty => true, :scalars => [])
|
7
|
+
@instance = Rapid::Setting::Namespace::Instance.new "hi", obj
|
8
|
+
@instance.root.should == "hi"
|
9
|
+
@instance._root.should == "hi"
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Rapid::Setting::NestedValidations do
|
4
|
+
|
5
|
+
before do
|
6
|
+
Object.class_eval do
|
7
|
+
class A
|
8
|
+
include ActiveModel::Validations
|
9
|
+
include Rapid::Setting::NestedValidations
|
10
|
+
attr_accessor :b, :value
|
11
|
+
end
|
12
|
+
|
13
|
+
class B
|
14
|
+
include ActiveModel::Validations
|
15
|
+
include Rapid::Setting::NestedValidations
|
16
|
+
attr_accessor :root, :c, :value
|
17
|
+
end
|
18
|
+
|
19
|
+
class C
|
20
|
+
include ActiveModel::Validations
|
21
|
+
include Rapid::Setting::NestedValidations
|
22
|
+
attr_accessor :root, :value
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
@a = A.new
|
27
|
+
@a.b = B.new
|
28
|
+
@a.b.c = C.new
|
29
|
+
end
|
30
|
+
|
31
|
+
after do
|
32
|
+
Object.send(:remove_const, :A) rescue nil
|
33
|
+
Object.send(:remove_const, :B) rescue nil
|
34
|
+
Object.send(:remove_const, :C) rescue nil
|
35
|
+
end
|
36
|
+
|
37
|
+
def init_a
|
38
|
+
@a = A.new
|
39
|
+
@a.b = B.new
|
40
|
+
@a.b.c = C.new
|
41
|
+
|
42
|
+
@a.b.root = @a
|
43
|
+
@a.b.c.root = @a
|
44
|
+
|
45
|
+
@a
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should obey dot-operators in the if clause" do
|
49
|
+
C.send :validates_presence_of, :value, :if => 'root.value'
|
50
|
+
init_a
|
51
|
+
@a.b.c.valid?.should == true
|
52
|
+
@a.value = 2
|
53
|
+
@a.b.c.valid?.should == false
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should obey dot-operators in the unless clause" do
|
57
|
+
C.send :validates_presence_of, :value, :unless => 'root.value'
|
58
|
+
init_a
|
59
|
+
@a.b.c.valid?.should == false
|
60
|
+
@a.value = 2
|
61
|
+
@a.b.c.valid?.should == true
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should raise an error when validating an unknown nested value" do
|
65
|
+
@settings = mock('setting')
|
66
|
+
@settings.should_receive(:find_namespace).and_return(nil)
|
67
|
+
A.should_receive(:setting).and_return(@settings)
|
68
|
+
|
69
|
+
lambda { A.validates_presence_of 'app.author' }.should raise_error(Rapid::UnknownSettingError)
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe Rapid::Settings do
|
4
|
+
|
5
|
+
before do
|
6
|
+
Object.class_eval do
|
7
|
+
class MyClass
|
8
|
+
include Rapid::Settings
|
9
|
+
|
10
|
+
setting.string 'foo'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
@instance = MyClass.new
|
15
|
+
end
|
16
|
+
|
17
|
+
after do
|
18
|
+
Object.send(:remove_const, :MyClass) rescue nil
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "extract_nested_namespace" do
|
22
|
+
|
23
|
+
it "should return the namespace and rest" do
|
24
|
+
Rapid::Settings.extract_nested_namespace("foo.bar").should == ["foo", "bar"]
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should return nil and original when not namespaced" do
|
28
|
+
Rapid::Settings.extract_nested_namespace("foo").should == [nil, "foo"]
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should handle nil" do
|
32
|
+
Rapid::Settings.extract_nested_namespace(nil).should == [nil, nil]
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should handle a symbol" do
|
36
|
+
Rapid::Settings.extract_nested_namespace(:foo).should == [nil, 'foo']
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should expose the setting's bindings by default" do
|
42
|
+
@instance.get_binding.should_not be_nil
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should provide array-like access" do
|
46
|
+
@instance['foo'] = 'hi'
|
47
|
+
@instance['foo'].should == 'hi'
|
48
|
+
@instance.foo.should == 'hi'
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|