rails_kvs_driver-redis_driver 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 19b774e652fcdea5cf78fc12325f96fc03c7df24
4
- data.tar.gz: 918da4d9464f9c98a0744d794a97f351150e31cb
3
+ metadata.gz: c001226ab448872b0d2b7a17f30bdfdcf00af159
4
+ data.tar.gz: 0d2fe48bbd9e808d190300bba6e527b5047e44d7
5
5
  SHA512:
6
- metadata.gz: a5eaec9a5f0a3997e45bdf447f7b360de964abc3584755e67a11472598ac36b303067ac2e79c5da90d0e95b52c9447cc87100905d710490f92b53b2f1bce8c33
7
- data.tar.gz: d9dda9ccf5209ed60e8f5188634bc67f25a2270906ca8cd48e4e0ee5cbc24226ad7b333bab81bd9146cce5a13cadaba0b6c9ae737575ce7c3666a02eb4ed0bd9
6
+ metadata.gz: 912b2a9462cf935c58ffa07a3780c97fc5213639b738631bfa02478830606d225b02e32367ec759d9b6055ed28246f0f458ccb386c6dddddfcd3cc21ce7cb332
7
+ data.tar.gz: 84f4a5967058cb0e5596bb2b630094f5dbd067274d672c769a0081661c409283c64b117dc3bc83517b81eed9fda09542183c244a0fac9c6ce0316304d1d7e60e
data/README.md CHANGED
@@ -36,6 +36,8 @@ driver_config = {
36
36
  ### connect and exec basic (set/get/delete)
37
37
  session method enables connection pool.
38
38
  ``` ruby
39
+ You can use Enumerable.
40
+
39
41
  RailsKvsDriver::RedisDriver::Driver::session(driver_config) do |redis|
40
42
 
41
43
  # set key to redis.
@@ -66,17 +68,70 @@ RailsKvsDriver::RedisDriver::Driver::session(driver_config) do |redis|
66
68
  end
67
69
  ```
68
70
 
71
+ ### list
72
+ ``` ruby
73
+ lists include Enumerable.
74
+
75
+ RailsKvsDriver::RedisDriver::Driver::session(driver_config) do |redis|
76
+
77
+ # set value to list.
78
+ redis.lists['anime'][0] = 'nyarukosan'
79
+ redis.lists['anime'][1] = 'kinnmoza'
80
+ redis.lists['anime'][2] = 'outbreakcompany'
81
+
82
+ # or can use this.
83
+ redis.lists['fruit'] = [:apple, :orange]
84
+
85
+
86
+ # get value of list.
87
+ puts redis.lists['anime'][0] # => 'nyarukosan'
88
+
89
+
90
+ # execute the block of code for each keys.
91
+ redis.lists.each do {|key| puts key } # => anime fruit
92
+
93
+ # execute the block of code for each member of list.
94
+ redis.lists['anime'].each do |index, value|
95
+ puts "#{index}: #{value}." # => '0: nyarukosan.'
96
+ # => '1: kinnmoza.'
97
+ # => '2: outbreakcompany.'
98
+ end
99
+
100
+ # get all keys
101
+ redis.lists.keys? # => ['anime', 'fruit']
102
+
103
+ # length of sorted set.
104
+ redis.lists.length # => 2
105
+
106
+ # length member of sorted set.
107
+ redis.lists['anime'].length # => 3
108
+
109
+
110
+ # delete key
111
+ redis.lists.delete('fruit')
112
+
113
+ # remove member of list.
114
+ redis.lists['anime'].delete('kinnmoza')
115
+
116
+ # or can use this.
117
+ redis.lists['anime'].delete_at(1)
118
+
119
+ end
120
+ ```
121
+
69
122
  ### sorted set
70
123
  ``` ruby
124
+ sorted_sets include Enumerable.
125
+
71
126
  RailsKvsDriver::RedisDriver::Driver::session(driver_config) do |redis|
72
127
 
73
128
  # set member to redis.
74
129
  redis.sorted_sets['anime']['nyarukosan'] = 10
75
- redis.sorted_sets['anime']['nonnonbiyori'] = 10
76
- redis.sorted_sets['anime']['kiniromosaic'] = 10
130
+ redis.sorted_sets['anime']['nonnonbiyori'] = 5
131
+ redis.sorted_sets['anime']['kiniromosaic'] = 1
77
132
 
78
133
  # or can use this.
79
- redis.sorted_set['fruit'] = [['apple', 1], [orange, 2]]
134
+ redis.sorted_set['fruit'] = [['apple', 1], ['orange', 2]]
80
135
 
81
136
 
82
137
  # get score of member.
@@ -47,6 +47,136 @@ module RailsKvsDriver::RedisDriver
47
47
  keys_with_type('string')
48
48
  end
49
49
 
50
+
51
+ #--------------------
52
+ # list (same as list of redis. refer to redis.)
53
+ #--------------------
54
+
55
+ # count value of the list.
56
+ # when the key doesn't exist, return 0.
57
+ #
58
+ # @param key [String] key of the list.
59
+ # @return [Integer] number.
60
+ # @abstract count value of the list.
61
+ def count_list_value(key)
62
+ @kvs_instance.llen(safe_key(key))
63
+ end
64
+
65
+ # delete value from list.
66
+ #
67
+ # @param key [String] key of the list.
68
+ # @param value [String] delete value.
69
+ # @abstract delete value from list.
70
+ def delete_list_value(key, value)
71
+ @kvs_instance.lrem(safe_key(key), 0, value)
72
+ end
73
+
74
+ # delete at index from list.
75
+ #
76
+ # @param key [String] key of the list.
77
+ # @param index [Integer] index of the list.
78
+ # @abstract delete at index from list.
79
+ def delete_list_value_at(key, index)
80
+ back_list = get_list_values(key, index+1)
81
+
82
+ if index > 0
83
+ @kvs_instance.ltrim(safe_key(key), 0, index-1)
84
+ else
85
+ delete(key)
86
+ end
87
+
88
+ back_list.each {|value| push_list_last(key, value) }
89
+ end
90
+
91
+ # get all keys of existed list.
92
+ #
93
+ # @return [Array<String>] keys.
94
+ # @abstract get all keys of existed list.
95
+ def get_list_keys
96
+ keys_with_type('list')
97
+ end
98
+
99
+ # get value from index of the list.
100
+ # when the key doesn't exist, return nil.
101
+ #
102
+ # @param key [String] key of the list.
103
+ # @param index [Integer] index of the list.
104
+ # @return [String] value.
105
+ # @abstract get value from index of the list.
106
+ def get_list_value(key, index)
107
+ @kvs_instance.lindex(safe_key(key), index)
108
+ end
109
+
110
+ # get values from index of the list.
111
+ # @example get_list_value(:key) => get all.
112
+ # @example get_list_value(:key, 5, 10) => 5~10 return total 6 values.
113
+ #
114
+ # @param key [String] key of the list.
115
+ # @param start [Integer] start index of the list.
116
+ # @param stop [Integer] end index of the list.
117
+ # @return [Array<String>] value.
118
+ # @abstract get values from index of the list.
119
+ def get_list_values(key, start = 0, stop = -1)
120
+ @kvs_instance.lrange(safe_key(key), start, stop)
121
+ end
122
+
123
+ # push value to first of the list.
124
+ # when the key doesn't exist, it's made newly list.
125
+ #
126
+ # @param key [String] key of list.
127
+ # @param value [String] push value.
128
+ # @return [Integer] length of list after push.
129
+ # @abstract push value to first of the list.
130
+ def push_list_first(key, value)
131
+ @kvs_instance.lpush(safe_key(key), value)
132
+ end
133
+
134
+ # push value to last of the list.
135
+ # when the key doesn't exist, it's made newly list.
136
+ #
137
+ # @param key [String] key of list.
138
+ # @param value [String] push value.
139
+ # @return [Integer] length of list after push.
140
+ # @abstract push value to last of the list.
141
+ def push_list_last(key, value)
142
+ @kvs_instance.rpush(safe_key(key), value)
143
+ end
144
+
145
+ # pop value from first of the list.
146
+ # when the key doesn't exist or is empty. return nil.
147
+ #
148
+ # @param key [String] key of the list.
149
+ # @return [String] value of the key.
150
+ # @abstract pop value from first of the list.
151
+ def pop_list_first(key)
152
+ @kvs_instance.lpop(safe_key(key))
153
+ end
154
+
155
+ # pop value from first of the list.
156
+ # when the key doesn't exist or is empty. return nil.
157
+ #
158
+ # @param key [String] key of the list.
159
+ # @return [String] value of the key.
160
+ # @abstract pop value from last of the list.
161
+ def pop_list_last(key)
162
+ @kvs_instance.rpop(safe_key(key))
163
+ end
164
+
165
+ # set value to index of the list.
166
+ #
167
+ # @param key [String] key of the list.
168
+ # @param index [Integer] index of the list.
169
+ # @param value [String] set value.
170
+ def set_list_value(key, index, value)
171
+ @kvs_instance.lset(safe_key(key), index, value)
172
+ end
173
+
174
+
175
+
176
+ #--------------------
177
+ # sorted set (same as sorted set of redis. refer to redis.)
178
+ #--------------------
179
+
50
180
  # add sorted set to kvs.
51
181
  # when the key doesn't exist, it's made newly.
52
182
  # @note same as sorted set of redis. refer to redis.
@@ -76,7 +206,7 @@ module RailsKvsDriver::RedisDriver
76
206
  # @param stop [Integer] stop index
77
207
  # @param reverse [Boolean] order by desc
78
208
  # @return [Array<String, Float>>] array of the member and score. when doesn't exist, nil.
79
- def get_sorted_set(key, start=0, stop=-1, reverse=false)
209
+ def get_sorted_set(key, start = 0, stop = -1, reverse=false)
80
210
  if reverse
81
211
  result = @kvs_instance.zrevrange(safe_key(key), start, stop, with_scores: true)
82
212
  else
@@ -1,5 +1,5 @@
1
1
  module RailsKvsDriver
2
2
  module RedisDriver
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -24,5 +24,5 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "yard"
25
25
 
26
26
  spec.add_dependency "redis"
27
- spec.add_dependency "rails_kvs_driver", "0.2.1"
27
+ spec.add_dependency "rails_kvs_driver", "0.3.1"
28
28
  end
@@ -40,23 +40,6 @@ shared_examples_for 'RailsKvsDriver example' do |driver_class, driver_config|
40
40
  end
41
41
  end
42
42
 
43
- it 'call add_sorted_set' do
44
- driver_class::session(driver_config) do |kvs|
45
- expect{
46
- kvs.add_sorted_set('example', 'element', 1)
47
- }.to change{ kvs.get_sorted_set_keys.length }.by(1)
48
- end
49
- end
50
-
51
- it 'call count_sorted_set_member' do
52
- driver_class::session(driver_config) do |kvs|
53
- kvs.add_sorted_set('example', 'element1', 5)
54
- kvs.add_sorted_set('example', 'element2', 5)
55
- kvs.add_sorted_set('example', 'element3', 5)
56
- expect(kvs.count_sorted_set_member('example')).to eq(3)
57
- end
58
- end
59
-
60
43
  it 'call delete' do
61
44
  driver_class::session(driver_config) do |kvs|
62
45
  kvs['example'] = 'nico-nico'
@@ -78,6 +61,148 @@ shared_examples_for 'RailsKvsDriver example' do |driver_class, driver_config|
78
61
  end
79
62
  end
80
63
 
64
+ it 'call has_key?' do
65
+ driver_class::session(driver_config) do |kvs|
66
+ kvs['example'] = 'nico-nico'
67
+
68
+ expect(kvs.has_key?('example')).to be_true
69
+ expect(kvs.has_key?('nothing key')).to be_false
70
+ end
71
+ end
72
+
73
+ it 'call keys' do
74
+ driver_class::session(driver_config) do |kvs|
75
+ kvs['example0'] = 'nico-nico'
76
+ kvs['example1'] = 'nico-nico'
77
+ kvs.add_sorted_set('example_ss', 'element5', 5)
78
+
79
+ expect(kvs.keys.length).to eq(2)
80
+ end
81
+ end
82
+
83
+
84
+ #--------------------
85
+ # list (same as list of redis. refer to redis.)
86
+ #--------------------
87
+
88
+ it 'call count_list_value' do
89
+ driver_class::session(driver_config) do |kvs|
90
+ kvs.push_list_last('anime', 'nyaruko')
91
+ kvs.push_list_last('anime', 'kinmoza')
92
+
93
+ expect(kvs.count_list_value('anime')).to eq(2)
94
+ end
95
+ end
96
+
97
+ it 'call delete_list_value' do
98
+ driver_class::session(driver_config) do |kvs|
99
+ kvs.push_list_last('anime', 'nyaruko')
100
+ kvs.push_list_last('anime', 'kinmoza')
101
+ kvs.push_list_last('anime', 'hoge')
102
+
103
+ expect{
104
+ kvs.delete_list_value('anime', 'hoge')
105
+ }.to change{ kvs.count_list_value('anime') }.by(-1)
106
+ expect(kvs.get_list_values('anime').include?('hoge')).to be_false
107
+ end
108
+ end
109
+
110
+ it 'call delete_list_value_at 0' do
111
+ driver_class::session(driver_config) do |kvs|
112
+ kvs.push_list_last('anime', 'hoge')
113
+ kvs.push_list_last('anime', 'nyaruko')
114
+ kvs.push_list_last('anime', 'kinmoza')
115
+
116
+ expect{
117
+ kvs.delete_list_value_at('anime', 0)
118
+ }.to change{ kvs.count_list_value('anime') }.by(-1)
119
+ expect(kvs.get_list_values('anime').include?('hoge')).to be_false
120
+ end
121
+ end
122
+
123
+ it 'call get_list_keys' do
124
+ driver_class::session(driver_config) do |kvs|
125
+ kvs.push_list_last('anime', 'nyaruko')
126
+ kvs.push_list_last('anime', 'kinmoza')
127
+
128
+ expect(kvs.get_list_keys.length).to eq(1)
129
+ end
130
+ end
131
+
132
+ it 'call get_list_value' do
133
+ driver_class::session(driver_config) do |kvs|
134
+ kvs.push_list_last('anime', 'nyaruko')
135
+ kvs.push_list_last('anime', 'kinmoza')
136
+
137
+ expect(kvs.get_list_value('anime', 0)).to eq('nyaruko')
138
+ end
139
+ end
140
+
141
+ it 'call get_list_values' do
142
+ driver_class::session(driver_config) do |kvs|
143
+ kvs.push_list_last('anime', 'nyaruko')
144
+ kvs.push_list_last('anime', 'kinmoza')
145
+
146
+ expect(kvs.get_list_values('anime').length).to eq(2)
147
+ end
148
+ end
149
+
150
+ it 'call push_list_*' do
151
+ driver_class::session(driver_config) do |kvs|
152
+ kvs.push_list_last('anime', 'nyaruko')
153
+ kvs.push_list_last('anime', 'kinmoza')
154
+ kvs.push_list_first('anime', 'inuhasa')
155
+
156
+ expect(kvs.get_list_value('anime', 2)).to eq('kinmoza')
157
+ expect(kvs.get_list_value('anime', 0)).to eq('inuhasa')
158
+ end
159
+ end
160
+
161
+ it 'call pop_list_*' do
162
+ driver_class::session(driver_config) do |kvs|
163
+ kvs.push_list_last('anime', 'nyaruko')
164
+ kvs.push_list_last('anime', 'kinmoza')
165
+ kvs.push_list_first('anime', 'inuhasa')
166
+
167
+ expect{
168
+ expect(kvs.pop_list_first('anime')).to eq('inuhasa')
169
+ expect(kvs.pop_list_last('anime')).to eq('kinmoza')
170
+ }.to change{ kvs.count_list_value('anime') }.by(-2)
171
+ end
172
+ end
173
+
174
+ it 'call pop_list_*' do
175
+ driver_class::session(driver_config) do |kvs|
176
+ kvs.push_list_last('anime', 'nyaruko')
177
+ kvs.push_list_last('anime', 'kinmoza')
178
+
179
+ kvs.set_list_value('anime', 1, 'inuhasa')
180
+
181
+ expect(kvs.get_list_value('anime', 1)).to eq('inuhasa')
182
+ end
183
+ end
184
+
185
+ #--------------------
186
+ # sorted set (same as sorted set of redis. refer to redis.)
187
+ #--------------------
188
+
189
+ it 'call add_sorted_set' do
190
+ driver_class::session(driver_config) do |kvs|
191
+ expect{
192
+ kvs.add_sorted_set('example', 'element', 1)
193
+ }.to change{ kvs.get_sorted_set_keys.length }.by(1)
194
+ end
195
+ end
196
+
197
+ it 'call count_sorted_set_member' do
198
+ driver_class::session(driver_config) do |kvs|
199
+ kvs.add_sorted_set('example', 'element1', 5)
200
+ kvs.add_sorted_set('example', 'element2', 5)
201
+ kvs.add_sorted_set('example', 'element3', 5)
202
+ expect(kvs.count_sorted_set_member('example')).to eq(3)
203
+ end
204
+ end
205
+
81
206
  it 'call get_sorted_set' do
82
207
  driver_class::session(driver_config) do |kvs|
83
208
  kvs.add_sorted_set('example', 'element1', 1)
@@ -121,25 +246,6 @@ shared_examples_for 'RailsKvsDriver example' do |driver_class, driver_config|
121
246
  end
122
247
  end
123
248
 
124
- it 'call has_key?' do
125
- driver_class::session(driver_config) do |kvs|
126
- kvs['example'] = 'nico-nico'
127
-
128
- expect(kvs.has_key?('example')).to be_true
129
- expect(kvs.has_key?('nothing key')).to be_false
130
- end
131
- end
132
-
133
- it 'call keys' do
134
- driver_class::session(driver_config) do |kvs|
135
- kvs['example0'] = 'nico-nico'
136
- kvs['example1'] = 'nico-nico'
137
- kvs.add_sorted_set('example_ss', 'element5', 5)
138
-
139
- expect(kvs.keys.length).to eq(2)
140
- end
141
- end
142
-
143
249
  it 'call remove_sorted_set' do
144
250
  driver_class::session(driver_config) do |kvs|
145
251
  kvs.add_sorted_set('example', 'element', 1)
@@ -190,6 +296,128 @@ shared_examples_for 'RailsKvsDriver example' do |driver_class, driver_config|
190
296
  end
191
297
  end
192
298
 
299
+ context 'lists' do
300
+ before(:each) do
301
+ driver_class::session(driver_config) do |kvs|
302
+ kvs.push_list_last('anime', 'nyaruko')
303
+ kvs.push_list_last('anime', 'haganai')
304
+ end
305
+ end
306
+
307
+ it 'call[]' do
308
+ driver_class::session(driver_config) do |kvs|
309
+ expect(kvs.lists['anime'].instance_of?(RailsKvsDriver::Lists::List)).to be_true
310
+ end
311
+ end
312
+
313
+ it 'call []=' do
314
+ driver_class::session(driver_config) do |kvs|
315
+
316
+ expect{
317
+ kvs.lists['fruit'] = [:apple, :orange]
318
+ }.to change{
319
+ kvs.lists.length
320
+ }.by(1)
321
+
322
+ expect(kvs.lists['fruit'].length).to eq(2)
323
+ end
324
+ end
325
+
326
+ it 'call delete' do
327
+ driver_class::session(driver_config) do |kvs|
328
+ expect{kvs.lists.delete('anime')}.to change{kvs.lists.length}.by(-1)
329
+ end
330
+ end
331
+
332
+ it 'call each' do
333
+ driver_class::session(driver_config) do |kvs|
334
+ kvs.lists.each do |key|
335
+ expect(key).to eq('anime')
336
+ end
337
+ end
338
+ end
339
+
340
+ it 'call keys' do
341
+ driver_class::session(driver_config) do |kvs|
342
+ expect(kvs.lists.keys.length).to eq(1)
343
+ end
344
+ end
345
+
346
+ it 'call length' do
347
+ driver_class::session(driver_config) do |kvs|
348
+ expect(kvs.lists.length).to eq(1)
349
+ end
350
+ end
351
+
352
+ context 'lists' do
353
+ it 'call[]' do
354
+ driver_class::session(driver_config) do |kvs|
355
+ expect(kvs.lists['anime'][0]).to eq('nyaruko')
356
+ end
357
+ end
358
+
359
+ it 'call []=' do
360
+ driver_class::session(driver_config) do |kvs|
361
+ kvs.lists['anime'][1] = 'inuhasa'
362
+ expect(kvs.lists['anime'][1]).to eq('inuhasa')
363
+ end
364
+ end
365
+
366
+ it 'call delete' do
367
+ driver_class::session(driver_config) do |kvs|
368
+ expect{
369
+ kvs.lists['anime'].delete('nyaruko')
370
+ }.to change{ kvs.lists['anime'].length }.by(-1)
371
+ expect(kvs.lists['anime'][0]).to eq('haganai')
372
+ end
373
+ end
374
+
375
+ it 'call delete_at' do
376
+ driver_class::session(driver_config) do |kvs|
377
+ expect{
378
+ kvs.lists['anime'].delete_at(1)
379
+ }.to change{ kvs.lists['anime'].length }.by(-1)
380
+ expect(kvs.lists['anime'][0]).to eq('nyaruko')
381
+ end
382
+ end
383
+
384
+ it 'call each' do
385
+ driver_class::session(driver_config) do |kvs|
386
+ kvs.lists['anime'].each do |index, value|
387
+ expect(kvs.lists['anime'][index]).to eq(value)
388
+ end
389
+ end
390
+ end
391
+
392
+ it 'call each' do
393
+ driver_class::session(driver_config) do |kvs|
394
+ kvs.lists['anime'].each do |index, value|
395
+ expect(kvs.lists['anime'][index]).to eq(value)
396
+ end
397
+ end
398
+ end
399
+
400
+ it 'call include?' do
401
+ driver_class::session(driver_config) do |kvs|
402
+ expect(kvs.lists['anime'].include?('nyaruko')).to be_true
403
+ expect(kvs.lists['anime'].include?('konnanonaiyo')).to be_false
404
+ end
405
+ end
406
+
407
+ it 'call push pop' do
408
+ driver_class::session(driver_config) do |kvs|
409
+ kvs.lists['anime'].push_first('rozen')
410
+ kvs.lists['anime'].push_last('maiden')
411
+
412
+ expect(kvs.lists['anime'].pop_first).to eq('rozen')
413
+ expect(kvs.lists['anime'].pop_last).to eq('maiden')
414
+ end
415
+ end
416
+ end
417
+
418
+ end
419
+
420
+
193
421
  context 'sorted_sets' do
194
422
  before(:each) do
195
423
  driver_class::session(driver_config) do |kvs|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_kvs_driver-redis_driver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - alfa-jpn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-13 00:00:00.000000000 Z
11
+ date: 2013-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -86,14 +86,14 @@ dependencies:
86
86
  requirements:
87
87
  - - '='
88
88
  - !ruby/object:Gem::Version
89
- version: 0.2.1
89
+ version: 0.3.1
90
90
  type: :runtime
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - '='
95
95
  - !ruby/object:Gem::Version
96
- version: 0.2.1
96
+ version: 0.3.1
97
97
  description: rails_kvs_driver is available to use namespace and connection pool and
98
98
  ruby-like methods of kvs.
99
99
  email: