tractor 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/tractor/model/base.rb +13 -12
- data/lib/tractor/model/mapper.rb +1 -1
- data/spec/model/base_spec.rb +20 -8
- data/tractor.gemspec +2 -2
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.2
|
data/lib/tractor/model/base.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'base64'
|
2
|
+
require 'yajl'
|
2
3
|
|
3
4
|
module Tractor
|
4
5
|
|
@@ -7,11 +8,7 @@ module Tractor
|
|
7
8
|
|
8
9
|
# important options are port, host and db
|
9
10
|
def connectdb(options={})
|
10
|
-
|
11
|
-
@redis = Redis.new(options)
|
12
|
-
else
|
13
|
-
@redis = Redis.new(:db => 1)
|
14
|
-
end
|
11
|
+
@redis = options.nil? ? Redis.new(:db => 1) : Redis.new(options)
|
15
12
|
end
|
16
13
|
|
17
14
|
def flushdb
|
@@ -54,7 +51,7 @@ module Tractor
|
|
54
51
|
end
|
55
52
|
|
56
53
|
def insert(id)
|
57
|
-
Tractor.redis.sadd(key, id) unless Tractor.redis.
|
54
|
+
Tractor.redis.sadd(key, id) unless Tractor.redis.sismember(key, id)
|
58
55
|
end
|
59
56
|
|
60
57
|
def delete(id)
|
@@ -77,6 +74,7 @@ module Tractor
|
|
77
74
|
def initialize(attributes={})
|
78
75
|
@attribute_store = {}
|
79
76
|
@association_store = {}
|
77
|
+
@encoder = Yajl::Encoder.new
|
80
78
|
attributes.each do |k,v|
|
81
79
|
send("#{k}=", v)
|
82
80
|
end
|
@@ -84,8 +82,7 @@ module Tractor
|
|
84
82
|
|
85
83
|
def save
|
86
84
|
raise "Probably wanna set an id" if self.id.nil? || self.id.to_s.empty?
|
87
|
-
|
88
|
-
Tractor.redis["#{self.class}:#{self.id}"] = Marshal.dump(self)
|
85
|
+
Tractor.redis["#{self.class}:#{self.id}"] = @encoder.encode(self.send(:attribute_store))
|
89
86
|
Tractor.redis.sadd "#{self.class}:all", self.id
|
90
87
|
add_to_indices
|
91
88
|
add_to_associations
|
@@ -151,12 +148,16 @@ module Tractor
|
|
151
148
|
m.save
|
152
149
|
m
|
153
150
|
end
|
151
|
+
|
152
|
+
def exists?(id)
|
153
|
+
Tractor.redis.sismember("#{self}:all", id)
|
154
|
+
end
|
154
155
|
|
155
156
|
def find_by_id(id)
|
156
|
-
|
157
|
-
return nil if
|
158
|
-
|
159
|
-
|
157
|
+
obj_data = Tractor.redis["#{self}:#{id}"]
|
158
|
+
return nil if obj_data.nil?
|
159
|
+
parser = Yajl::Parser.new
|
160
|
+
new(parser.parse(obj_data))
|
160
161
|
end
|
161
162
|
|
162
163
|
# use method missing to do craziness, or define a find_by on each index (BETTER)
|
data/lib/tractor/model/mapper.rb
CHANGED
data/spec/model/base_spec.rb
CHANGED
@@ -163,10 +163,11 @@ describe Tractor::Model::Base do
|
|
163
163
|
game.save
|
164
164
|
|
165
165
|
redis["Tractor::Model::Game:1"].should_not be_nil
|
166
|
-
|
167
|
-
redis_game
|
168
|
-
redis_game.
|
169
|
-
redis_game.
|
166
|
+
parser = Yajl::Parser.new
|
167
|
+
redis_game = parser.parse(redis["Tractor::Model::Game:1"])
|
168
|
+
redis_game["id"].should == "1"
|
169
|
+
redis_game["board"].should == "large"
|
170
|
+
redis_game["flying_object"].should == "disc"
|
170
171
|
end
|
171
172
|
|
172
173
|
it "appends the new object to the Game set" do
|
@@ -237,10 +238,10 @@ describe Tractor::Model::Base do
|
|
237
238
|
|
238
239
|
it "should write attributes to redis" do
|
239
240
|
sammich = Sammich.create({ :id => '1', :product => "Veggie Sammich" })
|
240
|
-
|
241
|
-
redis_sammich =
|
242
|
-
redis_sammich
|
243
|
-
redis_sammich
|
241
|
+
parser = Yajl::Parser.new
|
242
|
+
redis_sammich = parser.parse(redis["Sammich:1"])
|
243
|
+
redis_sammich["id"].should == "1"
|
244
|
+
redis_sammich["product"].should == "Veggie Sammich"
|
244
245
|
end
|
245
246
|
|
246
247
|
it "populates all the indices that are specified on the class" do
|
@@ -258,6 +259,17 @@ describe Tractor::Model::Base do
|
|
258
259
|
end
|
259
260
|
end
|
260
261
|
|
262
|
+
describe ".exists?" do
|
263
|
+
it "returns true if the object with the given id exists" do
|
264
|
+
MonkeyClient.create({ :id => 'a1a', :evil => true, :birthday => "Dec 3" })
|
265
|
+
MonkeyClient.exists?('a1a').should be_true
|
266
|
+
end
|
267
|
+
|
268
|
+
it "returns false if the object with the given id does not exist" do
|
269
|
+
MonkeyClient.exists?('a1a').should be_false
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
261
273
|
describe ".find_by_id" do
|
262
274
|
it "takes an id and returns the object from redis" do
|
263
275
|
sammich = Sammich.create({ :id => '1', :product => "Cold Cut Trio" })
|
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.2"
|
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-
|
12
|
+
s.date = %q{2010-06-09}
|
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.2
|
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-
|
12
|
+
date: 2010-06-09 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|