redisted 0.0.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 +7 -0
- data/Gemfile +16 -0
- data/Guardfile +31 -0
- data/README.md +643 -0
- data/Rakefile +1 -0
- data/config/application.rb +59 -0
- data/config/boot.rb +6 -0
- data/config/environment.rb +7 -0
- data/config/redis.rb +17 -0
- data/config/redis.yml +0 -0
- data/index.html +1 -0
- data/lib/redisted/base.rb +129 -0
- data/lib/redisted/delete.rb +21 -0
- data/lib/redisted/errors.rb +12 -0
- data/lib/redisted/fields.rb +59 -0
- data/lib/redisted/getsetattr.rb +220 -0
- data/lib/redisted/index.rb +330 -0
- data/lib/redisted/instantiate.rb +34 -0
- data/lib/redisted/references.rb +105 -0
- data/lib/redisted/relation.rb +190 -0
- data/lib/redisted/version.rb +3 -0
- data/lib/redisted.rb +10 -0
- data/log/development.log +0 -0
- data/log/test.log +0 -0
- data/readme.md +643 -0
- data/redisted.gemspec +23 -0
- data/spec/redisted/callbacks_spec.rb +76 -0
- data/spec/redisted/delete_destroy_spec.rb +49 -0
- data/spec/redisted/fields_spec.rb +118 -0
- data/spec/redisted/filter_index_spec.rb +90 -0
- data/spec/redisted/find_spec.rb +30 -0
- data/spec/redisted/log/test.log +0 -0
- data/spec/redisted/match_index_spec.rb +36 -0
- data/spec/redisted/persisted_fields_spec.rb +238 -0
- data/spec/redisted/recalculate_index_spec.rb +6 -0
- data/spec/redisted/references_spec.rb +128 -0
- data/spec/redisted/scopes_spec.rb +88 -0
- data/spec/redisted/unique_index_spec.rb +113 -0
- data/spec/redisted/validations_spec.rb +71 -0
- data/spec/spec_helper.rb +61 -0
- metadata +110 -0
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Callbacks" do
|
4
|
+
before :each do
|
5
|
+
$create_called=false
|
6
|
+
$update_called=false
|
7
|
+
$save_called=false
|
8
|
+
$destroy_called=false
|
9
|
+
end
|
10
|
+
class CallbackTest < Redisted::Base
|
11
|
+
field :name, type: :string
|
12
|
+
after_create :create_handler
|
13
|
+
def create_handler
|
14
|
+
$create_called=true
|
15
|
+
end
|
16
|
+
after_update do
|
17
|
+
$update_called=true
|
18
|
+
end
|
19
|
+
after_save do
|
20
|
+
$save_called=true
|
21
|
+
end
|
22
|
+
after_destroy :destroy_handler
|
23
|
+
def destroy_handler
|
24
|
+
$destroy_called=false
|
25
|
+
end
|
26
|
+
end
|
27
|
+
it "work on create" do
|
28
|
+
a=CallbackTest.new
|
29
|
+
$create_called.should be_false
|
30
|
+
a.save
|
31
|
+
$create_called.should be_true
|
32
|
+
$update_called.should be_false
|
33
|
+
end
|
34
|
+
it "work on update" do
|
35
|
+
a=CallbackTest.new
|
36
|
+
$create_called.should be_false
|
37
|
+
$update_called.should be_false
|
38
|
+
a.save
|
39
|
+
$create_called.should be_true
|
40
|
+
$update_called.should be_false
|
41
|
+
a.name="Test"
|
42
|
+
$create_called.should be_true
|
43
|
+
$update_called.should be_true
|
44
|
+
end
|
45
|
+
it "work on save" do
|
46
|
+
a=CallbackTest.create
|
47
|
+
$save_called=false
|
48
|
+
$save_called.should be_false
|
49
|
+
a.name="Test"
|
50
|
+
$save_called.should be_true
|
51
|
+
end
|
52
|
+
it "work on save (non-cached)" do
|
53
|
+
a=CallbackTest.create
|
54
|
+
$save_called=false
|
55
|
+
$save_called.should be_false
|
56
|
+
a.cache do
|
57
|
+
a.name="Test"
|
58
|
+
$save_called.should be_false
|
59
|
+
end
|
60
|
+
$save_called.should be_true
|
61
|
+
end
|
62
|
+
it "work on destroy" do
|
63
|
+
a=CallbackTest.create
|
64
|
+
$destroy_called=false
|
65
|
+
$destroy_called.should be_false
|
66
|
+
a.destroy
|
67
|
+
$destroy_called.should be_true
|
68
|
+
end
|
69
|
+
it "not work on delete" do
|
70
|
+
a=CallbackTest.create
|
71
|
+
$destroy_called=false
|
72
|
+
$destroy_called.should be_false
|
73
|
+
a.delete
|
74
|
+
$destroy_called.should be_false
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Delete/Destroy" do
|
4
|
+
class FieldTest < Redisted::Base
|
5
|
+
field :field1, type: :string
|
6
|
+
field :field2, type: :string
|
7
|
+
field :field3, type: :string
|
8
|
+
end
|
9
|
+
it "an object can be deleted" do
|
10
|
+
a=FieldTest.create({field1: "test1123", field2: "test1223"})
|
11
|
+
key="field_test:#{a.id}"
|
12
|
+
$redis.hget(key,"field1").should ==("test1123")
|
13
|
+
$redis.hget(key,"field2").should ==("test1223")
|
14
|
+
a.delete
|
15
|
+
$redis.hget(key,"field1").should be_nil
|
16
|
+
$redis.hget(key,"field2").should be_nil
|
17
|
+
end
|
18
|
+
it "an object can be destroyed" do
|
19
|
+
a=FieldTest.create({field1: "test1123", field2: "test1223"})
|
20
|
+
key="field_test:#{a.id}"
|
21
|
+
$redis.hget(key,"field1").should ==("test1123")
|
22
|
+
$redis.hget(key,"field2").should ==("test1223")
|
23
|
+
a.destroy
|
24
|
+
$redis.hget(key,"field1").should be_nil
|
25
|
+
$redis.hget(key,"field2").should be_nil
|
26
|
+
end
|
27
|
+
it "an object that was opened (not created) can be deleted" do
|
28
|
+
orig=FieldTest.create({field1: "test1123", field2: "test1223"})
|
29
|
+
a=FieldTest.find orig.id
|
30
|
+
a.id.should ==orig.id
|
31
|
+
key="field_test:#{a.id}"
|
32
|
+
$redis.hget(key,"field1").should ==("test1123")
|
33
|
+
$redis.hget(key,"field2").should ==("test1223")
|
34
|
+
a.delete
|
35
|
+
$redis.hget(key,"field1").should be_nil
|
36
|
+
$redis.hget(key,"field2").should be_nil
|
37
|
+
end
|
38
|
+
it "an object that was opened (not created) can be destroyed" do
|
39
|
+
orig=FieldTest.create({field1: "test1123", field2: "test1223"})
|
40
|
+
a=FieldTest.find orig.id
|
41
|
+
a.id.should ==orig.id
|
42
|
+
key="field_test:#{a.id}"
|
43
|
+
$redis.hget(key,"field1").should ==("test1123")
|
44
|
+
$redis.hget(key,"field2").should ==("test1223")
|
45
|
+
a.destroy
|
46
|
+
$redis.hget(key,"field1").should be_nil
|
47
|
+
$redis.hget(key,"field2").should be_nil
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Fields" do
|
4
|
+
class FieldTest1 < Redisted::Base
|
5
|
+
field :field1, type: :string
|
6
|
+
field :field2, type: :string
|
7
|
+
field :field3, type: :string
|
8
|
+
end
|
9
|
+
class FieldTest2 < Redisted::Base
|
10
|
+
field :field1, type: :string
|
11
|
+
field :field2, type: :integer
|
12
|
+
field :field3, type: :string
|
13
|
+
end
|
14
|
+
it "can be added to a model using new as separate statements" do
|
15
|
+
|
16
|
+
a=FieldTest1.new
|
17
|
+
a.save
|
18
|
+
a.field1="test1"
|
19
|
+
a.field2="test2"
|
20
|
+
a.field3="test3"
|
21
|
+
|
22
|
+
key="field_test1:#{a.id}"
|
23
|
+
$redis.hget(key,"field1").should ==("test1")
|
24
|
+
$redis.hget(key,"field2").should ==("test2")
|
25
|
+
$redis.hget(key,"field3").should ==("test3")
|
26
|
+
end
|
27
|
+
it "can be added to a model using create as separate statements" do
|
28
|
+
a=FieldTest1.create
|
29
|
+
a.field1="test1"
|
30
|
+
a.field2="test2"
|
31
|
+
a.field3="test3"
|
32
|
+
|
33
|
+
key="field_test1:#{a.id}"
|
34
|
+
$redis.hget(key,"field1").should ==("test1")
|
35
|
+
$redis.hget(key,"field2").should ==("test2")
|
36
|
+
$redis.hget(key,"field3").should ==("test3")
|
37
|
+
end
|
38
|
+
it "can be added to a model using new as arguments" do
|
39
|
+
a=FieldTest1.new({field1: "test1", field2: "test2", field3: "test3"})
|
40
|
+
a.save
|
41
|
+
|
42
|
+
key="field_test1:#{a.id}"
|
43
|
+
$redis.hget(key,"field1").should ==("test1")
|
44
|
+
$redis.hget(key,"field2").should ==("test2")
|
45
|
+
$redis.hget(key,"field3").should ==("test3")
|
46
|
+
end
|
47
|
+
it "can be added to a model using create as arguments" do
|
48
|
+
|
49
|
+
a=FieldTest1.create({field1: "test1", field2: "test2", field3: "test3"})
|
50
|
+
|
51
|
+
key="field_test1:#{a.id}"
|
52
|
+
$redis.hget(key,"field1").should ==("test1")
|
53
|
+
$redis.hget(key,"field2").should ==("test2")
|
54
|
+
$redis.hget(key,"field3").should ==("test3")
|
55
|
+
end
|
56
|
+
it "can be integers" do
|
57
|
+
a=FieldTest2.create
|
58
|
+
a.field2="456def"
|
59
|
+
a.save
|
60
|
+
a.field2.should ==456
|
61
|
+
key="field_test2:#{a.id}"
|
62
|
+
$redis.hget(key,"field2").should ==("456")
|
63
|
+
end
|
64
|
+
it "can be string" do
|
65
|
+
a=FieldTest2.create
|
66
|
+
a.field1="123abc"
|
67
|
+
a.save
|
68
|
+
a.field1.should =="123abc"
|
69
|
+
key="field_test2:#{a.id}"
|
70
|
+
$redis.hget(key,"field1").should ==("123abc")
|
71
|
+
end
|
72
|
+
it "can be datetime" do
|
73
|
+
now=DateTime.now
|
74
|
+
a=FieldTest2.create
|
75
|
+
a.field3=now
|
76
|
+
a.save
|
77
|
+
a.field3.should ==now
|
78
|
+
key="field_test2:#{a.id}"
|
79
|
+
$redis.hget(key,"field3").should ==now.to_s
|
80
|
+
end
|
81
|
+
it "can have their configuration read from the model's Class" do
|
82
|
+
field1_list=FieldTest1.fields
|
83
|
+
field2_list=FieldTest2.fields
|
84
|
+
|
85
|
+
field1_list[:field1].should_not be_nil
|
86
|
+
field1_list[:field1][:type].should ==:string
|
87
|
+
field1_list[:field2].should_not be_nil
|
88
|
+
field1_list[:field2][:type].should ==:string
|
89
|
+
field1_list[:field3].should_not be_nil
|
90
|
+
field1_list[:field3][:type].should ==:string # datetime...
|
91
|
+
field2_list[:field1].should_not be_nil
|
92
|
+
field2_list[:field1][:type].should ==:string
|
93
|
+
field2_list[:field2].should_not be_nil
|
94
|
+
field2_list[:field2][:type].should ==:integer
|
95
|
+
field2_list[:field3].should_not be_nil
|
96
|
+
field2_list[:field3][:type].should ==:string
|
97
|
+
|
98
|
+
end
|
99
|
+
it "can have their configuration read from an instance of the model" do
|
100
|
+
a1=FieldTest1.new
|
101
|
+
a2=FieldTest2.new
|
102
|
+
field1_list=a1.fields
|
103
|
+
field2_list=a2.fields
|
104
|
+
|
105
|
+
field1_list[:field1].should_not be_nil
|
106
|
+
field1_list[:field1][:type].should ==:string
|
107
|
+
field1_list[:field2].should_not be_nil
|
108
|
+
field1_list[:field2][:type].should ==:string
|
109
|
+
field1_list[:field3].should_not be_nil
|
110
|
+
field1_list[:field3][:type].should ==:string # datetime...
|
111
|
+
field2_list[:field1].should_not be_nil
|
112
|
+
field2_list[:field1][:type].should ==:string
|
113
|
+
field2_list[:field2].should_not be_nil
|
114
|
+
field2_list[:field2][:type].should ==:integer
|
115
|
+
field2_list[:field3].should_not be_nil
|
116
|
+
field2_list[:field3][:type].should ==:string
|
117
|
+
end
|
118
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
|
2
|
+
describe "Filter Index" do
|
3
|
+
it "creates filter index" do
|
4
|
+
class FilterIndex1 < Redisted::Base
|
5
|
+
field :name, type: :string
|
6
|
+
field :test_int, type: :integer
|
7
|
+
index :odd, includes: ->(elem){(elem.test_int%2)!=0}
|
8
|
+
index :even, includes: ->(elem){(elem.test_int%2)==0}
|
9
|
+
index :thrice, includes: ->(elem){(elem.test_int%3)==0}
|
10
|
+
index :asc, order: ->(elem){elem.test_int},fields: :test_int
|
11
|
+
index :desc, order: ->(elem){-elem.test_int},fields: :test_int
|
12
|
+
end
|
13
|
+
a=FilterIndex1.create({test_int: 3})
|
14
|
+
b=FilterIndex1.create({test_int: 4})
|
15
|
+
c=FilterIndex1.create({test_int: 2})
|
16
|
+
d=FilterIndex1.create({test_int: 9})
|
17
|
+
e=FilterIndex1.create({test_int: 1})
|
18
|
+
f=FilterIndex1.create({test_int: 6})
|
19
|
+
|
20
|
+
res=FilterIndex1.by_odd.by_asc.all
|
21
|
+
res.size.should ==3
|
22
|
+
res[0].id.to_i.should ==e.id
|
23
|
+
res[1].id.to_i.should ==a.id
|
24
|
+
res[2].id.to_i.should ==d.id
|
25
|
+
|
26
|
+
res=FilterIndex1.by_even.by_asc.all
|
27
|
+
res.size.should ==3
|
28
|
+
res[0].id.to_i.should ==c.id
|
29
|
+
res[1].id.to_i.should ==b.id
|
30
|
+
res[2].id.to_i.should ==f.id
|
31
|
+
|
32
|
+
res=FilterIndex1.by_even.by_desc.all
|
33
|
+
res.size.should ==3
|
34
|
+
res[0].id.to_i.should ==f.id
|
35
|
+
res[1].id.to_i.should ==b.id
|
36
|
+
res[2].id.to_i.should ==c.id
|
37
|
+
|
38
|
+
res=FilterIndex1.by_thrice.by_asc.all
|
39
|
+
res.size.should ==3
|
40
|
+
res[0].id.to_i.should ==a.id
|
41
|
+
res[1].id.to_i.should ==f.id
|
42
|
+
res[2].id.to_i.should ==d.id
|
43
|
+
|
44
|
+
res=FilterIndex1.by_thrice.by_odd.by_asc.all
|
45
|
+
res.size.should ==2
|
46
|
+
res[0].id.to_i.should ==a.id
|
47
|
+
res[1].id.to_i.should ==d.id
|
48
|
+
|
49
|
+
res=FilterIndex1.by_thrice.by_even.by_asc.all
|
50
|
+
res.size.should ==1
|
51
|
+
res[0].id.to_i.should ==f.id
|
52
|
+
end
|
53
|
+
it "allows incomplete filters to work" do
|
54
|
+
class FilterIndex2 < Redisted::Base
|
55
|
+
field :name, type: :string
|
56
|
+
field :test_int, type: :integer
|
57
|
+
index :odd, includes: ->(elem){(elem.test_int%2)!=0}
|
58
|
+
index :even, includes: ->(elem){(elem.test_int%2)==0}
|
59
|
+
index :thrice, includes: ->(elem){(elem.test_int%3)==0}
|
60
|
+
index :asc, order: ->(elem){elem.test_int},fields: :test_int
|
61
|
+
index :desc, order: ->(elem){-elem.test_int},fields: :test_int
|
62
|
+
end
|
63
|
+
a=FilterIndex2.create({test_int: 3})
|
64
|
+
b=FilterIndex2.create({test_int: 4})
|
65
|
+
c=FilterIndex2.create({test_int: 2})
|
66
|
+
d=FilterIndex2.create({test_int: 9})
|
67
|
+
e=FilterIndex2.create({test_int: 1})
|
68
|
+
f=FilterIndex2.create({test_int: 6})
|
69
|
+
|
70
|
+
aaa=FilterIndex2.by_odd
|
71
|
+
res=aaa.by_asc.all
|
72
|
+
res.size.should ==3
|
73
|
+
res[0].id.to_i.should ==e.id
|
74
|
+
res[1].id.to_i.should ==a.id
|
75
|
+
res[2].id.to_i.should ==d.id
|
76
|
+
|
77
|
+
aaa=FilterIndex2.scoped
|
78
|
+
res=aaa.by_thrice.by_asc.all
|
79
|
+
res.size.should ==3
|
80
|
+
res[0].id.to_i.should ==a.id
|
81
|
+
res[1].id.to_i.should ==f.id
|
82
|
+
res[2].id.to_i.should ==d.id
|
83
|
+
|
84
|
+
aaa=FilterIndex2.by_thrice.by_even.by_asc
|
85
|
+
res=aaa.all
|
86
|
+
res.size.should ==1
|
87
|
+
res[0].id.to_i.should ==f.id
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Find" do
|
4
|
+
class FieldTest < Redisted::Base
|
5
|
+
field :field1, type: :string
|
6
|
+
field :field2, type: :string
|
7
|
+
field :field3, type: :string
|
8
|
+
end
|
9
|
+
it "can find an object with a specific ID" do
|
10
|
+
orig=FieldTest.create({field1: "test1123", field2: "test1223"})
|
11
|
+
id=orig.id
|
12
|
+
obj=FieldTest.find id
|
13
|
+
obj.field1.should =="test1123"
|
14
|
+
obj.field2.should =="test1223"
|
15
|
+
orig.id.should ==obj.id
|
16
|
+
orig.object_id.should_not ==obj.object_id
|
17
|
+
end
|
18
|
+
it "can find an array objects with a list of IDs" do
|
19
|
+
orig1=FieldTest.create({field1: "test1123", field2: "test1223"})
|
20
|
+
orig2=FieldTest.create({field1: "test2123", field2: "test2223"})
|
21
|
+
orig3=FieldTest.create({field1: "test3123", field2: "test3223"})
|
22
|
+
objs=FieldTest.find [orig1.id,orig3.id,orig2.id]
|
23
|
+
objs[0].field1="test1123"
|
24
|
+
objs[0].field2="test1223"
|
25
|
+
objs[2].field1="test2123"
|
26
|
+
objs[2].field2="test2223"
|
27
|
+
objs[1].field1="test3123"
|
28
|
+
objs[1].field2="test3223"
|
29
|
+
end
|
30
|
+
end
|
File without changes
|
@@ -0,0 +1,36 @@
|
|
1
|
+
|
2
|
+
describe "Match Index" do
|
3
|
+
it "creates match index" do
|
4
|
+
class MatchIndex1 < Redisted::Base
|
5
|
+
field :name, type: :string
|
6
|
+
field :test_int, type: :string
|
7
|
+
field :provider, type: :string
|
8
|
+
|
9
|
+
index :provider, match: ->(elem){(elem.provider)}, fields: :provider,order: ->(elem){elem.test_int}
|
10
|
+
end
|
11
|
+
a=MatchIndex1.create({provider: "cable",test_int: 1})
|
12
|
+
b=MatchIndex1.create({provider: "satellite",test_int: 2})
|
13
|
+
c=MatchIndex1.create({provider: "cable",test_int: 3})
|
14
|
+
d=MatchIndex1.create({provider: "overair",test_int: 4})
|
15
|
+
e=MatchIndex1.create({provider: "satellite",test_int: 5})
|
16
|
+
f=MatchIndex1.create({provider: "cable",test_int: 6})
|
17
|
+
|
18
|
+
res=MatchIndex1.by_provider("cable").all
|
19
|
+
res.size.should ==3
|
20
|
+
res[0].id.to_i.should ==a.id
|
21
|
+
res[1].id.to_i.should ==c.id
|
22
|
+
res[2].id.to_i.should ==f.id
|
23
|
+
|
24
|
+
res=MatchIndex1.by_provider("satellite").all
|
25
|
+
res.size.should ==2
|
26
|
+
res[0].id.to_i.should ==b.id
|
27
|
+
res[1].id.to_i.should ==e.id
|
28
|
+
|
29
|
+
res=MatchIndex1.by_provider("overair").all
|
30
|
+
res.size.should ==1
|
31
|
+
res[0].id.to_i.should ==d.id
|
32
|
+
|
33
|
+
res=MatchIndex1.by_provider("xxx").all
|
34
|
+
res.size.should ==0
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,238 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Persisted Fields" do
|
4
|
+
class FieldTest1 < Redisted::Base
|
5
|
+
field :field1, type: :string
|
6
|
+
field :field2, type: :string
|
7
|
+
field :field3, type: :string
|
8
|
+
end
|
9
|
+
it "can be saved one value at a time" do
|
10
|
+
a=FieldTest1.create
|
11
|
+
key="field_test1:#{a.id}"
|
12
|
+
$redis.hget(key,"field1").should be_nil
|
13
|
+
$redis.hget(key,"field2").should be_nil
|
14
|
+
a.field1="test"
|
15
|
+
$redis.hget(key,"field1").should =="test"
|
16
|
+
$redis.hget(key,"field2").should be_nil
|
17
|
+
a.field2="test2"
|
18
|
+
$redis.hget(key,"field1").should =="test"
|
19
|
+
$redis.hget(key,"field2").should =="test2"
|
20
|
+
a.save
|
21
|
+
$redis.hget(key,"field1").should =="test"
|
22
|
+
$redis.hget(key,"field2").should =="test2"
|
23
|
+
end
|
24
|
+
it "can be cached and save them all in one redis call" do
|
25
|
+
a=FieldTest1.create
|
26
|
+
key="field_test1:#{a.id}"
|
27
|
+
a.cache do
|
28
|
+
$redis.hget(key,"field1").should be_nil
|
29
|
+
$redis.hget(key,"field2").should be_nil
|
30
|
+
a.field1="test"
|
31
|
+
$redis.hget(key,"field1").should be_nil
|
32
|
+
$redis.hget(key,"field2").should be_nil
|
33
|
+
a.field2="test2"
|
34
|
+
$redis.hget(key,"field1").should be_nil
|
35
|
+
$redis.hget(key,"field2").should be_nil
|
36
|
+
end
|
37
|
+
$redis.hget(key,"field1").should =="test"
|
38
|
+
$redis.hget(key,"field2").should =="test2"
|
39
|
+
end
|
40
|
+
it "can be cached and save them all in one redis call without using a block" do
|
41
|
+
a=FieldTest1.create
|
42
|
+
key="field_test1:#{a.id}"
|
43
|
+
a.cache
|
44
|
+
$redis.hget(key,"field1").should be_nil
|
45
|
+
$redis.hget(key,"field2").should be_nil
|
46
|
+
a.field1="test"
|
47
|
+
$redis.hget(key,"field1").should be_nil
|
48
|
+
$redis.hget(key,"field2").should be_nil
|
49
|
+
a.field2="test2"
|
50
|
+
$redis.hget(key,"field1").should be_nil
|
51
|
+
$redis.hget(key,"field2").should be_nil
|
52
|
+
a.save
|
53
|
+
$redis.hget(key,"field1").should =="test"
|
54
|
+
$redis.hget(key,"field2").should =="test2"
|
55
|
+
end
|
56
|
+
it "caches until first save" do
|
57
|
+
a=FieldTest1.new
|
58
|
+
a.field1="Test"
|
59
|
+
a.field2="Test2"
|
60
|
+
key="field_test1:#{a.id}"
|
61
|
+
$redis.hget(key,"field1").should be_nil
|
62
|
+
$redis.hget(key,"field2").should be_nil
|
63
|
+
a.save
|
64
|
+
key="field_test1:#{a.id}"
|
65
|
+
$redis.hget(key,"field1").should =="Test"
|
66
|
+
$redis.hget(key,"field2").should =="Test2"
|
67
|
+
end
|
68
|
+
it "can be configured to cache all until save" do
|
69
|
+
class FieldTest < Redisted::Base
|
70
|
+
always_cache_until_save
|
71
|
+
field :field1, type: :string
|
72
|
+
field :field2, type: :string
|
73
|
+
end
|
74
|
+
a=FieldTest.create
|
75
|
+
key="field_test:#{a.id}"
|
76
|
+
$redis.hget(key,"field1").should be_nil
|
77
|
+
$redis.hget(key,"field2").should be_nil
|
78
|
+
a.field1="test"
|
79
|
+
$redis.hget(key,"field1").should be_nil
|
80
|
+
$redis.hget(key,"field2").should be_nil
|
81
|
+
a.field2="test2"
|
82
|
+
$redis.hget(key,"field1").should be_nil
|
83
|
+
$redis.hget(key,"field2").should be_nil
|
84
|
+
a.save
|
85
|
+
$redis.hget(key,"field1").should =="test"
|
86
|
+
$redis.hget(key,"field2").should =="test2"
|
87
|
+
end
|
88
|
+
it "can be read one by one as needed" do
|
89
|
+
key="field_test1:12345"
|
90
|
+
$redis.hmset key,"field1","3","field2","4"
|
91
|
+
b=FieldTest1.find(12345)
|
92
|
+
b.field1.should =="3"
|
93
|
+
$redis.hmset key,"field1","5","field2","6"
|
94
|
+
b.field2.should =="6"
|
95
|
+
end
|
96
|
+
it "can have the cache flushed" do
|
97
|
+
key="field_test1:12345"
|
98
|
+
$redis.hmset key,"field1","3","field2","4"
|
99
|
+
b=FieldTest1.find(12345)
|
100
|
+
b.field1.should =="3"
|
101
|
+
$redis.hmset key,"field1","5","field2","6"
|
102
|
+
b.field1.should =="3"
|
103
|
+
b.field2.should =="6"
|
104
|
+
b.flush
|
105
|
+
b.field1.should =="5"
|
106
|
+
b.field2.should =="6"
|
107
|
+
end
|
108
|
+
it "are read only once then cached" do
|
109
|
+
key="field_test1:12345"
|
110
|
+
$redis.hmset key,"field1","3"
|
111
|
+
b=FieldTest1.find(12345)
|
112
|
+
b.field1.should =="3"
|
113
|
+
$redis.hmset key,"field1","5"
|
114
|
+
b.field1.should =="3"
|
115
|
+
end
|
116
|
+
it "can be read and cached all at once (at load)" do
|
117
|
+
class FieldTest < Redisted::Base
|
118
|
+
pre_cache_all
|
119
|
+
field :field1, type: :string
|
120
|
+
field :field2, type: :string
|
121
|
+
field :field3, type: :string
|
122
|
+
end
|
123
|
+
key="field_test:12345"
|
124
|
+
$redis.hmset key,"field1","3","field2","4","field3","5"
|
125
|
+
b=FieldTest.find(12345)
|
126
|
+
$redis.hmset key,"field1","6","field2","7","field3","8"
|
127
|
+
b.field1.should =="3"
|
128
|
+
b.field2.should =="4"
|
129
|
+
b.field3.should =="5"
|
130
|
+
b.flush
|
131
|
+
b.field1.should =="6"
|
132
|
+
b.field2.should =="7"
|
133
|
+
b.field3.should =="8"
|
134
|
+
end
|
135
|
+
it "can be automatically read and cached at object creation" do
|
136
|
+
class FieldTest < Redisted::Base
|
137
|
+
pre_cache_all when: :create
|
138
|
+
field :field1, type: :string
|
139
|
+
field :field2, type: :string
|
140
|
+
field :field3, type: :string
|
141
|
+
end
|
142
|
+
key="field_test:12345"
|
143
|
+
$redis.hmset key,"field1","3","field2","4","field3","5"
|
144
|
+
b=FieldTest.find(12345)
|
145
|
+
$redis.hmset key,"field1","6","field2","7","field3","8"
|
146
|
+
b.field1.should =="3"
|
147
|
+
b.field2.should =="4"
|
148
|
+
b.field3.should =="5"
|
149
|
+
b.flush
|
150
|
+
b.field1.should =="6"
|
151
|
+
b.field2.should =="7"
|
152
|
+
b.field3.should =="8"
|
153
|
+
end
|
154
|
+
it "can be automatically read and cached at first field read" do
|
155
|
+
class FieldTest < Redisted::Base
|
156
|
+
pre_cache_all when: :first_read
|
157
|
+
field :field1, type: :string
|
158
|
+
field :field2, type: :string
|
159
|
+
field :field3, type: :string
|
160
|
+
end
|
161
|
+
key="field_test:12345"
|
162
|
+
$redis.hmset key,"field1","3","field2","4","field3","5"
|
163
|
+
b=FieldTest.find(12345)
|
164
|
+
$redis.hmset key,"field1","6","field2","7","field3","8"
|
165
|
+
b.field1.should =="6"
|
166
|
+
$redis.hmset key,"field1","9","field2","10","field3","11"
|
167
|
+
b.field2.should =="7"
|
168
|
+
b.field3.should =="8"
|
169
|
+
b.flush
|
170
|
+
b.field1.should =="9"
|
171
|
+
b.field2.should =="10"
|
172
|
+
b.field3.should =="11"
|
173
|
+
end
|
174
|
+
it "can have autoamtic caching explicitely for all fields" do
|
175
|
+
class FieldTest < Redisted::Base
|
176
|
+
pre_cache_all when: :create, keys: :all
|
177
|
+
field :field1, type: :string
|
178
|
+
field :field2, type: :string
|
179
|
+
field :field3, type: :string
|
180
|
+
end
|
181
|
+
key="field_test:12345"
|
182
|
+
$redis.hmset key,"field1","3","field2","4","field3","5"
|
183
|
+
b=FieldTest.find(12345)
|
184
|
+
$redis.hmset key,"field1","6","field2","7","field3","8"
|
185
|
+
b.field1.should =="3"
|
186
|
+
b.field2.should =="4"
|
187
|
+
b.field3.should =="5"
|
188
|
+
b.flush
|
189
|
+
b.field1.should =="6"
|
190
|
+
b.field2.should =="7"
|
191
|
+
b.field3.should =="8"
|
192
|
+
end
|
193
|
+
it "can have automatic caching for some fields only" do
|
194
|
+
class FieldTest < Redisted::Base
|
195
|
+
pre_cache_all when: :create, keys: [:field1,:field3]
|
196
|
+
field :field1, type: :string
|
197
|
+
field :field2, type: :string
|
198
|
+
field :field3, type: :string
|
199
|
+
end
|
200
|
+
key="field_test:12345"
|
201
|
+
$redis.hmset key,"field1","3","field2","4","field3","5"
|
202
|
+
b=FieldTest.find(12345)
|
203
|
+
$redis.hmset key,"field1","6","field2","7","field3","8"
|
204
|
+
b.field1.should =="3"
|
205
|
+
b.field2.should =="7"
|
206
|
+
b.field3.should =="5"
|
207
|
+
b.flush
|
208
|
+
b.field1.should =="6"
|
209
|
+
b.field2.should =="7"
|
210
|
+
b.field3.should =="8"
|
211
|
+
end
|
212
|
+
it "can have automatic caching for all fields except specified fields" do
|
213
|
+
class FieldTest < Redisted::Base
|
214
|
+
pre_cache_all when: :create, except: [:field1,:field3]
|
215
|
+
field :field1, type: :string
|
216
|
+
field :field2, type: :string
|
217
|
+
field :field3, type: :string
|
218
|
+
end
|
219
|
+
key="field_test:12345"
|
220
|
+
$redis.hmset key,"field1","3","field2","4","field3","5"
|
221
|
+
b=FieldTest.find(12345)
|
222
|
+
$redis.hmset key,"field1","6","field2","7","field3","8"
|
223
|
+
b.field1.should =="6"
|
224
|
+
b.field2.should =="4"
|
225
|
+
b.field3.should =="8"
|
226
|
+
b.flush
|
227
|
+
b.field1.should =="6"
|
228
|
+
b.field2.should =="7"
|
229
|
+
b.field3.should =="8"
|
230
|
+
end
|
231
|
+
|
232
|
+
it "can populate an object via parameters to create" do
|
233
|
+
obj=FieldTest1.create({field1: "test111",field2: "test222"})
|
234
|
+
key="field_test1:#{obj.id}"
|
235
|
+
$redis.hget(key,"field1").should =="test111"
|
236
|
+
$redis.hget(key,"field2").should =="test222"
|
237
|
+
end
|
238
|
+
end
|