tractor 0.2.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.1
@@ -81,14 +81,8 @@ module Tractor
81
81
 
82
82
  def save
83
83
  raise "Probably wanna set an id" if self.id.nil? || self.id.to_s.empty?
84
- key_base = "#{self.class}:#{self.id}"
85
- #raise "Duplicate value for #{self.class} 'id'" if Tractor.redis.keys("#{key_base}:*").any?
86
84
 
87
- scoped_attributes = attribute_store.inject({}) do |h, (attr_name, value)|
88
- h["#{key_base}:#{attr_name}"] = value
89
- h
90
- end
91
- Tractor.redis.mset scoped_attributes
85
+ Tractor.redis["#{self.class}:#{self.id}"] = Marshal.dump(self)
92
86
  Tractor.redis.sadd "#{self.class}:all", self.id
93
87
  add_to_indices
94
88
 
@@ -96,10 +90,9 @@ module Tractor
96
90
  end
97
91
 
98
92
  def destroy
99
- keys = Tractor.redis.keys("#{self.class}:#{self.id}:*")
100
- delete_from_indices(keys.map{|k| k.split(":").last })
93
+ delete_from_indices(attribute_store)
101
94
  Tractor.redis.srem("#{self.class}:all", self.id)
102
- keys.each { |k| Tractor.redis.del k }
95
+ Tractor.redis.del "#{self.class}:#{self.id}"
103
96
  end
104
97
 
105
98
  def update(attributes = {})
@@ -136,40 +129,40 @@ module Tractor
136
129
  attr_reader :attributes, :associations, :indices
137
130
 
138
131
  def create(attributes={})
132
+ raise "Duplicate value for #{self} 'id'" if Tractor.redis.sismember("#{self}:all", attributes[:id])
139
133
  m = new(attributes)
140
134
  m.save
141
135
  m
142
136
  end
137
+
138
+ def ids
139
+ Tractor.redis.smembers "#{self}:all"
140
+ end
141
+
142
+ def count
143
+ ids.size
144
+ end
143
145
 
144
146
  def find_by_id(id)
145
- keys = Tractor.redis.keys("#{self}:#{id}:*")
146
- return nil if keys.empty?
147
-
148
- scoped_attributes = Tractor.redis.mapped_mget(*keys)
149
- unscoped_attributes = scoped_attributes.inject({}) do |h, (key, value)|
150
-
151
- name = key.split(":").last
152
- type = attributes[name.to_sym][:type]
153
- if type == :integer
154
- value = value.to_i
155
- elsif type == :boolean
156
- value = value.to_s.match(/(true|1)$/i) != nil
157
- end
158
- h[name] = value
159
- h
160
- end
161
- self.new(unscoped_attributes)
147
+ redis_obj = Tractor.redis["#{self}:#{id}"]
148
+ return nil if redis_obj.nil?
149
+
150
+ Marshal.load(redis_obj)
162
151
  end
163
152
 
164
153
  # use method missing to do craziness, or define a find_by on each index (BETTER)
165
154
  def find_by_attribute(name, value)
166
155
  raise "No index on '#{name}'" unless indices.include?(name)
167
156
 
168
- ids = Tractor.redis.smembers(Index.key_for(self, name, value))
157
+ ids = ids_for_find(name, value)
169
158
  ids.map do |id|
170
159
  find_by_id(id)
171
160
  end
172
161
  end
162
+
163
+ def ids_for_find(name, value)
164
+ Tractor.redis.smembers(Index.key_for(self, name, value))
165
+ end
173
166
 
174
167
  def find(options = {})
175
168
  return [] if options.empty?
@@ -54,7 +54,7 @@ describe Tractor::Model::Base do
54
54
  end
55
55
  end
56
56
  end
57
-
57
+
58
58
  describe "#association" do
59
59
  attr_reader :game, :player1, :player2
60
60
 
@@ -143,9 +143,11 @@ describe Tractor::Model::Base do
143
143
  game = Game.new({:id => '1', :board => "large", :flying_object => "disc"})
144
144
  game.save
145
145
 
146
- redis["Game:1:id"].should == "1"
147
- redis["Game:1:board"].should == "large"
148
- redis["Game:1:flying_object"].should == "disc"
146
+ redis["Game:1"].should_not be_nil
147
+ redis_game = Marshal.load(redis["Game:1"])
148
+ redis_game.id.should == "1"
149
+ redis_game.board.should == "large"
150
+ redis_game.flying_object.should == "disc"
149
151
  end
