yaml-model 1.3.1 → 1.3.2

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.
@@ -151,6 +151,10 @@ class YAML_Model
151
151
  @@database[ :data ][ self.class.name ].delete( self )
152
152
  end
153
153
 
154
+ def to_s
155
+ "#{self.class.name}[#{id}]"
156
+ end
157
+
154
158
  def self.sort_by( *attributes )
155
159
  define_method '<=>'.to_sym do |other|
156
160
  attributes.map{|a|self.send(a)} <=> attributes.map{|a|other.send(a)}
@@ -190,6 +194,11 @@ end" )
190
194
  via_instance.send( "#{that_attribute_singular}=".to_sym, that_instance )
191
195
  end
192
196
 
197
+ define_method "remove_#{that_attribute_singular}".to_sym do |that_instance|
198
+ via_instance = via_class.select{|n| n.send(this_attribute_singular).id == self.id && n.send(that_attribute_singular).id == that_instance.id }.first
199
+ via_instance.send( :delete )
200
+ end
201
+
193
202
  else
194
203
 
195
204
  define_method that_attribute_plural do
@@ -1,3 +1,3 @@
1
1
  class YAML_Model
2
- VERSION = "1.3.1"
2
+ VERSION = "1.3.2"
3
3
  end
@@ -0,0 +1,47 @@
1
+ require 'yaml-model'
2
+
3
+ describe YAML_Model, ".delete" do
4
+
5
+ DeleteModelInstance = Class.new( YAML_Model )
6
+
7
+ DeleteModelInstanceOne = Class.new( YAML_Model )
8
+ DeleteModelInstanceMany = Class.new( YAML_Model )
9
+
10
+ class DeleteModelInstanceOne < YAML_Model
11
+ has :many, DeleteModelInstanceMany
12
+ end
13
+
14
+ class DeleteModelInstanceMany < YAML_Model
15
+ type :one, DeleteModelInstanceOne
16
+ init :one
17
+ end
18
+
19
+ before( :each ) do
20
+ YAML_Model.reset!
21
+ end
22
+
23
+ it "Deletes model instances from self" do
24
+ a = DeleteModelInstance.create
25
+ b = DeleteModelInstance.create
26
+ c = DeleteModelInstance.create
27
+ DeleteModelInstance.all.should == [ a, b, c ]
28
+ b.delete
29
+ DeleteModelInstance.all.should == [ a, c ]
30
+ a.delete
31
+ DeleteModelInstance.all.should == [ c ]
32
+ c.delete
33
+ DeleteModelInstance.all.should == [ ]
34
+ end
35
+
36
+ it "Deletes model instances when referenced" do
37
+ one = DeleteModelInstanceOne.create
38
+ many_a = DeleteModelInstanceMany.create( one )
39
+ many_b = DeleteModelInstanceMany.create( one )
40
+
41
+ many_a.delete
42
+
43
+ DeleteModelInstanceMany.all.should == [ many_b ]
44
+ one.many.should == [ many_b ]
45
+ end
46
+
47
+ end
@@ -110,6 +110,41 @@ describe YAML_Model, "::has" do
110
110
  Tag.instance_methods.index( :add_post ).should_not == nil
111
111
  end
112
112
 
113
+ it "adds instances in many_to_many relationships" do
114
+ dummy_user = User.create
115
+ post = Post.create( dummy_user )
116
+ tag_a = Tag.create
117
+ tag_b = Tag.create
118
+ post.tags.should == []
119
+ post.add_tag( tag_b )
120
+ post.tags.should == [ tag_b ]
121
+ tag_b.posts.should == [ post ]
122
+ post.add_tag( tag_a )
123
+ post.tags.sort{|a,b|a.id<=>b.id}.should == [ tag_a, tag_b ]
124
+ tag_a.posts.should == [ post ]
125
+ end
126
+
127
+ it "adds a remove_ method when the relationship is many_to_many" do
128
+ Post.instance_methods.index( :remove_tag ).should_not == nil
129
+ Tag.instance_methods.index( :remove_post ).should_not == nil
130
+ end
131
+
132
+ it "removes instances in many_to_many relationships" do
133
+ dummy_user = User.create
134
+ post = Post.create( dummy_user )
135
+ tag_a = Tag.create
136
+ tag_b = Tag.create
137
+ post.add_tag( tag_a )
138
+ post.add_tag( tag_b )
139
+ post.tags.sort{|a,b|a.id<=>b.id}.should == [ tag_a, tag_b ]
140
+ post.remove_tag( tag_a )
141
+ post.tags.should == [ tag_b ]
142
+ tag_a.posts.should == []
143
+ post.remove_tag( tag_b )
144
+ post.tags.should == []
145
+ tag_b.posts.should == []
146
+ end
147
+
113
148
  it "handles many to many relationships seamlessly" do
114
149
  dummy_user = User.create
115
150
 
@@ -0,0 +1,16 @@
1
+ require 'yaml-model'
2
+
3
+ describe YAML_Model, ".to_s" do
4
+
5
+ AModel = Class.new( YAML_Model )
6
+
7
+ before( :each ) do
8
+ YAML_Model.reset!
9
+ end
10
+
11
+ it "Stringifies with the correct id" do
12
+ next_oid = YAML_Model.next_oid
13
+ AModel.create.to_s.should == "AModel[#{next_oid}]"
14
+ end
15
+
16
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 3
8
- - 1
9
- version: 1.3.1
8
+ - 2
9
+ version: 1.3.2
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-02-13 00:00:00 +02:00
17
+ date: 2011-02-16 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -78,8 +78,10 @@ files:
78
78
  - lib/yaml-model.rb
79
79
  - lib/yaml-model/version.rb
80
80
  - test/create.rb
81
+ - test/delete.rb
81
82
  - test/has.rb
82
83
  - test/init.rb
84
+ - test/to_s.rb
83
85
  - test/type.rb
84
86
  - yaml-model.gemspec
85
87
  has_rdoc: true
@@ -96,7 +98,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
96
98
  requirements:
97
99
  - - ">="
98
100
  - !ruby/object:Gem::Version
99
- hash: -582286063
101
+ hash: -230929969
100
102
  segments:
101
103
  - 0
102
104
  version: "0"
@@ -105,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
107
  requirements:
106
108
  - - ">="
107
109
  - !ruby/object:Gem::Version
108
- hash: -582286063
110
+ hash: -230929969
109
111
  segments:
110
112
  - 0
111
113
  version: "0"