redis_storage 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/redis_storage.rb CHANGED
@@ -30,7 +30,7 @@ module RedisStorage
30
30
  end
31
31
 
32
32
  def self.random
33
- i = $db.srandmember("#{db_key}:persisted")
33
+ i = $db.srandmember(persisted_key)
34
34
  find_by_id(i) unless i.nil?
35
35
  end
36
36
  def self.find(params=nil)
@@ -41,12 +41,13 @@ module RedisStorage
41
41
  end
42
42
  end
43
43
  def self.find_by_id(entry_id)
44
+ return nil if entry_id.nil?
44
45
  r = $db.get("#{db_key}:#{entry_id}")
45
46
  new(JSON.parse(r)) unless r.nil?
46
47
  end
47
48
 
48
49
  def self.all
49
- keys = $db.smembers("#{db_key}:persisted").map do |i|
50
+ keys = $db.smembers(persisted_key).map do |i|
50
51
  "#{db_key}:#{i}"
51
52
  end
52
53
 
@@ -59,6 +60,15 @@ module RedisStorage
59
60
  end
60
61
  end
61
62
 
63
+ def self.count
64
+ $db.scard(persisted_key)
65
+ end
66
+ def self.first
67
+ find_by_id $db.smembers(persisted_key).sort.first
68
+ end
69
+ def self.last
70
+ find_by_id $db.smembers(persisted_key).sort.last
71
+ end
62
72
  def serializable_hash
63
73
  self.class.attrs.inject({:id => @id}) do |a,key|
64
74
  a[key] = send(key)
@@ -77,12 +87,12 @@ module RedisStorage
77
87
  end
78
88
  def save
79
89
  unless persisted?
80
- @id = $db.incr("#{db_key}:nextid")
90
+ @id = $db.incr(self.class.next_id_key)
81
91
  end
82
92
  if valid?
83
93
  $db.multi do
84
- $db.set db_key, serializable_hash.to_json
85
- $db.sadd "#{self.class.db_key}:persisted", id
94
+ $db.set db_key, to_json
95
+ $db.sadd self.class.persisted_key, id
86
96
  end
87
97
  @id
88
98
  else
@@ -97,7 +107,7 @@ module RedisStorage
97
107
  if persisted?
98
108
  $db.multi do
99
109
  $db.del db_key
100
- $db.srem "#{self.class.db_key}:persisted", id
110
+ $db.srem self.class.persisted_key, id
101
111
  end
102
112
  true
103
113
  else
@@ -109,7 +119,7 @@ module RedisStorage
109
119
  if id.nil?
110
120
  false
111
121
  else
112
- $db.sismember("#{self.class.db_key}:persisted", id)
122
+ $db.sismember(self.class.persisted_key, id)
113
123
  end
114
124
  end
115
125
 
@@ -119,6 +129,9 @@ module RedisStorage
119
129
  def db_key
120
130
  "#{self.class.db_key}:#{self.id}"
121
131
  end
132
+ def self.persisted_key
133
+ "#{db_key}:persisted"
134
+ end
122
135
 
123
136
  def initialize(params={})
124
137
  @id = nil
@@ -127,5 +140,9 @@ module RedisStorage
127
140
  send("#{key}=", value)
128
141
  end
129
142
  end
143
+ private
144
+ def self.next_id_key
145
+ "#{db_key}:next_id"
146
+ end
130
147
  end
131
148
  end
@@ -1,3 +1,3 @@
1
1
  module RedisStorage
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.6"
3
3
  end
@@ -10,16 +10,14 @@ Gem::Specification.new do |s|
10
10
  s.email = ["dev@eger-andreas.de"]
11
11
  s.homepage = "https://github.com/sch1zo/redis_storage"
12
12
  s.summary = %q{A simple Redis ORM for Rails}
13
- s.description = %q{Provides a databackend for a Redis in Rails, will also provide a Rails 3 Generator}
13
+ s.description = %q{Provides a data backend for a Redis in Rails, will also provide a Rails 3 Generator}
14
14
 
15
- s.add_dependency 'redis'
15
+ s.add_dependency 'redis', '>= 2.2.0'
16
16
  s.add_dependency 'json'
17
17
  s.add_dependency 'activemodel', '>= 3.0.0'
18
18
 
19
19
  s.add_development_dependency 'rspec', '>= 2.0.0'
20
- s.add_development_dependency 'mocha'
21
- s.add_development_dependency 'autotest'
22
- s.add_development_dependency 'redis'
20
+ s.add_development_dependency 'mocha', '>= 0.9.10'
23
21
  s.add_development_dependency 'rails', '>= 3.0.0'
24
22
 
25
23
  s.rubyforge_project = "redis_storage"
data/spec/model_spec.rb CHANGED
@@ -90,6 +90,21 @@ describe RedisStorage::Model do
90
90
  end
91
91
  end
92
92
  end
93
+ context '#count' do
94
+ it 'should return the count of the persited model instances' do
95
+ MockModel.count.should eq(MockModel.all.size)
96
+ end
97
+ end
98
+ context '#first' do
99
+ it 'should return the instance with the oldest - smallest id' do
100
+ MockModel.first.id.should eq(1)
101
+ end
102
+ end
103
+ context '#last' do
104
+ it 'should return the instance with the newest - highest id' do
105
+ MockModel.last.id.should eq(4)
106
+ end
107
+ end
93
108
  end
