yaml-model 1.2.1 → 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/yaml-model.rb +15 -4
- data/lib/yaml-model/version.rb +1 -1
- data/test/create.rb +6 -0
- data/test/has.rb +66 -2
- metadata +5 -5
data/lib/yaml-model.rb
CHANGED
@@ -22,6 +22,10 @@ class YAML_Model
|
|
22
22
|
}
|
23
23
|
end
|
24
24
|
|
25
|
+
def self.next_oid
|
26
|
+
@@database[ :next_oid ]
|
27
|
+
end
|
28
|
+
|
25
29
|
reset!
|
26
30
|
|
27
31
|
attr_reader :id
|
@@ -155,8 +159,11 @@ class YAML_Model
|
|
155
159
|
|
156
160
|
sort_by :id
|
157
161
|
|
158
|
-
def self.has( that_attribute_plural, that_class,
|
159
|
-
|
162
|
+
def self.has( that_attribute_plural, that_class, opts = {} )
|
163
|
+
options = {
|
164
|
+
:many_to_many => false
|
165
|
+
}.merge( opts )
|
166
|
+
if options[ :many_to_many ]
|
160
167
|
this_class = self
|
161
168
|
this_class_name, that_class_name = [this_class,that_class].map{|n|n.name.split(':')[-1]}
|
162
169
|
this_attribute_singular = this_class_name.downcase.to_sym
|
@@ -187,8 +194,12 @@ end" )
|
|
187
194
|
|
188
195
|
define_method that_attribute_plural do
|
189
196
|
that_class.select do |that_instance|
|
190
|
-
|
191
|
-
|
197
|
+
if options[ that_class ]
|
198
|
+
that_instance.instance_eval( options[ that_class ].to_s ).id == self.id
|
199
|
+
else
|
200
|
+
that_instance.instance_variables.inject( false ) do |result,variable|
|
201
|
+
result ||= that_instance.instance_eval(variable.to_s).class == self.class && that_instance.instance_eval(variable.to_s).id == self.id
|
202
|
+
end
|
192
203
|
end
|
193
204
|
end
|
194
205
|
end
|
data/lib/yaml-model/version.rb
CHANGED
data/test/create.rb
CHANGED
@@ -28,5 +28,11 @@ describe YAML_Model, "::create" do
|
|
28
28
|
lambda{ Person.create( "Bob", "Smith" ) }.should raise_error( ArgumentError )
|
29
29
|
end
|
30
30
|
|
31
|
+
it "increments the next_oid" do
|
32
|
+
next_oid = YAML_Model.next_oid
|
33
|
+
Person.create( "Bob" )
|
34
|
+
( next_oid + 1 ).should == YAML_Model.next_oid
|
35
|
+
end
|
36
|
+
|
31
37
|
end
|
32
38
|
|
data/test/has.rb
CHANGED
@@ -5,11 +5,13 @@ describe YAML_Model, "::has" do
|
|
5
5
|
User = Class.new( YAML_Model )
|
6
6
|
Post = Class.new( YAML_Model )
|
7
7
|
Tag = Class.new( YAML_Model )
|
8
|
+
MultiUserUser = Class.new( YAML_Model )
|
9
|
+
MultiUserPost = Class.new( YAML_Model )
|
8
10
|
|
9
11
|
class Post < YAML_Model
|
10
12
|
type :user, User
|
11
13
|
init :user
|
12
|
-
has :tags, Tag, :many_to_many
|
14
|
+
has :tags, Tag, :many_to_many => true
|
13
15
|
end
|
14
16
|
|
15
17
|
class User < YAML_Model
|
@@ -17,7 +19,19 @@ describe YAML_Model, "::has" do
|
|
17
19
|
end
|
18
20
|
|
19
21
|
class Tag < YAML_Model
|
20
|
-
has :posts, Post, :many_to_many
|
22
|
+
has :posts, Post, :many_to_many => true
|
23
|
+
end
|
24
|
+
|
25
|
+
class MultiUserUser < YAML_Model
|
26
|
+
has :posts_confirmed, MultiUserPost, MultiUserPost => :confirmed_by
|
27
|
+
has :posts_created, MultiUserPost, MultiUserPost => :created_by
|
28
|
+
has :posts, MultiUserPost
|
29
|
+
end
|
30
|
+
|
31
|
+
class MultiUserPost < YAML_Model
|
32
|
+
type :created_by, MultiUserUser
|
33
|
+
type :confirmed_by, MultiUserUser
|
34
|
+
init :created_by, :confirmed_by
|
21
35
|
end
|
22
36
|
|
23
37
|
before( :each ) do
|
@@ -28,6 +42,56 @@ describe YAML_Model, "::has" do
|
|
28
42
|
User.instance_methods.index( :posts ).should_not == nil
|
29
43
|
end
|
30
44
|
|
45
|
+
it "caters for multiple links to a single model" do
|
46
|
+
creator_1 = MultiUserUser.create
|
47
|
+
creator_2 = MultiUserUser.create
|
48
|
+
confirmer_1 = MultiUserUser.create
|
49
|
+
confirmer_2 = MultiUserUser.create
|
50
|
+
|
51
|
+
post_1 = MultiUserPost.create( creator_1, confirmer_1 )
|
52
|
+
post_2 = MultiUserPost.create( creator_1, confirmer_2 )
|
53
|
+
post_3 = MultiUserPost.create( creator_2, confirmer_1 )
|
54
|
+
post_4 = MultiUserPost.create( creator_1, creator_2 )
|
55
|
+
|
56
|
+
post_1.created_by.id.should == creator_1.id
|
57
|
+
post_2.created_by.id.should == creator_1.id
|
58
|
+
post_3.created_by.id.should == creator_2.id
|
59
|
+
post_4.created_by.id.should == creator_1.id
|
60
|
+
|
61
|
+
post_1.confirmed_by.id.should == confirmer_1.id
|
62
|
+
post_2.confirmed_by.id.should == confirmer_2.id
|
63
|
+
post_3.confirmed_by.id.should == confirmer_1.id
|
64
|
+
post_4.confirmed_by.id.should == creator_2.id
|
65
|
+
|
66
|
+
creator_1.posts_created.size.should == 3
|
67
|
+
creator_1.posts_created.map{|n|n.id}.sort.should == [ post_1.id, post_2.id, post_4.id ]
|
68
|
+
creator_1.posts_confirmed.size.should == 0
|
69
|
+
creator_1.posts_confirmed.map{|n|n.id}.sort.should == []
|
70
|
+
creator_1.posts.size.should == 3
|
71
|
+
creator_1.posts.map{|n|n.id}.sort.should == [ post_1.id, post_2.id, post_4.id ]
|
72
|
+
|
73
|
+
creator_2.posts_created.size.should == 1
|
74
|
+
creator_2.posts_created.map{|n|n.id}.sort.should == [ post_3.id ]
|
75
|
+
creator_2.posts_confirmed.size.should == 1
|
76
|
+
creator_2.posts_confirmed.map{|n|n.id}.sort.should == [ post_4.id ]
|
77
|
+
creator_2.posts.size.should == 2
|
78
|
+
creator_2.posts.map{|n|n.id}.sort.should == [ post_3.id, post_4.id ]
|
79
|
+
|
80
|
+
confirmer_1.posts_created.size.should == 0
|
81
|
+
confirmer_1.posts_created.map{|n|n.id}.sort.should == []
|
82
|
+
confirmer_1.posts_confirmed.size.should == 2
|
83
|
+
confirmer_1.posts_confirmed.map{|n|n.id}.sort.should == [ post_1.id, post_3.id ]
|
84
|
+
confirmer_1.posts.size.should == 2
|
85
|
+
confirmer_1.posts.map{|n|n.id}.sort.should == [ post_1.id, post_3.id ]
|
86
|
+
|
87
|
+
confirmer_2.posts_created.size.should == 0
|
88
|
+
confirmer_2.posts_created.map{|n|n.id}.sort.should == []
|
89
|
+
confirmer_2.posts_confirmed.size.should == 1
|
90
|
+
confirmer_2.posts_confirmed.map{|n|n.id}.sort.should == [ post_2.id]
|
91
|
+
confirmer_2.posts.size.should == 1
|
92
|
+
confirmer_2.posts.map{|n|n.id}.sort.should == [ post_2.id ]
|
93
|
+
end
|
94
|
+
|
31
95
|
it "correctly references items that belong to it" do
|
32
96
|
user_a = User.create
|
33
97
|
user_b = User.create
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 1
|
7
|
-
-
|
7
|
+
- 3
|
8
8
|
- 1
|
9
|
-
version: 1.
|
9
|
+
version: 1.3.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Clive Crous
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-
|
17
|
+
date: 2011-02-13 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -96,7 +96,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
96
96
|
requirements:
|
97
97
|
- - ">="
|
98
98
|
- !ruby/object:Gem::Version
|
99
|
-
hash:
|
99
|
+
hash: -582286063
|
100
100
|
segments:
|
101
101
|
- 0
|
102
102
|
version: "0"
|
@@ -105,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
105
105
|
requirements:
|
106
106
|
- - ">="
|
107
107
|
- !ruby/object:Gem::Version
|
108
|
-
hash:
|
108
|
+
hash: -582286063
|
109
109
|
segments:
|
110
110
|
- 0
|
111
111
|
version: "0"
|