tractor 0.4.0 → 0.4.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/.gitignore +2 -0
- data/VERSION +1 -1
- data/lib/tractor/model/base.rb +21 -17
- data/spec/model/base_spec.rb +5 -18
- data/tractor.gemspec +2 -2
- metadata +2 -2
data/.gitignore
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.1
|
data/lib/tractor/model/base.rb
CHANGED
@@ -107,23 +107,18 @@ module Tractor
|
|
107
107
|
save
|
108
108
|
end
|
109
109
|
|
110
|
-
def
|
111
|
-
|
112
|
-
|
113
|
-
return unless foreign_key_value
|
114
|
-
value[:foreign_klass].find_by_id(foreign_key_value).send(key).delete(self.id)
|
110
|
+
def add_to_associations
|
111
|
+
for_each_associated_foreign_instance do |foreign_instance|
|
112
|
+
foreign_instance.push(self)
|
115
113
|
end
|
116
114
|
end
|
117
|
-
|
118
|
-
def
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
return unless foreign_key_value
|
123
|
-
value[:foreign_klass].find_by_id(foreign_key_value).send(key).push(self)
|
115
|
+
|
116
|
+
def remove_from_associations
|
117
|
+
for_each_associated_foreign_instance do |foreign_instance|
|
118
|
+
foreign_instance.delete(self.id)
|
124
119
|
end
|
125
120
|
end
|
126
|
-
|
121
|
+
|
127
122
|
def add_to_indices
|
128
123
|
self.class.indices.each do |name|
|
129
124
|
index = Index.new(self.class, name, send(name))
|
@@ -201,9 +196,8 @@ module Tractor
|
|
201
196
|
|
202
197
|
# make an assumption about the foreign_key... probably bad :)
|
203
198
|
def association(name, klass)
|
204
|
-
|
205
|
-
klass.associations[name] = {:
|
206
|
-
|
199
|
+
foreign_key_name = "#{self.to_s.gsub(/^.*::/, '').downcase}_id"
|
200
|
+
klass.associations[self.name] = {:foreign_key_name => foreign_key_name, :set_name => name}
|
207
201
|
define_method(name) do
|
208
202
|
@association_store[name] = Association.new("#{self.class}:#{self.id}:#{name}", klass)
|
209
203
|
end
|
@@ -263,6 +257,16 @@ module Tractor
|
|
263
257
|
private
|
264
258
|
|
265
259
|
attr_reader :attribute_store, :association_store
|
260
|
+
|
261
|
+
def for_each_associated_foreign_instance
|
262
|
+
self.class.associations.each do |association_owner_class_name, association_attributes|
|
263
|
+
foreign_klass = Object.module_eval(association_owner_class_name)
|
264
|
+
foreign_key = self.send(association_attributes[:foreign_key_name])
|
265
|
+
foreign_instance = foreign_klass.find_by_id(foreign_key)
|
266
|
+
yield(foreign_instance.send(association_attributes[:set_name]))
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
266
270
|
end
|
267
271
|
end
|
268
|
-
end
|
272
|
+
end
|
data/spec/model/base_spec.rb
CHANGED
@@ -81,8 +81,9 @@ describe Tractor::Model::Base do
|
|
81
81
|
|
82
82
|
before do
|
83
83
|
@game = Tractor::Model::Game.new({ :id => 'g1' })
|
84
|
-
|
85
|
-
@
|
84
|
+
Tractor::Model::Game.create({ :id => 'g2' })
|
85
|
+
@player1 = Tractor::Model::Player.new({ :id => 'p1', :name => "delicious", :game_id => "g1" })
|
86
|
+
@player2 = Tractor::Model::Player.new({ :id => 'p2', :name => "gross", :game_id => "g2" })
|
86
87
|
|
87
88
|
game.save
|
88
89
|
player1.save
|
@@ -94,13 +95,11 @@ describe Tractor::Model::Base do
|
|
94
95
|
end
|
95
96
|
|
96
97
|
it "adds a push method for the set on an instance of the class" do
|
97
|
-
game.players.push
|
98
|
-
redis.smembers('Tractor::Model::Game:g1:players').should == ['p1']
|
98
|
+
game.players.push player2
|
99
|
+
redis.smembers('Tractor::Model::Game:g1:players').should == ['p1', 'p2']
|
99
100
|
end
|
100
101
|
|
101
102
|
it "adds an ids method for the set that returns all ids in it" do
|
102
|
-
game.players.ids.should be_empty
|
103
|
-
game.players.push player1
|
104
103
|
game.players.ids.should == [player1.id]
|
105
104
|
end
|
106
105
|
|
@@ -111,17 +110,9 @@ describe Tractor::Model::Base do
|
|
111
110
|
end
|
112
111
|
|
113
112
|
it "adds an all method for the association to return the items in it" do
|
114
|
-
game.players.all.should == []
|
115
|
-
game.players.push player1
|
116
|
-
game.players.push player2
|
117
|
-
|
118
113
|
player1_from_game = game.players.all[0]
|
119
|
-
player2_from_game = game.players.all[1]
|
120
|
-
|
121
114
|
player1_from_game.name.should == player1.name
|
122
115
|
player1_from_game.id.should == player1.id
|
123
|
-
player2_from_game.name.should == player2.name
|
124
|
-
player2_from_game.id.should == player2.id
|
125
116
|
end
|
126
117
|
|
127
118
|
it "requires the object being added to have been saved to the database before adding it to the association"
|
@@ -216,12 +207,10 @@ describe Tractor::Model::Base do
|
|
216
207
|
before do
|
217
208
|
Sammich.create({ :id => 's1', :weight => "medium", :product => "Turkey Avocado" })
|
218
209
|
Sammich.create({ :id => 's2', :weight => "medium", :product => "Reuben Sammich" })
|
219
|
-
Tractor::Model::Player.create({ :id => 'p1', :name => "delicious" })
|
220
210
|
end
|
221
211
|
|
222
212
|
it "returns all the ids for a given class" do
|
223
213
|
Sammich.ids.should == ['s1', 's2']
|
224
|
-
Tractor::Model::Player.ids.should == ['p1']
|
225
214
|
end
|
226
215
|
end
|
227
216
|
|
@@ -229,12 +218,10 @@ describe Tractor::Model::Base do
|
|
229
218
|
before do
|
230
219
|
Sammich.create({ :id => 's1', :weight => "medium", :product => "Turkey Avocado" })
|
231
220
|
Sammich.create({ :id => 's2', :weight => "medium", :product => "Reuben Sammich" })
|
232
|
-
Tractor::Model::Player.create({ :id => 'p1', :name => "delicious" })
|
233
221
|
end
|
234
222
|
|
235
223
|
it "returns the count of all items of a given class" do
|
236
224
|
Sammich.count.should == 2
|
237
|
-
Tractor::Model::Player.count.should == 1
|
238
225
|
end
|
239
226
|
end
|
240
227
|
|
data/tractor.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{tractor}
|
8
|
-
s.version = "0.4.
|
8
|
+
s.version = "0.4.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Shane Wolf"]
|
12
|
-
s.date = %q{2010-05-
|
12
|
+
s.date = %q{2010-05-26}
|
13
13
|
s.description = %q{Very simple object mappings for ruby objects}
|
14
14
|
s.email = %q{shanewolf@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tractor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shane Wolf
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-05-
|
12
|
+
date: 2010-05-26 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|