94
109
  end
95
110
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_storage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,22 +9,23 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-06-26 00:00:00.000000000Z
12
+ date: 2011-06-27 00:00:00.000000000 +02:00
13
+ default_executable:
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: redis
16
- requirement: &44131200 !ruby/object:Gem::Requirement
17
+ requirement: &193730080 !ruby/object:Gem::Requirement
17
18
  none: false
18
19
  requirements:
19
20
  - - ! '>='
20
21
  - !ruby/object:Gem::Version
21
- version: '0'
22
+ version: 2.2.0
22
23
  type: :runtime
23
24
  prerelease: false
24
- version_requirements: *44131200
25
+ version_requirements: *193730080
25
26
  - !ruby/object:Gem::Dependency
26
27
  name: json
27
- requirement: &44130780 !ruby/object:Gem::Requirement
28
+ requirement: &193729660 !ruby/object:Gem::Requirement
28
29
  none: false
29
30
  requirements:
30
31
  - - ! '>='
@@ -32,10 +33,10 @@ dependencies:
32
33
  version: '0'
33
34
  type: :runtime
34
35
  prerelease: false
35
- version_requirements: *44130780
36
+ version_requirements: *193729660
36
37
  - !ruby/object:Gem::Dependency
37
38
  name: activemodel
38
- requirement: &44130280 !ruby/object:Gem::Requirement
39
+ requirement: &193729120 !ruby/object:Gem::Requirement
39
40
  none: false
40
41
  requirements:
41
42
  - - ! '>='
@@ -43,10 +44,10 @@ dependencies:
43
44
  version: 3.0.0
44
45
  type: :runtime
45
46
  prerelease: false
46
- version_requirements: *44130280
47
+ version_requirements: *193729120
47
48
  - !ruby/object:Gem::Dependency
48
49
  name: rspec
49
- requirement: &44129780 !ruby/object:Gem::Requirement
50
+ requirement: &193728620 !ruby/object:Gem::Requirement
50
51
  none: false
51
52
  requirements:
52
53
  - - ! '>='
@@ -54,43 +55,21 @@ dependencies:
54
55
  version: 2.0.0
55
56
  type: :development
56
57
  prerelease: false
57
- version_requirements: *44129780
58
+ version_requirements: *193728620
58
59
  - !ruby/object:Gem::Dependency
59
60
  name: mocha
60
- requirement: &44129400 !ruby/object:Gem::Requirement
61
- none: false
62
- requirements:
63
- - - ! '>='
64
- - !ruby/object:Gem::Version
65
- version: '0'
66
- type: :development
67
- prerelease: false
68
- version_requirements: *44129400
69
- - !ruby/object:Gem::Dependency
70
- name: autotest
71
- requirement: &44128940 !ruby/object:Gem::Requirement
61
+ requirement: &193728160 !ruby/object:Gem::Requirement
72
62
  none: false
73
63
  requirements:
74
64
  - - ! '>='
75
65
  - !ruby/object:Gem::Version
76
- version: '0'
77
- type: :development
78
- prerelease: false
79
- version_requirements: *44128940
80
- - !ruby/object:Gem::Dependency
81
- name: redis
82
- requirement: &44128520 !ruby/object:Gem::Requirement
83
- none: false
84
- requirements:
85
- - - ! '>='
86
- - !ruby/object:Gem::Version
87
- version: '0'
66
+ version: 0.9.10
88
67
  type: :development
89
68
  prerelease: false
90
- version_requirements: *44128520
69
+ version_requirements: *193728160
91
70
  - !ruby/object:Gem::Dependency
92
71
  name: rails
93
- requirement: &44128020 !ruby/object:Gem::Requirement
72
+ requirement: &193727700 !ruby/object:Gem::Requirement
94
73
  none: false
95
74
  requirements:
96
75
  - - ! '>='
@@ -98,8 +77,8 @@ dependencies:
98
77
  version: 3.0.0
99
78
  type: :development
100
79
  prerelease: false
101
- version_requirements: *44128020
102
- description: Provides a databackend for a Redis in Rails, will also provide a Rails
80
+ version_requirements: *193727700
81
+ description: Provides a data backend for a Redis in Rails, will also provide a Rails
103
82
  3 Generator
104
83
  email:
105
84
  - dev@eger-andreas.de
@@ -123,6 +102,7 @@ files:
123
102
  - spec/model_spec.rb
124
103
  - spec/spec_helper.rb
125
104
  - test/generator_test.rb
105
+ has_rdoc: true
126
106
  homepage: https://github.com/sch1zo/redis_storage
127
107
  licenses: []
128
108
  post_install_message:
@@ -143,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
123
  version: '0'
144
124
  requirements: []
145
125
  rubyforge_project: redis_storage
146
- rubygems_version: 1.8.5
126
+ rubygems_version: 1.6.2
147
127
  signing_key:
148
128
  specification_version: 3
149
129
  summary: A simple Redis ORM for Rails