doodle 0.0.4 → 0.0.5
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 +36 -7
- data/lib/doodle.rb +71 -42
- data/spec/arg_order_spec.rb +81 -98
- data/spec/attributes_spec.rb +70 -82
- data/spec/bugs_spec.rb +21 -0
- data/spec/class_spec.rb +63 -70
- data/spec/collector_spec.rb +97 -0
- data/spec/conversion_spec.rb +1 -3
- data/spec/defaults_spec.rb +104 -125
- data/spec/doodle_spec.rb +240 -227
- data/spec/flatten_first_level_spec.rb +4 -3
- data/spec/init_spec.rb +45 -49
- data/spec/required_spec.rb +9 -17
- data/spec/serialization_spec.rb +32 -0
- data/spec/superclass_spec.rb +17 -20
- data/spec/validation_spec.rb +79 -88
- metadata +5 -6
- data/examples/example-01.rdoc +0 -16
- data/examples/example-02.rdoc +0 -62
- data/examples/foo.rb +0 -10
- data/lib/spec_helper.rb +0 -19
data/spec/attributes_spec.rb
CHANGED
@@ -1,110 +1,98 @@
|
|
1
|
-
require '
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
2
|
|
3
3
|
describe Doodle::Attribute, 'basics' do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
include Doodle::Helper
|
13
|
-
has :name, :default => 'Hello'
|
14
|
-
class << self
|
15
|
-
has :metadata
|
4
|
+
temporary_constants :Foo, :Bar do
|
5
|
+
before(:each) do
|
6
|
+
class Foo
|
7
|
+
include Doodle::Helper
|
8
|
+
has :name, :default => 'Hello'
|
9
|
+
class << self
|
10
|
+
has :metadata
|
11
|
+
end
|
16
12
|
end
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
13
|
+
class Bar < Foo
|
14
|
+
has :info
|
15
|
+
class << self
|
16
|
+
has :doc
|
17
|
+
end
|
22
18
|
end
|
23
|
-
end
|
24
19
|
|
25
|
-
|
26
|
-
|
27
|
-
|
20
|
+
@foo = Foo.new
|
21
|
+
@bar = Bar.new :info => 'Hi'
|
22
|
+
end
|
28
23
|
|
29
|
-
|
30
|
-
|
31
|
-
|
24
|
+
it 'should have attribute :name with default defined' do
|
25
|
+
@foo.attributes[:name].default.should == 'Hello'
|
26
|
+
end
|
32
27
|
|
33
|
-
|
34
|
-
|
35
|
-
|
28
|
+
it 'should have default name' do
|
29
|
+
@foo.name.should == 'Hello'
|
30
|
+
end
|
36
31
|
|
37
|
-
|
38
|
-
|
39
|
-
|
32
|
+
it 'should not have an instance variable for a default' do
|
33
|
+
@foo.instance_variables.include?('@name').should == false
|
34
|
+
end
|
40
35
|
|
41
|
-
|
42
|
-
|
43
|
-
|
36
|
+
it 'should have name required == false (because has default)' do
|
37
|
+
@foo.attributes[:name].required?.should == false
|
38
|
+
end
|
44
39
|
|
45
|
-
|
46
|
-
|
47
|
-
|
40
|
+
it 'should have info required == true' do
|
41
|
+
@bar.attributes[:info].required?.should == true
|
42
|
+
end
|
48
43
|
|
49
|
-
|
50
|
-
|
51
|
-
|
44
|
+
it 'should have name.optional? == true (because has default)' do
|
45
|
+
@foo.attributes[:name].optional?.should == true
|
46
|
+
end
|
52
47
|
|
53
|
-
|
54
|
-
|
55
|
-
|
48
|
+
it 'should inherit attribute from parent' do
|
49
|
+
@bar.attributes[:name].should == @foo.attributes[:name]
|
50
|
+
end
|
56
51
|
|
57
|
-
|
58
|
-
|
59
|
-
|
52
|
+
it 'should have info.optional? == false' do
|
53
|
+
@bar.attributes[:info].optional?.should == false
|
54
|
+
end
|
60
55
|
|
61
|
-
|
62
|
-
|
63
|
-
|
56
|
+
it "should have parents in correct order" do
|
57
|
+
Bar.parents.should == [Foo, Object]
|
58
|
+
end
|
64
59
|
|
65
|
-
|
66
|
-
|
67
|
-
|
60
|
+
it "should have Bar's singleton parents in reverse order of definition" do
|
61
|
+
@bar.singleton_class.parents.should == [Bar.singleton_class.singleton_class, Bar.singleton_class, Foo.singleton_class]
|
62
|
+
end
|
68
63
|
|
69
|
-
|
70
|
-
|
71
|
-
|
64
|
+
it 'should have inherited singleton local_attributes in order of definition' do
|
65
|
+
@bar.singleton_class.class_eval { collect_inherited(:local_attributes).map { |x| x[0]} }.should == [:metadata, :doc]
|
66
|
+
end
|
72
67
|
|
73
|
-
|
74
|
-
|
68
|
+
it 'should have inherited singleton attributes in order of definition' do
|
69
|
+
@bar.singleton_class.attributes.keys.should == [:metadata, :doc]
|
70
|
+
end
|
75
71
|
end
|
76
72
|
end
|
77
73
|
|
78
74
|
describe Doodle::Attribute, 'attribute order' do
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
end
|
75
|
+
temporary_constants :A, :B, :C do
|
76
|
+
before :each do
|
77
|
+
class A < Doodle::Base
|
78
|
+
has :a
|
79
|
+
end
|
85
80
|
|
86
|
-
|
87
|
-
|
88
|
-
|
81
|
+
class B < A
|
82
|
+
has :b
|
83
|
+
end
|
89
84
|
|
90
|
-
|
91
|
-
|
85
|
+
class C < B
|
86
|
+
has :c
|
87
|
+
end
|
92
88
|
end
|
93
|
-
end
|
94
|
-
|
95
|
-
after :each do
|
96
|
-
undefine_const(:A)
|
97
|
-
undefine_const(:B)
|
98
|
-
undefine_const(:C)
|
99
|
-
end
|
100
89
|
|
101
|
-
|
102
|
-
|
103
|
-
|
90
|
+
it 'should keep order of inherited attributes' do
|
91
|
+
C.parents.should == [B, A, Doodle::Base, Object]
|
92
|
+
end
|
104
93
|
|
105
|
-
|
106
|
-
|
94
|
+
it 'should keep order of inherited attributes' do
|
95
|
+
C.attributes.keys.should == [:a, :b, :c]
|
96
|
+
end
|
107
97
|
end
|
108
98
|
end
|
109
|
-
|
110
|
-
raise_if_defined(:Foo, :Bar, :A, :B, :C)
|
data/spec/bugs_spec.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
describe 'Doodle', 'parents' do
|
4
|
+
temporary_constant :Foo do
|
5
|
+
before :each do
|
6
|
+
class Foo < Doodle::Base
|
7
|
+
has :var1, :kind => Integer
|
8
|
+
has :var2, :kind => Integer, :default => 1
|
9
|
+
must 'have var1 != var2' do
|
10
|
+
var1 != var2
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should not duplicate validations when accessing them!' do
|
16
|
+
foo = Foo 2
|
17
|
+
foo.validations.size.should == 1
|
18
|
+
foo.validations.size.should == 1
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/spec/class_spec.rb
CHANGED
@@ -1,90 +1,83 @@
|
|
1
|
-
|
2
|
-
require 'lib/spec_helper'
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
3
2
|
|
4
3
|
describe Doodle, 'class attributes' do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
include Doodle::Helper
|
13
|
-
class << self
|
14
|
-
has :metadata
|
4
|
+
temporary_constants :Bar, :Foo do
|
5
|
+
before(:each) do
|
6
|
+
class Foo
|
7
|
+
include Doodle::Helper
|
8
|
+
class << self
|
9
|
+
has :metadata
|
10
|
+
end
|
15
11
|
end
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
12
|
+
@foo = Foo.new
|
13
|
+
class Bar < Foo
|
14
|
+
include Doodle::Helper
|
15
|
+
class << self
|
16
|
+
has :doc
|
17
|
+
end
|
22
18
|
end
|
19
|
+
@foo = Foo.new
|
20
|
+
@bar = Bar.new
|
23
21
|
end
|
24
|
-
@foo = Foo.new
|
25
|
-
@bar = Bar.new
|
26
|
-
end
|
27
22
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
23
|
+
it 'should create class attribute' do
|
24
|
+
Foo.metadata = 'Foo metadata'
|
25
|
+
Foo.metadata.should == 'Foo metadata'
|
26
|
+
end
|
32
27
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
28
|
+
it 'should access class attribute via self.class' do
|
29
|
+
@foo.class.metadata = '@foo metadata'
|
30
|
+
@foo.class.metadata.should == '@foo metadata'
|
31
|
+
Foo.metadata.should == '@foo metadata'
|
37
32
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
33
|
+
Foo.metadata = 'Foo metadata'
|
34
|
+
Foo.metadata.should == 'Foo metadata'
|
35
|
+
@foo.class.metadata.should == 'Foo metadata'
|
36
|
+
end
|
42
37
|
|
43
|
-
|
44
|
-
|
45
|
-
|
38
|
+
it "should list all class's own attributes" do
|
39
|
+
Foo.meta.attributes(false).keys.should == [:metadata]
|
40
|
+
end
|
46
41
|
|
47
|
-
|
48
|
-
|
49
|
-
|
42
|
+
it "should list all class's own attributes" do
|
43
|
+
Foo.meta.attributes.keys.should == [:metadata]
|
44
|
+
end
|
50
45
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
46
|
+
it 'should create Bar class attribute' do
|
47
|
+
Bar.metadata = 'Bar metadata'
|
48
|
+
Bar.metadata.should == 'Bar metadata'
|
49
|
+
end
|
55
50
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
51
|
+
it 'should access class attribute via self.class' do
|
52
|
+
@bar.class.metadata = '@bar metadata'
|
53
|
+
@bar.class.metadata.should == '@bar metadata'
|
54
|
+
Bar.metadata.should == '@bar metadata'
|
60
55
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
56
|
+
Bar.metadata = 'Bar metadata'
|
57
|
+
Bar.metadata.should == 'Bar metadata'
|
58
|
+
@bar.class.metadata.should == 'Bar metadata'
|
59
|
+
end
|
65
60
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
61
|
+
it 'should not allow inherited class attributes to interfere with each other' do
|
62
|
+
Foo.metadata = 'Foo metadata'
|
63
|
+
@bar.class.metadata = '@bar metadata'
|
64
|
+
@bar.class.metadata.should == '@bar metadata'
|
65
|
+
Bar.metadata.should == '@bar metadata'
|
71
66
|
|
72
|
-
|
73
|
-
|
74
|
-
|
67
|
+
Bar.metadata = 'Bar metadata'
|
68
|
+
Bar.metadata.should == 'Bar metadata'
|
69
|
+
@bar.class.metadata.should == 'Bar metadata'
|
75
70
|
|
76
|
-
|
77
|
-
|
78
|
-
|
71
|
+
Foo.metadata.should == 'Foo metadata'
|
72
|
+
@foo.class.metadata.should == 'Foo metadata'
|
73
|
+
end
|
79
74
|
|
80
|
-
|
81
|
-
|
82
|
-
|
75
|
+
it "should list all class's own attributes" do
|
76
|
+
Bar.meta.attributes(false).keys.should == [:doc]
|
77
|
+
end
|
83
78
|
|
84
|
-
|
85
|
-
|
79
|
+
it "should list all class's own attributes" do
|
80
|
+
Bar.meta.attributes.keys.should == [:metadata, :doc]
|
81
|
+
end
|
86
82
|
end
|
87
|
-
|
88
83
|
end
|
89
|
-
|
90
|
-
raise_if_defined(:Foo, :Bar)
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
describe Doodle, "Simple collector" do
|
4
|
+
temporary_constant :Foo do
|
5
|
+
before :each do
|
6
|
+
class Foo < Doodle::Base
|
7
|
+
has :list, :init => [], :collect => :item
|
8
|
+
end
|
9
|
+
@foo = Foo do
|
10
|
+
item "Hello"
|
11
|
+
item "World"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
after :each do
|
15
|
+
remove_ivars :foo
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should define a collector method :item" do
|
19
|
+
@foo.methods.include?('item').should == true
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should collect items into attribute :list" do
|
23
|
+
@foo.list.should == ["Hello", "World"]
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe Doodle, "Typed collector with default collector name" do
|
30
|
+
temporary_constant :Event, :Location do
|
31
|
+
before :each do
|
32
|
+
class Location < Doodle::Base
|
33
|
+
has :name, :kind => String
|
34
|
+
end
|
35
|
+
class Event < Doodle::Base
|
36
|
+
has :locations, :init => [], :collect => Location
|
37
|
+
end
|
38
|
+
@event = Event do
|
39
|
+
location "Stage 1"
|
40
|
+
location "Stage 2"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
after :each do
|
44
|
+
remove_ivars :event
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should define a collector method :location" do
|
48
|
+
@event.methods.include?('location').should == true
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should collect items into attribute :list" do
|
52
|
+
@event.locations.map{|loc| loc.name}.should == ["Stage 1", "Stage 2"]
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe Doodle, "Typed collector with specified collector name" do
|
59
|
+
temporary_constant :Location, :Event do
|
60
|
+
before :each do
|
61
|
+
class Location < Doodle::Base
|
62
|
+
has :name, :kind => String
|
63
|
+
end
|
64
|
+
class Event < Doodle::Base
|
65
|
+
has :locations, :init => [], :collect => { :place => :Location }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
it "should define a collector method :place" do
|
69
|
+
Event.instance_methods.include?('place').should == true
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe Doodle, "Typed collector with specified collector name" do
|
75
|
+
temporary_constant :Location, :Event do
|
76
|
+
before :each do
|
77
|
+
class Location < Doodle::Base
|
78
|
+
has :name, :kind => String
|
79
|
+
end
|
80
|
+
class Event < Doodle::Base
|
81
|
+
has :locations, :init => [], :collect => { :place => Location }
|
82
|
+
end
|
83
|
+
end
|
84
|
+
it "should collect items into attribute :list" do
|
85
|
+
event = nil
|
86
|
+
proc {
|
87
|
+
event = Event do
|
88
|
+
place "Stage 1"
|
89
|
+
place "Stage 2"
|
90
|
+
end
|
91
|
+
}.should_not raise_error
|
92
|
+
event.locations.map{|loc| loc.name}.should == ["Stage 1", "Stage 2"]
|
93
|
+
event.locations.map{|loc| loc.class}.should == [Location, Location]
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|