norton 0.0.19 → 0.0.20
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/Rakefile +6 -4
- data/lib/norton/hash.rb +27 -0
- data/lib/norton/objects/hash_map.rb +68 -0
- data/lib/norton/version.rb +1 -1
- data/lib/norton.rb +4 -0
- data/spec/norton/hash_spec.rb +34 -0
- data/spec/norton/objects/hash_map_spec.rb +115 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8de7d288d5c2e1b54399508842ffc761063d2bbf
|
4
|
+
data.tar.gz: b2cb45b37ca12fa36e8adf03455220fba96153ea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: afeb424fb75456de2a6dafa5eb1285dc6bdf6e6fbdca78e1b1c5e8bb819a60da37957a108d638747030c134c94f71aef55349533bd1cde6bd3fa73494dfcff43
|
7
|
+
data.tar.gz: 38a31ffb7c30e55f60061abd6e49be42955ffa4a9cc8b59b3a7bcff8aa68c49ad03aefd145808ee6ef310f2ffc83144005b145d5484bde8e74632c2ab5adb46f
|
data/Rakefile
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
2
|
require 'rake/testtask'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
end
|
4
|
+
desc "run all the specs"
|
5
|
+
task :test do
|
6
|
+
sh "rspec spec"
|
7
|
+
end
|
8
|
+
task :default => :test
|
9
|
+
task :spec => :test
|
data/lib/norton/hash.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
module Norton
|
2
|
+
module Hash
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
def hash_map(name)
|
7
|
+
define_method(name) do
|
8
|
+
instance_variable_get("@#{name}") ||
|
9
|
+
instance_variable_set("@#{name}",
|
10
|
+
Norton::Objects::HashMap.new(norton_field_key(name))
|
11
|
+
)
|
12
|
+
end
|
13
|
+
|
14
|
+
after_destroy { send(name).clear } if respond_to?(:after_destroy)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def norton_field_key(name)
|
19
|
+
if id.nil?
|
20
|
+
raise NilObjectId,
|
21
|
+
"Attempt to address norton field :#{name} on class #{self.class.name} with nil id"
|
22
|
+
end
|
23
|
+
|
24
|
+
"#{self.class.name.tableize}:#{id}:#{name}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module Norton
|
2
|
+
module Objects
|
3
|
+
class HashMap
|
4
|
+
attr_reader :key
|
5
|
+
|
6
|
+
def initialize(key)
|
7
|
+
@key = key
|
8
|
+
end
|
9
|
+
|
10
|
+
# Redis: HSET
|
11
|
+
def hset(field, value)
|
12
|
+
Norton.redis.with { |conn| conn.hset(key, field, value) }
|
13
|
+
end
|
14
|
+
alias_method :[]=, :hset
|
15
|
+
|
16
|
+
# Redis: HGET
|
17
|
+
def hget(field)
|
18
|
+
Norton.redis.with { |conn| conn.hget(key, field) }
|
19
|
+
end
|
20
|
+
alias_method :get, :hget
|
21
|
+
alias_method :[], :hget
|
22
|
+
|
23
|
+
# Redis: HMGET
|
24
|
+
def hmget(*field)
|
25
|
+
Norton.redis.with { |conn| conn.hmget(key, field) }
|
26
|
+
end
|
27
|
+
alias_method :mget, :hmget
|
28
|
+
|
29
|
+
# Redis: HDEL
|
30
|
+
def hdel(*field)
|
31
|
+
Norton.redis.with { |conn| conn.hdel(key, field) }
|
32
|
+
end
|
33
|
+
alias_method :delete, :hdel
|
34
|
+
|
35
|
+
# Redis: HINCRBY
|
36
|
+
def hincrby(field, by = 1)
|
37
|
+
Norton.redis.with { |conn| conn.hincrby(key, field, by) }
|
38
|
+
end
|
39
|
+
alias_method :incr, :hincrby
|
40
|
+
|
41
|
+
# Redis: HINCRBY
|
42
|
+
def hdecrby(field, by = 1)
|
43
|
+
hincrby(field, -by)
|
44
|
+
end
|
45
|
+
alias_method :decr, :hdecrby
|
46
|
+
|
47
|
+
# Redis: HEXISTS
|
48
|
+
def hexists(field)
|
49
|
+
Norton.redis.with { |conn| conn.hexists(key, field) }
|
50
|
+
end
|
51
|
+
alias_method :include?, :hexists
|
52
|
+
alias_method :has_key?, :hexists
|
53
|
+
alias_method :key?, :hexists
|
54
|
+
alias_method :member?, :hexists
|
55
|
+
|
56
|
+
# Redis: HEXISTS
|
57
|
+
def hkeys
|
58
|
+
Norton.redis.with { |conn| conn.hkeys(key) }
|
59
|
+
end
|
60
|
+
alias_method :keys, :hkeys
|
61
|
+
|
62
|
+
# Redis: DEL
|
63
|
+
def clear
|
64
|
+
Norton.redis.with { |conn| conn.del(key) }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/lib/norton/version.rb
CHANGED
data/lib/norton.rb
CHANGED
@@ -7,8 +7,12 @@ require "norton/timestamp"
|
|
7
7
|
require "norton/counter"
|
8
8
|
require "norton/timed_value"
|
9
9
|
require "norton/helper"
|
10
|
+
require "norton/objects/hash_map"
|
11
|
+
require "norton/hash"
|
10
12
|
|
11
13
|
module Norton
|
14
|
+
class NilObjectId < StandardError; end
|
15
|
+
|
12
16
|
class << self
|
13
17
|
attr_accessor :redis
|
14
18
|
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class HashObject
|
4
|
+
include Norton::Hash
|
5
|
+
|
6
|
+
hash_map :profile
|
7
|
+
|
8
|
+
def id
|
9
|
+
@id ||= 99
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe Norton::Hash do
|
14
|
+
describe "#norton_field_key" do
|
15
|
+
it "generates the correct field key" do
|
16
|
+
object = HashObject.new
|
17
|
+
expect(object.norton_field_key(:profile)).to eq("hash_objects:99:profile")
|
18
|
+
end
|
19
|
+
|
20
|
+
it "raises NilObjectId if the object id is nil" do
|
21
|
+
object = HashObject.new
|
22
|
+
allow(object).to receive(:id) { nil }
|
23
|
+
|
24
|
+
expect { object.norton_field_key(:profile) }.to raise_error(Norton::NilObjectId)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#hash_map" do
|
29
|
+
it "sets a instance variable" do
|
30
|
+
object = HashObject.new
|
31
|
+
expect(object.profile).to be_a(Norton::Objects::HashMap)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Norton::Objects::HashMap do
|
4
|
+
describe "#hset" do
|
5
|
+
it "saves the value of the field in redis" do
|
6
|
+
hash_map = Norton::Objects::HashMap.new("users:99:profile")
|
7
|
+
hash_map.hset(:name, "Bob")
|
8
|
+
expect(Norton.redis.with { |conn| conn.hget(hash_map.key, :name) }).to eq("Bob")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "#hget" do
|
13
|
+
it "returns the value of the field from redis" do
|
14
|
+
hash_map = Norton::Objects::HashMap.new("users:99:profile")
|
15
|
+
hash_map.hset(:name, "Bob")
|
16
|
+
|
17
|
+
expect(hash_map.hget(:name)).to eq("Bob")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#hdel" do
|
22
|
+
it "deletes the field from redis" do
|
23
|
+
hash_map = Norton::Objects::HashMap.new("users:99:profile")
|
24
|
+
hash_map.hset(:name, "Bob")
|
25
|
+
|
26
|
+
hash_map.hdel(:name)
|
27
|
+
|
28
|
+
expect(hash_map.hget(:name)).to be(nil)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "deletes multiple fields from redis" do
|
32
|
+
hash_map = Norton::Objects::HashMap.new("users:99:profile")
|
33
|
+
hash_map.hset(:name, "Bob")
|
34
|
+
hash_map.hset(:age, 21)
|
35
|
+
|
36
|
+
hash_map.hdel(:name, :age)
|
37
|
+
|
38
|
+
expect(hash_map.hget(:name)).to be(nil)
|
39
|
+
expect(hash_map.hget(:age)).to be(nil)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "#hmget" do
|
44
|
+
it "returns the values associated with the specified fields in the hash" do
|
45
|
+
hash_map = Norton::Objects::HashMap.new("users:99:profile")
|
46
|
+
hash_map.hset(:name, "Bob")
|
47
|
+
hash_map.hset(:age, 21)
|
48
|
+
|
49
|
+
expect(hash_map.hmget(:name, :age)).to eq(["Bob", "21"])
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "#hincrby" do
|
54
|
+
it "increments value by integer at field" do
|
55
|
+
hash_map = Norton::Objects::HashMap.new("users:99:profile")
|
56
|
+
hash_map.hset(:age, 21)
|
57
|
+
|
58
|
+
hash_map.hincrby(:age, 2)
|
59
|
+
expect(hash_map.hget(:age)).to eq("23")
|
60
|
+
end
|
61
|
+
|
62
|
+
it "increments value by 1" do
|
63
|
+
hash_map = Norton::Objects::HashMap.new("users:99:profile")
|
64
|
+
hash_map.hset(:age, 21)
|
65
|
+
|
66
|
+
hash_map.hincrby(:age)
|
67
|
+
expect(hash_map.hget(:age)).to eq("22")
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "#hdecrby" do
|
72
|
+
it "decrements value by integer at field" do
|
73
|
+
hash_map = Norton::Objects::HashMap.new("users:99:profile")
|
74
|
+
hash_map.hset(:age, 21)
|
75
|
+
|
76
|
+
hash_map.hdecrby(:age, 2)
|
77
|
+
expect(hash_map.hget(:age)).to eq("19")
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "#hexists" do
|
82
|
+
it "returns true if the field exists in redis" do
|
83
|
+
hash_map = Norton::Objects::HashMap.new("users:99:profile")
|
84
|
+
hash_map.hset(:name, "Bob")
|
85
|
+
|
86
|
+
expect(hash_map.hexists(:name)).to be(true)
|
87
|
+
end
|
88
|
+
|
89
|
+
it "returns false if the field doesn't exist in redis" do
|
90
|
+
hash_map = Norton::Objects::HashMap.new("users:99:profile")
|
91
|
+
|
92
|
+
expect(hash_map.hexists(:name)).to be(false)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "#hkeys" do
|
97
|
+
it "returns all keys in a hash" do
|
98
|
+
hash_map = Norton::Objects::HashMap.new("users:99:profile")
|
99
|
+
hash_map.hset(:name, "Bob")
|
100
|
+
hash_map.hset(:age, 21)
|
101
|
+
|
102
|
+
expect(hash_map.hkeys).to match_array(["name", "age"])
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe "#clear" do
|
107
|
+
it "deletes the hash from redis" do
|
108
|
+
hash_map = Norton::Objects::HashMap.new("users:99:profile")
|
109
|
+
hash_map.hset(:name, "Bob")
|
110
|
+
|
111
|
+
hash_map.clear
|
112
|
+
expect(Norton.redis.with { |conn| conn.exists(hash_map.key) }).to eq(false)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: norton
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.20
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Larry Zhao
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-02-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|
@@ -180,13 +180,17 @@ files:
|
|
180
180
|
- Rakefile
|
181
181
|
- lib/norton.rb
|
182
182
|
- lib/norton/counter.rb
|
183
|
+
- lib/norton/hash.rb
|
183
184
|
- lib/norton/helper.rb
|
185
|
+
- lib/norton/objects/hash_map.rb
|
184
186
|
- lib/norton/timed_value.rb
|
185
187
|
- lib/norton/timestamp.rb
|
186
188
|
- lib/norton/version.rb
|
187
189
|
- norton.gemspec
|
188
190
|
- spec/norton/counter_spec.rb
|
191
|
+
- spec/norton/hash_spec.rb
|
189
192
|
- spec/norton/helper_spec.rb
|
193
|
+
- spec/norton/objects/hash_map_spec.rb
|
190
194
|
- spec/norton/timestamp_spec.rb
|
191
195
|
- spec/norton_spec.rb
|
192
196
|
- spec/spec_helper.rb
|
@@ -217,7 +221,9 @@ summary: Provide simple helpers on persist values in redis for performance. Work
|
|
217
221
|
with ActiveRecord.
|
218
222
|
test_files:
|
219
223
|
- spec/norton/counter_spec.rb
|
224
|
+
- spec/norton/hash_spec.rb
|
220
225
|
- spec/norton/helper_spec.rb
|
226
|
+
- spec/norton/objects/hash_map_spec.rb
|
221
227
|
- spec/norton/timestamp_spec.rb
|
222
228
|
- spec/norton_spec.rb
|
223
229
|
- spec/spec_helper.rb
|