constructor 1.0.0 → 1.0.1
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/History.txt +4 -0
- data/Rakefile +10 -0
- data/lib/constructor.rb +16 -5
- data/specs/constructor_spec.rb +135 -121
- metadata +51 -44
data/History.txt
CHANGED
data/Rakefile
CHANGED
@@ -21,3 +21,13 @@ Spec::Rake::SpecTask.new(:spec) do |t|
|
|
21
21
|
t.spec_files = FileList['specs/*_spec.rb']
|
22
22
|
t.spec_opts << '-c -f s'
|
23
23
|
end
|
24
|
+
|
25
|
+
if File.exists?("../tools/")
|
26
|
+
load "../tools/tasks/homepage.rake"
|
27
|
+
load "../tools/tasks/release_tagging.rake"
|
28
|
+
ReleaseTagging.new do |t|
|
29
|
+
t.package = "constructor"
|
30
|
+
t.version = CONSTRUCTOR_VERSION
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
data/lib/constructor.rb
CHANGED
@@ -1,7 +1,12 @@
|
|
1
|
-
CONSTRUCTOR_VERSION = '1.0.
|
1
|
+
CONSTRUCTOR_VERSION = '1.0.1' #:nodoc:#
|
2
2
|
|
3
|
-
class Class
|
4
|
-
def constructor(*attrs)
|
3
|
+
class Class #:nodoc:#
|
4
|
+
def constructor(*attrs, &block)
|
5
|
+
call_block = ''
|
6
|
+
if block_given?
|
7
|
+
@constructor_block = block
|
8
|
+
call_block = 'self.instance_eval(&self.class.constructor_block)'
|
9
|
+
end
|
5
10
|
# Look for embedded options in the listing:
|
6
11
|
opts = attrs.find { |a| a.kind_of?(Hash) and attrs.delete(a) }
|
7
12
|
do_acc = opts.nil? ? false : opts[:accessors] == true
|
@@ -73,6 +78,7 @@ class Class
|
|
73
78
|
#{validation_code}
|
74
79
|
#{assigns}
|
75
80
|
setup if respond_to?(:setup)
|
81
|
+
#{call_block}
|
76
82
|
end
|
77
83
|
}
|
78
84
|
|
@@ -81,11 +87,16 @@ class Class
|
|
81
87
|
end
|
82
88
|
|
83
89
|
# Access the constructor keys for this class
|
84
|
-
def constructor_keys; @_ctor_keys; end
|
90
|
+
def constructor_keys; @_ctor_keys ||=[]; end
|
91
|
+
|
92
|
+
def constructor_block #:nodoc:#
|
93
|
+
@constructor_block
|
94
|
+
end
|
95
|
+
|
85
96
|
end
|
86
97
|
|
87
98
|
# Fancy validation exception, based on missing and extraneous keys.
|
88
|
-
class ConstructorArgumentError < RuntimeError
|
99
|
+
class ConstructorArgumentError < RuntimeError #:nodoc:#
|
89
100
|
def initialize(missing,rejected=[])
|
90
101
|
err_msg = ''
|
91
102
|
if missing.size > 0
|
data/specs/constructor_spec.rb
CHANGED
@@ -1,123 +1,5 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/../lib/constructor')
|
2
2
|
|
3
|
-
class TestingClass
|
4
|
-
attr_accessor :foo, :bar, :why, :qux
|
5
|
-
constructor :foo, :bar, :why, :qux, :strict => false
|
6
|
-
|
7
|
-
def to_pretty_pretty
|
8
|
-
"#{@foo} #{@bar}"
|
9
|
-
end
|
10
|
-
|
11
|
-
end
|
12
|
-
|
13
|
-
class Mama
|
14
|
-
attr_accessor :fat, :age
|
15
|
-
constructor :age, :strict => false
|
16
|
-
def setup
|
17
|
-
@fat = "much"
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
class Baby < Mama
|
22
|
-
constructor :cuteness
|
23
|
-
end
|
24
|
-
|
25
|
-
class Sissy < Mama
|
26
|
-
attr_accessor :friends, :beauty
|
27
|
-
constructor :beauty, :strict => false
|
28
|
-
def setup
|
29
|
-
super #IMPORTANT!
|
30
|
-
@friends = "many"
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
class TestingStrictArgsDefault
|
35
|
-
constructor :foo, :bar
|
36
|
-
def to_pretty_pretty
|
37
|
-
"#{@foo} #{@bar}"
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
class TestingStrictArgs
|
42
|
-
constructor :foo, :bar, :strict => true
|
43
|
-
def to_pretty_pretty
|
44
|
-
"#{@foo} #{@bar}"
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
class TestingStrictArgs2
|
49
|
-
constructor :foo, :bar, :accessors => true
|
50
|
-
end
|
51
|
-
|
52
|
-
class SubclassOfTestingClass < TestingClass
|
53
|
-
end
|
54
|
-
|
55
|
-
class SubclassOfTestingClass2 < TestingClass
|
56
|
-
def initialize; end
|
57
|
-
end
|
58
|
-
|
59
|
-
class SubclassOfTestingClass3 < TestingClass
|
60
|
-
attr_reader :my_new_var
|
61
|
-
def initialize(hash = nil)
|
62
|
-
super
|
63
|
-
@my_new_var = "something"
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
class TestingAutoAccessors
|
68
|
-
constructor :foo, :bar, :why, :qux, :accessors => true, :strict => false
|
69
|
-
def to_pretty_pretty
|
70
|
-
"#{@foo} #{@bar}"
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
class TestingSuperConstructorBase
|
75
|
-
attr_reader :a, :b
|
76
|
-
def initialize(a,b)
|
77
|
-
@a = a
|
78
|
-
@b = b
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
class TestingSuperConstructor < TestingSuperConstructorBase
|
83
|
-
constructor :far, :away, :accessors => true, :super => ["once", :twice], :strict => false
|
84
|
-
end
|
85
|
-
|
86
|
-
class TestingSuperConstructorBase2
|
87
|
-
attr_reader :c, :d
|
88
|
-
def initialize
|
89
|
-
@c = 'what a'
|
90
|
-
@d = 'day for'
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
class TestingSuperConstructor2 < TestingSuperConstructorBase2
|
95
|
-
constructor :some, :accessors => true, :super => [], :strict => false
|
96
|
-
end
|
97
|
-
|
98
|
-
class TestingBlockedAccessors
|
99
|
-
constructor :foo, :bar, :accessors => false
|
100
|
-
def to_pretty_pretty
|
101
|
-
"#{@foo} #{@bar}"
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
class Papa
|
106
|
-
constructor :car, :saw
|
107
|
-
end
|
108
|
-
|
109
|
-
class Sonny < Papa
|
110
|
-
constructor :computer, :accessors => true
|
111
|
-
end
|
112
|
-
|
113
|
-
class Llamma
|
114
|
-
attr_accessor :hungry, :hair
|
115
|
-
constructor :hair
|
116
|
-
def setup
|
117
|
-
@hungry = true
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
3
|
describe 'standard constructor usage' do
|
122
4
|
it 'allows for object construction using a hash of named arguments' do
|
123
5
|
fuh = TestingClass.new(
|
@@ -291,9 +173,11 @@ describe 'stict mode usage' do
|
|
291
173
|
end
|
292
174
|
|
293
175
|
it 'does not allow extraneous arguments when strict option is true' do
|
294
|
-
|
295
|
-
|
296
|
-
|
176
|
+
[ /thing/, /other/ ].each do |rejected_arg|
|
177
|
+
lambda {
|
178
|
+
TestingStrictArgs.new(:foo => 1, :bar => 2, :other => 3, :thing => 4)
|
179
|
+
}.should raise_error(ConstructorArgumentError, rejected_arg)
|
180
|
+
end
|
297
181
|
end
|
298
182
|
|
299
183
|
it 'allows for setting accessors option while in strict mode' do
|
@@ -317,3 +201,133 @@ describe 'catching ConstructorArgumentError' do
|
|
317
201
|
end
|
318
202
|
end
|
319
203
|
end
|
204
|
+
|
205
|
+
describe 'block yielding' do
|
206
|
+
it 'executes a specified block after instantiating' do
|
207
|
+
TestingBlockYield.new(:a => false).a.should == true
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
class TestingClass
|
212
|
+
attr_accessor :foo, :bar, :why, :qux
|
213
|
+
constructor :foo, :bar, :why, :qux, :strict => false
|
214
|
+
|
215
|
+
def to_pretty_pretty
|
216
|
+
"#{@foo} #{@bar}"
|
217
|
+
end
|
218
|
+
|
219
|
+
end
|
220
|
+
|
221
|
+
class Mama
|
222
|
+
attr_accessor :fat, :age
|
223
|
+
constructor :age, :strict => false
|
224
|
+
def setup
|
225
|
+
@fat = "much"
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
class Baby < Mama
|
230
|
+
constructor :cuteness
|
231
|
+
end
|
232
|
+
|
233
|
+
class Sissy < Mama
|
234
|
+
attr_accessor :friends, :beauty
|
235
|
+
constructor :beauty, :strict => false
|
236
|
+
def setup
|
237
|
+
super #IMPORTANT!
|
238
|
+
@friends = "many"
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
class TestingStrictArgsDefault
|
243
|
+
constructor :foo, :bar
|
244
|
+
def to_pretty_pretty
|
245
|
+
"#{@foo} #{@bar}"
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
class TestingStrictArgs
|
250
|
+
constructor :foo, :bar, :strict => true
|
251
|
+
def to_pretty_pretty
|
252
|
+
"#{@foo} #{@bar}"
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
class TestingStrictArgs2
|
257
|
+
constructor :foo, :bar, :accessors => true
|
258
|
+
end
|
259
|
+
|
260
|
+
class SubclassOfTestingClass < TestingClass
|
261
|
+
end
|
262
|
+
|
263
|
+
class SubclassOfTestingClass2 < TestingClass
|
264
|
+
def initialize; end
|
265
|
+
end
|
266
|
+
|
267
|
+
class SubclassOfTestingClass3 < TestingClass
|
268
|
+
attr_reader :my_new_var
|
269
|
+
def initialize(hash = nil)
|
270
|
+
super
|
271
|
+
@my_new_var = "something"
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
class TestingAutoAccessors
|
276
|
+
constructor :foo, :bar, :why, :qux, :accessors => true, :strict => false
|
277
|
+
def to_pretty_pretty
|
278
|
+
"#{@foo} #{@bar}"
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
282
|
+
class TestingSuperConstructorBase
|
283
|
+
attr_reader :a, :b
|
284
|
+
def initialize(a,b)
|
285
|
+
@a = a
|
286
|
+
@b = b
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
class TestingSuperConstructor < TestingSuperConstructorBase
|
291
|
+
constructor :far, :away, :accessors => true, :super => ["once", :twice], :strict => false
|
292
|
+
end
|
293
|
+
|
294
|
+
class TestingSuperConstructorBase2
|
295
|
+
attr_reader :c, :d
|
296
|
+
def initialize
|
297
|
+
@c = 'what a'
|
298
|
+
@d = 'day for'
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
302
|
+
class TestingSuperConstructor2 < TestingSuperConstructorBase2
|
303
|
+
constructor :some, :accessors => true, :super => [], :strict => false
|
304
|
+
end
|
305
|
+
|
306
|
+
class TestingBlockedAccessors
|
307
|
+
constructor :foo, :bar, :accessors => false
|
308
|
+
def to_pretty_pretty
|
309
|
+
"#{@foo} #{@bar}"
|
310
|
+
end
|
311
|
+
end
|
312
|
+
|
313
|
+
class Papa
|
314
|
+
constructor :car, :saw
|
315
|
+
end
|
316
|
+
|
317
|
+
class Sonny < Papa
|
318
|
+
constructor :computer, :accessors => true
|
319
|
+
end
|
320
|
+
|
321
|
+
class Llamma
|
322
|
+
attr_accessor :hungry, :hair
|
323
|
+
constructor :hair
|
324
|
+
def setup
|
325
|
+
@hungry = true
|
326
|
+
end
|
327
|
+
end
|
328
|
+
|
329
|
+
class TestingBlockYield
|
330
|
+
constructor :a, :accessors => true do
|
331
|
+
@a = true
|
332
|
+
end
|
333
|
+
end
|
metadata
CHANGED
@@ -1,33 +1,36 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.0
|
3
|
-
specification_version: 1
|
4
2
|
name: constructor
|
5
3
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.0.
|
7
|
-
date: 2007-11-18 00:00:00 -05:00
|
8
|
-
summary: Declarative, named constructor arguments.
|
9
|
-
require_paths:
|
10
|
-
- lib
|
11
|
-
email: dev@atomicobject.com
|
12
|
-
homepage: http://rubyforge.org/projects/atomicobjectrb/
|
13
|
-
rubyforge_project: atomicobjectrb
|
14
|
-
description: "== DESCRIPTION: Declarative means to define object properties by passing a hash to the constructor, which will set the corresponding ivars."
|
15
|
-
autorequire:
|
16
|
-
default_executable:
|
17
|
-
bindir: bin
|
18
|
-
has_rdoc: true
|
19
|
-
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
-
requirements:
|
21
|
-
- - ">"
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 0.0.0
|
24
|
-
version:
|
4
|
+
version: 1.0.1
|
25
5
|
platform: ruby
|
26
|
-
signing_key:
|
27
|
-
cert_chain:
|
28
|
-
post_install_message:
|
29
6
|
authors:
|
30
7
|
- Atomic Object
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-05-08 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.5.1
|
23
|
+
version:
|
24
|
+
description: "== DESCRIPTION: Declarative means to define object properties by passing a hash to the constructor, which will set the corresponding ivars."
|
25
|
+
email: dev@atomicobject.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files:
|
31
|
+
- History.txt
|
32
|
+
- Manifest.txt
|
33
|
+
- README.txt
|
31
34
|
files:
|
32
35
|
- History.txt
|
33
36
|
- Manifest.txt
|
@@ -35,28 +38,32 @@ files:
|
|
35
38
|
- Rakefile
|
36
39
|
- lib/constructor.rb
|
37
40
|
- specs/constructor_spec.rb
|
38
|
-
|
39
|
-
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: http://rubyforge.org/projects/atomicobjectrb/
|
43
|
+
post_install_message:
|
40
44
|
rdoc_options:
|
41
45
|
- --main
|
42
46
|
- README.txt
|
43
|
-
|
44
|
-
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
51
61
|
requirements: []
|
52
62
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: 1.3.0
|
62
|
-
version:
|
63
|
+
rubyforge_project: atomicobjectrb
|
64
|
+
rubygems_version: 1.1.1
|
65
|
+
signing_key:
|
66
|
+
specification_version: 2
|
67
|
+
summary: Declarative, named constructor arguments.
|
68
|
+
test_files: []
|
69
|
+
|