collectr 0.0.7 → 1.0.0
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.
- checksums.yaml +4 -4
- data/.travis.yml +3 -0
- data/README.md +44 -5
- data/lib/collectr/memory/memory_hash.rb +4 -0
- data/lib/collectr/null/null_hash.rb +4 -0
- data/lib/collectr/redis/redis_array.rb +0 -1
- data/lib/collectr/redis/redis_base.rb +2 -0
- data/lib/collectr/redis/redis_hash.rb +4 -18
- data/lib/collectr/redis/redis_hash_expiry.rb +4 -2
- data/lib/collectr/version.rb +1 -1
- data/spec/hash_spec_helper.rb +5 -16
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd10b772686cd8a80b70a85a0d5c523c9fdcb4b6
|
4
|
+
data.tar.gz: da8f0bdf753af2d4abd1afc207a22b19e3bd9f79
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4c21545dce0ae951df5e1e83df1d7f99d1b5e172f2e02223145efe1d398cfda6234555f387df1356535b1a0b4347bf5334aca7b5f071d21b8c7f5d31ca6e3e14
|
7
|
+
data.tar.gz: 5de5acf3777e51f00de689fa38ad2e896519a23e1f3ad75ed81bd8401b3faae81ca37f839bd1f288f162158c74b83d49e851ddfb47e50399c0b478577323827a
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,11 +1,9 @@
|
|
1
|
-
[](https://travis-ci.org/brianmd/collectr) [](http://badge.fury.io/rb/collectr) [](https://travis-ci.org/brianmd/collectr) [](http://badge.fury.io/rb/collectr) [](https://coveralls.io/github/brianmd/collectr?branch=master)
|
2
2
|
|
3
3
|
# Collectr
|
4
4
|
|
5
5
|
Abstraction for thread-safe collections (array, hash, set, bag).
|
6
6
|
|
7
|
-
Once it is fleshed out, will include complete abstractions for redis and memory.
|
8
|
-
|
9
7
|
## Installation
|
10
8
|
|
11
9
|
Add this line to your application's Gemfile:
|
@@ -16,7 +14,7 @@ gem 'collectr'
|
|
16
14
|
|
17
15
|
And then execute:
|
18
16
|
|
19
|
-
$ bundle
|
17
|
+
$ bundle install
|
20
18
|
|
21
19
|
Or install it yourself as:
|
22
20
|
|
@@ -24,7 +22,48 @@ Or install it yourself as:
|
|
24
22
|
|
25
23
|
## Usage
|
26
24
|
|
27
|
-
|
25
|
+
```ruby
|
26
|
+
require 'collectr/redis/redis_hash'
|
27
|
+
x = Collectr::RedisHash.new('example')
|
28
|
+
x[3] = 7
|
29
|
+
x[3]
|
30
|
+
=> 7
|
31
|
+
x.fetch(3) { 88 }
|
32
|
+
=> 7
|
33
|
+
x.fetch(:not_found) { 88 }
|
34
|
+
=> 88
|
35
|
+
```
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
require 'collectr/redis/redis_hash_expiry'
|
39
|
+
x = Collectr::RedisHashExpiry.new('example', expires_in: 60)
|
40
|
+
x['purple'] = :blue
|
41
|
+
x['purple']
|
42
|
+
=> "blue"
|
43
|
+
sleep 61
|
44
|
+
x['purple']
|
45
|
+
=> nil
|
46
|
+
x.write('nine', 9, expires_in: 2)
|
47
|
+
x['nine']
|
48
|
+
```
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
require 'collectr/memory/memory_hash'
|
52
|
+
x = Collectr::MemoryHash.new('example')
|
53
|
+
x[:a] = 'abc'
|
54
|
+
x[:a]
|
55
|
+
=> "abc"
|
56
|
+
```
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
require 'collectr/redis/redis_array'
|
60
|
+
array = Collectr::RedisArray.new('arr', max_size: 3)
|
61
|
+
array.clear
|
62
|
+
array << 'a'
|
63
|
+
array << 'b'
|
64
|
+
array.to_a
|
65
|
+
=> ["a", "b"]
|
66
|
+
```
|
28
67
|
|
29
68
|
## Contributing
|
30
69
|
|
@@ -1,25 +1,7 @@
|
|
1
|
-
require 'redis'
|
2
1
|
require 'json'
|
3
2
|
|
4
3
|
require_relative 'redis_base'
|
5
4
|
|
6
|
-
# __FILE__.has_spec 'common/spec/classes/collection/hash_spec'
|
7
|
-
|
8
|
-
|
9
|
-
# class Redis
|
10
|
-
# def fetch(key, options={})
|
11
|
-
# val = self[key]
|
12
|
-
# if val.nil?
|
13
|
-
# begin
|
14
|
-
# val = yield key
|
15
|
-
# self[key] = val
|
16
|
-
# end
|
17
|
-
# end
|
18
|
-
# val
|
19
|
-
# end
|
20
|
-
# end
|
21
|
-
|
22
|
-
|
23
5
|
module Collectr
|
24
6
|
class RedisHash < RedisBase
|
25
7
|
attr_reader :store
|
@@ -91,6 +73,10 @@ module Collectr
|
|
91
73
|
@store.hkeys(@title).collect{ |key| deserialize key }
|
92
74
|
end
|
93
75
|
|
76
|
+
def values
|
77
|
+
keys.collect{ |key| self[key] }
|
78
|
+
end
|
79
|
+
|
94
80
|
def to_hash
|
95
81
|
hash = {}
|
96
82
|
@store.hgetall(@title).each do |key, val|
|
@@ -1,4 +1,3 @@
|
|
1
|
-
require 'redis'
|
2
1
|
require 'json'
|
3
2
|
|
4
3
|
require_relative 'redis_base'
|
@@ -46,7 +45,6 @@ module Collectr
|
|
46
45
|
end
|
47
46
|
|
48
47
|
def fetch(key, options={})
|
49
|
-
expiration = options.fetch(:expires_in) { @default_expires_in }
|
50
48
|
result = self[key]
|
51
49
|
if result.nil?
|
52
50
|
return nil if has_key?(key)
|
@@ -94,6 +92,10 @@ module Collectr
|
|
94
92
|
@store.keys(key_prefix).collect{ |key| dekey key }
|
95
93
|
end
|
96
94
|
|
95
|
+
def values
|
96
|
+
keys.collect{ |key| self[key] }
|
97
|
+
end
|
98
|
+
|
97
99
|
def to_hash
|
98
100
|
hash = {}
|
99
101
|
keys.each do |key|
|
data/lib/collectr/version.rb
CHANGED
data/spec/hash_spec_helper.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
require 'json'
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
|
+
if redis_exists?
|
5
|
+
warn Redis.current.flushall
|
6
|
+
end
|
7
|
+
|
4
8
|
shared_examples 'a hash' do
|
5
9
|
context 'when keys and/or values may be other than strings' do
|
6
10
|
subject(:collection) { described_class.new 'example' }
|
@@ -25,6 +29,7 @@ shared_examples 'a hash' do
|
|
25
29
|
|
26
30
|
its(:size) { should eq(3) }
|
27
31
|
it { expect(collection.keys.to_set).to eq([1, 'b2', [1,2]].to_set) }
|
32
|
+
it { expect(collection.values.to_set).to eq(["aa1", nil, 3].to_set) }
|
28
33
|
its(:to_hash) { should eq({1=>'aa1', 'b2'=>nil, [1,2]=>3}) }
|
29
34
|
it { expect(collection.fetch(1)).to eq('aa1') }
|
30
35
|
it { expect(collection.fetch('b2')).to eq(nil) }
|
@@ -55,21 +60,5 @@ shared_examples 'a hash' do
|
|
55
60
|
# it 'string key is distinct from symbol key' do expect(collection.fetch('x')).to eq(7) end
|
56
61
|
it 'new value is retained' do collection['x'] = 9 ; expect(collection.fetch('x')).to eq(9) end
|
57
62
|
end
|
58
|
-
|
59
|
-
# context 'when expires' do
|
60
|
-
# it 'expires on time'
|
61
|
-
# end
|
62
63
|
end
|
63
64
|
|
64
|
-
if redis_exists? and false
|
65
|
-
describe Collectr::RedisHash do
|
66
|
-
it_behaves_like 'a hash'
|
67
|
-
end
|
68
|
-
|
69
|
-
describe Collectr::RedisHashExpiry do
|
70
|
-
it_behaves_like 'a hash'
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
# warn Redis.current.flushall
|
75
|
-
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: collectr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Murphy-Dye
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|