tractor 0.4.4 → 0.4.5
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/VERSION +1 -1
- data/lib/tractor/model/base.rb +10 -6
- data/lib/tractor/model/mapper.rb +5 -1
- data/spec/model/base_spec.rb +4 -4
- data/spec/model/mapper_spec.rb +16 -0
- data/tractor.gemspec +4 -4
- metadata +37 -13
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.4.
|
|
1
|
+
0.4.5
|
data/lib/tractor/model/base.rb
CHANGED
|
@@ -3,6 +3,10 @@ require 'yajl'
|
|
|
3
3
|
|
|
4
4
|
module Tractor
|
|
5
5
|
|
|
6
|
+
class MissingIdError < StandardError; end
|
|
7
|
+
class DuplicateKeyError < StandardError; end
|
|
8
|
+
class MissingIndexError < StandardError; end
|
|
9
|
+
|
|
6
10
|
class << self
|
|
7
11
|
attr_reader :redis
|
|
8
12
|
|
|
@@ -37,7 +41,7 @@ module Tractor
|
|
|
37
41
|
end
|
|
38
42
|
|
|
39
43
|
def all
|
|
40
|
-
ids.inject([]){
|
|
44
|
+
ids.inject([]){|o, id| o << klass.find_by_id(id); o }
|
|
41
45
|
end
|
|
42
46
|
end
|
|
43
47
|
|
|
@@ -81,7 +85,7 @@ module Tractor
|
|
|
81
85
|
end
|
|
82
86
|
|
|
83
87
|
def save
|
|
84
|
-
raise "Probably wanna set an id" if self.id.nil? || self.id.to_s.empty?
|
|
88
|
+
raise MissingIdError, "Probably wanna set an id" if self.id.nil? || self.id.to_s.empty?
|
|
85
89
|
Tractor.redis["#{self.class}:#{self.id}"] = @encoder.encode(self.send(:attribute_store))
|
|
86
90
|
Tractor.redis.sadd "#{self.class}:all", self.id
|
|
87
91
|
add_to_indices
|
|
@@ -100,7 +104,7 @@ module Tractor
|
|
|
100
104
|
def update(attributes = {})
|
|
101
105
|
attributes.delete(:id)
|
|
102
106
|
delete_from_indices(attributes)
|
|
103
|
-
attributes.each{
|
|
107
|
+
attributes.each{|k,v| self.send("#{k}=", v) }
|
|
104
108
|
save
|
|
105
109
|
end
|
|
106
110
|
|
|
@@ -143,7 +147,7 @@ module Tractor
|
|
|
143
147
|
attr_reader :attributes, :associations, :indices
|
|
144
148
|
|
|
145
149
|
def create(attributes={})
|
|
146
|
-
raise "Duplicate value for #{self} 'id'" if Tractor.redis.sismember("#{self}:all", attributes[:id])
|
|
150
|
+
raise DuplicateKeyError, "Duplicate value for #{self} 'id'" if Tractor.redis.sismember("#{self}:all", attributes[:id])
|
|
147
151
|
m = new(attributes)
|
|
148
152
|
m.save
|
|
149
153
|
m
|
|
@@ -162,7 +166,7 @@ module Tractor
|
|
|
162
166
|
|
|
163
167
|
# use method missing to do craziness, or define a find_by on each index (BETTER)
|
|
164
168
|
def find_by_attribute(name, value)
|
|
165
|
-
raise "No index on '#{name}'" unless indices.include?(name)
|
|
169
|
+
raise MissingIndexError, "No index on '#{name}'" unless indices.include?(name)
|
|
166
170
|
find({name => value})
|
|
167
171
|
end
|
|
168
172
|
|
|
@@ -213,7 +217,7 @@ module Tractor
|
|
|
213
217
|
end
|
|
214
218
|
|
|
215
219
|
def all
|
|
216
|
-
ids.inject([]){
|
|
220
|
+
ids.inject([]){|a, id| a << find_by_id(id); a }
|
|
217
221
|
end
|
|
218
222
|
|
|
219
223
|
###
|
data/lib/tractor/model/mapper.rb
CHANGED
|
@@ -25,7 +25,11 @@ module Tractor
|
|
|
25
25
|
|
|
26
26
|
def create_from_instance(server_instance)
|
|
27
27
|
hydrate_attributes(server_instance) do |attributes|
|
|
28
|
-
|
|
28
|
+
begin
|
|
29
|
+
return self.create(attributes)
|
|
30
|
+
rescue DuplicateKeyError
|
|
31
|
+
return find_from_instance(server_instance)
|
|
32
|
+
end
|
|
29
33
|
end
|
|
30
34
|
end
|
|
31
35
|
|
data/spec/model/base_spec.rb
CHANGED
|
@@ -153,9 +153,9 @@ describe Tractor::Model::Base do
|
|
|
153
153
|
it "raises if id is nil or empty" do
|
|
154
154
|
game = Tractor::Model::Game.new
|
|
155
155
|
game.id = nil
|
|
156
|
-
lambda { game.save }.should raise_error("Probably wanna set an id")
|
|
156
|
+
lambda { game.save }.should raise_error(Tractor::MissingIdError, "Probably wanna set an id")
|
|
157
157
|
game.id = ''
|
|
158
|
-
lambda { game.save }.should raise_error("Probably wanna set an id")
|
|
158
|
+
lambda { game.save }.should raise_error(Tractor::MissingIdError, "Probably wanna set an id")
|
|
159
159
|
end
|
|
160
160
|
|
|
161
161
|
it "should write attributes to redis" do
|
|
@@ -233,7 +233,7 @@ describe Tractor::Model::Base do
|
|
|
233
233
|
MonkeyClient.create({ :id => 'a1a', :evil => true, :birthday => "Dec 3" })
|
|
234
234
|
lambda do
|
|
235
235
|
MonkeyClient.create({ :id => 'a1a', :evil => false, :birthday => "Jan 4" })
|
|
236
|
-
end.should raise_error("Duplicate value for MonkeyClient 'id'")
|
|
236
|
+
end.should raise_error(Tractor::DuplicateKeyError, "Duplicate value for MonkeyClient 'id'")
|
|
237
237
|
end
|
|
238
238
|
|
|
239
239
|
it "should write attributes to redis" do
|
|
@@ -399,7 +399,7 @@ describe Tractor::Model::Base do
|
|
|
399
399
|
it "raises if index does not exist for given key" do
|
|
400
400
|
lambda do
|
|
401
401
|
Sammich.find_by_attribute(:expensive, true)
|
|
402
|
-
end.should raise_error("No index on 'expensive'")
|
|
402
|
+
end.should raise_error(Tractor::MissingIndexError, "No index on 'expensive'")
|
|
403
403
|
end
|
|
404
404
|
|
|
405
405
|
it "takes an index name and value and finds all matching objects" do
|
data/spec/model/mapper_spec.rb
CHANGED
|
@@ -89,8 +89,24 @@ describe Tractor::Model::Mapper do
|
|
|
89
89
|
redis_monkey.evil.should == true
|
|
90
90
|
redis_monkey.id.should == "a1a"
|
|
91
91
|
end
|
|
92
|
+
|
|
93
|
+
context "when the instance already exists in it's tractor representation" do
|
|
94
|
+
attr_reader :monkey, :tractor_monkey
|
|
95
|
+
before do
|
|
96
|
+
@monkey = Monkey.new("Dec. 3, 1981", true, 'a1a')
|
|
97
|
+
@tractor_monkey = MonkeyClient.create_from_instance(monkey)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
it "returns the instance from the tractor representation" do
|
|
101
|
+
result = MonkeyClient.create_from_instance(monkey)
|
|
102
|
+
result.id.should == tractor_monkey.id
|
|
103
|
+
result.birthday.should == tractor_monkey.birthday
|
|
104
|
+
result.evil.should == tractor_monkey.evil
|
|
105
|
+
end
|
|
106
|
+
end
|
|
92
107
|
end
|
|
93
108
|
|
|
109
|
+
|
|
94
110
|
describe "update_from_instance" do
|
|
95
111
|
it "ensures dependencies are met"
|
|
96
112
|
it "finds an existing record based on id and updates the attributes accordingly" do
|
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.5"
|
|
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-08-16}
|
|
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 = [
|
|
@@ -36,7 +36,7 @@ Gem::Specification.new do |s|
|
|
|
36
36
|
s.homepage = %q{http://github.com/gizm0duck/tractor}
|
|
37
37
|
s.rdoc_options = ["--charset=UTF-8"]
|
|
38
38
|
s.require_paths = ["lib"]
|
|
39
|
-
s.rubygems_version = %q{1.3.
|
|
39
|
+
s.rubygems_version = %q{1.3.7}
|
|
40
40
|
s.summary = %q{Very simple object mapping for ruby objects}
|
|
41
41
|
s.test_files = [
|
|
42
42
|
"spec/model/base_spec.rb",
|
|
@@ -49,7 +49,7 @@ Gem::Specification.new do |s|
|
|
|
49
49
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
|
50
50
|
s.specification_version = 3
|
|
51
51
|
|
|
52
|
-
if Gem::Version.new(Gem::
|
|
52
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
|
53
53
|
s.add_runtime_dependency(%q<redis>, [">= 2.0.0"])
|
|
54
54
|
s.add_runtime_dependency(%q<yajl-ruby>, [">= 0.7.6"])
|
|
55
55
|
else
|
metadata
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: tractor
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
|
|
4
|
+
hash: 5
|
|
5
|
+
prerelease: false
|
|
6
|
+
segments:
|
|
7
|
+
- 0
|
|
8
|
+
- 4
|
|
9
|
+
- 5
|
|
10
|
+
version: 0.4.5
|
|
5
11
|
platform: ruby
|
|
6
12
|
authors:
|
|
7
13
|
- Shane Wolf
|
|
@@ -9,29 +15,41 @@ autorequire:
|
|
|
9
15
|
bindir: bin
|
|
10
16
|
cert_chain: []
|
|
11
17
|
|
|
12
|
-
date: 2010-
|
|
18
|
+
date: 2010-08-16 00:00:00 -07:00
|
|
13
19
|
default_executable:
|
|
14
20
|
dependencies:
|
|
15
21
|
- !ruby/object:Gem::Dependency
|
|
16
22
|
name: redis
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
23
|
+
prerelease: false
|
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
20
26
|
requirements:
|
|
21
27
|
- - ">="
|
|
22
28
|
- !ruby/object:Gem::Version
|
|
29
|
+
hash: 15
|
|
30
|
+
segments:
|
|
31
|
+
- 2
|
|
32
|
+
- 0
|
|
33
|
+
- 0
|
|
23
34
|
version: 2.0.0
|
|
24
|
-
|
|
35
|
+
type: :runtime
|
|
36
|
+
version_requirements: *id001
|
|
25
37
|
- !ruby/object:Gem::Dependency
|
|
26
38
|
name: yajl-ruby
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
39
|
+
prerelease: false
|
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
|
41
|
+
none: false
|
|
30
42
|
requirements:
|
|
31
43
|
- - ">="
|
|
32
44
|
- !ruby/object:Gem::Version
|
|
45
|
+
hash: 15
|
|
46
|
+
segments:
|
|
47
|
+
- 0
|
|
48
|
+
- 7
|
|
49
|
+
- 6
|
|
33
50
|
version: 0.7.6
|
|
34
|
-
|
|
51
|
+
type: :runtime
|
|
52
|
+
version_requirements: *id002
|
|
35
53
|
description: Very simple object mappings for ruby objects
|
|
36
54
|
email: shanewolf@gmail.com
|
|
37
55
|
executables: []
|
|
@@ -67,21 +85,27 @@ rdoc_options:
|
|
|
67
85
|
require_paths:
|
|
68
86
|
- lib
|
|
69
87
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
88
|
+
none: false
|
|
70
89
|
requirements:
|
|
71
90
|
- - ">="
|
|
72
91
|
- !ruby/object:Gem::Version
|
|
92
|
+
hash: 3
|
|
93
|
+
segments:
|
|
94
|
+
- 0
|
|
73
95
|
version: "0"
|
|
74
|
-
version:
|
|
75
96
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
|
+
none: false
|
|
76
98
|
requirements:
|
|
77
99
|
- - ">="
|
|
78
100
|
- !ruby/object:Gem::Version
|
|
101
|
+
hash: 3
|
|
102
|
+
segments:
|
|
103
|
+
- 0
|
|
79
104
|
version: "0"
|
|
80
|
-
version:
|
|
81
105
|
requirements: []
|
|
82
106
|
|
|
83
107
|
rubyforge_project:
|
|
84
|
-
rubygems_version: 1.3.
|
|
108
|
+
rubygems_version: 1.3.7
|
|
85
109
|
signing_key:
|
|
86
110
|
specification_version: 3
|
|
87
111
|
summary: Very simple object mapping for ruby objects
|