150
152
 
151
153
  it "appends the new object to the Game set" do
@@ -156,7 +158,7 @@ describe Tractor::Model::Base do
156
158
  Game.all.size.should == 1
157
159
  end
158
160
  end
159
-
161
+
160
162
  describe ".all" do
161
163
  it "every object that is created for this class will be in this set" do
162
164
  MonkeyClient.all.size.should == 0
@@ -182,26 +184,66 @@ describe Tractor::Model::Base do
182
184
  end
183
185
  end
184
186
 
187
+ describe ".ids" do
188
+ before do
189
+ Sammich.create({ :id => 's1', :weight => "medium", :product => "Turkey Avocado" })
190
+ Sammich.create({ :id => 's2', :weight => "medium", :product => "Reuben Sammich" })
191
+ Player.create({ :id => 'p1', :name => "delicious" })
192
+ end
193
+
194
+ it "returns all the ids for a given class" do
195
+ Sammich.ids.should == ['s1', 's2']
196
+ Player.ids.should == ['p1']
197
+ end
198
+ end
199
+
200
+ describe ".count" do
201
+ before do
202
+ Sammich.create({ :id => 's1', :weight => "medium", :product => "Turkey Avocado" })
203
+ Sammich.create({ :id => 's2', :weight => "medium", :product => "Reuben Sammich" })
204
+ Player.create({ :id => 'p1', :name => "delicious" })
205
+ end
206
+
207
+ it "returns the count of all items of a given class" do
208
+ Sammich.count.should == 2
209
+ Player.count.should == 1
210
+ end
211
+ end
212
+
213
+ describe ".ids_for_find" do
214
+ before do
215
+ Sammich.create({ :id => 's1', :weight => "medium", :product => "Turkey Avocado" })
216
+ Sammich.create({ :id => 's2', :weight => "medium", :product => "Reuben Sammich" })
217
+ Player.create({ :id => 'p1', :name => "delicious" })
218
+ end
219
+
220
+ it "returns all the ids for a given attribute and value on a class" do
221
+ Sammich.ids_for_find(:weight, "medium").should == ['s1', 's2']
222
+ end
223
+ end
224
+
185
225
  describe "#create" do
186
226
  it "allows you to specify which attributes should be unique"
187
- # it "raises exception if the id exists" do
188
- # MonkeyClient.create({ :id => 'a1a', :evil => true, :birthday => "Dec 3" })
189
- # lambda do
190
- # MonkeyClient.create({ :id => 'a1a', :evil => false, :birthday => "Jan 4" })
191
- # end.should raise_error("Duplicate value for MonkeyClient 'id'")
192
- # end
227
+
228
+ it "raises exception if the id exists" do
229
+ MonkeyClient.create({ :id => 'a1a', :evil => true, :birthday => "Dec 3" })
230
+ lambda do
231
+ MonkeyClient.create({ :id => 'a1a', :evil => false, :birthday => "Jan 4" })
232
+ end.should raise_error("Duplicate value for MonkeyClient 'id'")
233
+ end
193
234
 
194
235
  it "should write attributes to redis" do
195
236
  sammich = Sammich.create({ :id => '1', :product => "Veggie Sammich" })
196
237
 
197
- redis["Sammich:1:id"].should == "1"
198
- redis["Sammich:1:product"].should == "Veggie Sammich"
238
+ redis_sammich = Marshal.load(redis["Sammich:1"])
239
+ redis_sammich.id.should == "1"
240
+ redis_sammich.product.should == "Veggie Sammich"
199
241
  end
200
242
 
201
243
  it "populates all the indices that are specified on the class" do
202
244
  Sammich.create({ :id => '1', :weight => "heavy", :product => "Ham Sammich" })
203
245
  Sammich.create({ :id => '2', :weight => "heavy", :product => "Tuna Sammich" })
204
-
246
+
205
247
  redis.smembers("Sammich:product:SGFtIFNhbW1pY2g=").should include('1')
206
248
  redis.smembers("Sammich:product:VHVuYSBTYW1taWNo").should include('2')
207
249
  redis.smembers("Sammich:weight:aGVhdnk=").should == ['1', '2']
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.2.0"
8
+ s.version = "0.3.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-02-12}
12
+ s.date = %q{2010-02-24}
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.2.0
4
+ version: 0.3.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-02-12 00:00:00 -08:00
12
+ date: 2010-02-24 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15