norton 0.0.22 → 0.0.24
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/lib/norton/counter.rb +13 -9
- data/lib/norton/hash_map.rb +5 -10
- data/lib/norton/helper.rb +40 -5
- data/lib/norton/timed_value.rb +4 -4
- data/lib/norton/timestamp.rb +15 -4
- data/lib/norton/version.rb +1 -1
- data/lib/norton.rb +1 -1
- data/spec/norton/hash_map_spec.rb +2 -9
- data/spec/norton/helper_spec.rb +43 -6
- data/spec/norton/timestamp_spec.rb +5 -0
- 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: 47f5a6a213e297cec27645a41ff453ae1b7d4bfd
|
4
|
+
data.tar.gz: 37b3492ec453174d6bb8993ddf835ae2af2a43b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd5c343f935788a653d31e65b8f94107353df0121ced3610cc65613782fdc462450b05437a06c613f1dfd6c84dc6941dcf90119ccb66f0e1dbd81ce12ce6ca47
|
7
|
+
data.tar.gz: 12969d600acc46904a4680e49cf62f3acaff0243970449358d98619a9cf4e9e08f441f6c4cb86ddb057c81b56312f9cea06780e7b51bfa8ebde631cd5ce29a7f
|
data/lib/norton/counter.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
module Norton
|
2
2
|
module Counter
|
3
|
-
extend ActiveSupport::Concern
|
3
|
+
extend ::ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
include Norton::Helper
|
7
|
+
end
|
4
8
|
|
5
9
|
module ClassMethods
|
6
10
|
#
|
@@ -13,37 +17,37 @@ module Norton
|
|
13
17
|
def counter(name, options={}, &blk)
|
14
18
|
define_method(name) do
|
15
19
|
Norton.redis.with do |conn|
|
16
|
-
conn.get(
|
20
|
+
conn.get(self.norton_redis_key(name)).try(:to_i) || 0
|
17
21
|
end
|
18
22
|
end
|
19
23
|
|
20
24
|
define_method("incr_#{name}") do
|
21
25
|
Norton.redis.with do |conn|
|
22
|
-
conn.incr(
|
26
|
+
conn.incr(self.norton_redis_key(name))
|
23
27
|
end
|
24
28
|
end
|
25
29
|
|
26
30
|
define_method("decr_#{name}") do
|
27
31
|
Norton.redis.with do |conn|
|
28
|
-
conn.decr(
|
32
|
+
conn.decr(self.norton_redis_key(name))
|
29
33
|
end
|
30
34
|
end
|
31
35
|
|
32
36
|
define_method("incr_#{name}_by") do |increment|
|
33
37
|
Norton.redis.with do |conn|
|
34
|
-
conn.incrby(
|
38
|
+
conn.incrby(self.norton_redis_key(name), increment)
|
35
39
|
end
|
36
40
|
end
|
37
41
|
|
38
42
|
define_method("decr_#{name}_by") do |decrement|
|
39
43
|
Norton.redis.with do |conn|
|
40
|
-
conn.decrby(
|
44
|
+
conn.decrby(self.norton_redis_key(name), decrement)
|
41
45
|
end
|
42
46
|
end
|
43
47
|
|
44
48
|
define_method("#{name}=") do |v|
|
45
49
|
Norton.redis.with do |conn|
|
46
|
-
conn.set(
|
50
|
+
conn.set(self.norton_redis_key(name), v)
|
47
51
|
end
|
48
52
|
end
|
49
53
|
|
@@ -51,13 +55,13 @@ module Norton
|
|
51
55
|
count = instance_eval(&blk)
|
52
56
|
|
53
57
|
Norton.redis.with do |conn|
|
54
|
-
conn.set(
|
58
|
+
conn.set(self.norton_redis_key(name), count)
|
55
59
|
end
|
56
60
|
end
|
57
61
|
|
58
62
|
define_method("remove_#{name}") do
|
59
63
|
Norton.redis.with do |conn|
|
60
|
-
conn.del(
|
64
|
+
conn.del(self.norton_redis_key(name))
|
61
65
|
end
|
62
66
|
end
|
63
67
|
send(:after_destroy, "remove_#{name}".to_sym) if respond_to? :after_destroy
|
data/lib/norton/hash_map.rb
CHANGED
@@ -2,26 +2,21 @@ module Norton
|
|
2
2
|
module HashMap
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
|
+
included do
|
6
|
+
include Norton::Helper
|
7
|
+
end
|
8
|
+
|
5
9
|
module ClassMethods
|
6
10
|
def hash_map(name)
|
7
11
|
define_method(name) do
|
8
12
|
instance_variable_get("@#{name}") ||
|
9
13
|
instance_variable_set("@#{name}",
|
10
|
-
Norton::Objects::Hash.new(
|
14
|
+
Norton::Objects::Hash.new(self.norton_redis_key(name))
|
11
15
|
)
|
12
16
|
end
|
13
17
|
|
14
18
|
after_destroy { send(name).clear } if respond_to?(:after_destroy)
|
15
19
|
end
|
16
20
|
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
21
|
end
|
27
22
|
end
|
data/lib/norton/helper.rb
CHANGED
@@ -2,6 +2,42 @@ module Norton
|
|
2
2
|
module Helper
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
|
+
#
|
6
|
+
# Prefix of Redis Key of Norton value, consists with Class name string in plural form
|
7
|
+
# and Instance id.
|
8
|
+
#
|
9
|
+
# Example:
|
10
|
+
#
|
11
|
+
# a User instance with id = 1 -> `users:1`
|
12
|
+
# a HolyLight::Spammer instance with id = 5 -> `holy_light/spammers:5`
|
13
|
+
#
|
14
|
+
#
|
15
|
+
# @return [String]
|
16
|
+
#
|
17
|
+
def norton_prefix
|
18
|
+
id = self.id
|
19
|
+
raise Norton::NilObjectId if id.nil?
|
20
|
+
klass = self.class.to_s.pluralize.underscore
|
21
|
+
"#{klass}:#{self.id}"
|
22
|
+
end
|
23
|
+
|
24
|
+
#
|
25
|
+
# Returns the final Redis Key of a certain Norton value, teh value will be saved in redis with
|
26
|
+
# this value.
|
27
|
+
#
|
28
|
+
# Example:
|
29
|
+
#
|
30
|
+
# a User instance with id = 1 defines a counter named `likes_count` -> users:1:likes_count
|
31
|
+
#
|
32
|
+
#
|
33
|
+
# @param [String] name
|
34
|
+
#
|
35
|
+
# @return [String]
|
36
|
+
#
|
37
|
+
def norton_redis_key(name)
|
38
|
+
"#{self.norton_prefix}:#{name}"
|
39
|
+
end
|
40
|
+
|
5
41
|
#
|
6
42
|
# 批量取出当前对象的多个 Norton value
|
7
43
|
#
|
@@ -9,21 +45,20 @@ module Norton
|
|
9
45
|
#
|
10
46
|
# @return [Hash]
|
11
47
|
#
|
12
|
-
def
|
48
|
+
def norton_vals(*names)
|
13
49
|
ret = {}
|
14
50
|
|
15
|
-
redis_keys =
|
51
|
+
redis_keys = names.map { |n| self.norton_redis_key(n) }
|
16
52
|
|
17
53
|
redis_values = Norton.redis.with do |conn|
|
18
54
|
conn.mget(redis_keys)
|
19
55
|
end
|
20
56
|
|
21
|
-
|
22
|
-
ret[
|
57
|
+
names.each_with_index do |n, index|
|
58
|
+
ret[n] = redis_values[index].try(:to_i)
|
23
59
|
end
|
24
60
|
|
25
61
|
ret
|
26
62
|
end
|
27
|
-
|
28
63
|
end
|
29
64
|
end
|
data/lib/norton/timed_value.rb
CHANGED
@@ -16,11 +16,11 @@ module Norton
|
|
16
16
|
# Define getter
|
17
17
|
define_method(name) do
|
18
18
|
Norton.redis.with do |conn|
|
19
|
-
v = conn.get(
|
19
|
+
v = conn.get(self.norton_redis_key(name))
|
20
20
|
|
21
21
|
if v.nil?
|
22
22
|
v = instance_eval(&blk)
|
23
|
-
conn.setex(
|
23
|
+
conn.setex(self.norton_redis_key(name), options[:ttl], v)
|
24
24
|
end
|
25
25
|
|
26
26
|
v
|
@@ -30,11 +30,11 @@ module Norton
|
|
30
30
|
define_method("reset_#{name}") do
|
31
31
|
Norton.redis.with do |conn|
|
32
32
|
v = instance_eval(&blk)
|
33
|
-
conn.setex(
|
33
|
+
conn.setex(self.norton_redis_key(name), options[:ttl], v)
|
34
34
|
v
|
35
35
|
end
|
36
36
|
end
|
37
37
|
end
|
38
38
|
end
|
39
39
|
end
|
40
|
-
end
|
40
|
+
end
|
data/lib/norton/timestamp.rb
CHANGED
@@ -2,6 +2,10 @@ module Norton
|
|
2
2
|
module Timestamp
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
|
+
included do
|
6
|
+
include Norton::Helper
|
7
|
+
end
|
8
|
+
|
5
9
|
module ClassMethods
|
6
10
|
#
|
7
11
|
# [timestamp Define a timestamp]
|
@@ -14,11 +18,11 @@ module Norton
|
|
14
18
|
ts = nil
|
15
19
|
|
16
20
|
Norton.redis.with do |conn|
|
17
|
-
ts = conn.get(
|
21
|
+
ts = conn.get(self.norton_redis_key(name)).try(:to_i)
|
18
22
|
|
19
23
|
if !options[:allow_nil] && ts.nil?
|
20
24
|
ts = Time.now.to_i
|
21
|
-
conn.set(
|
25
|
+
conn.set(self.norton_redis_key(name), ts)
|
22
26
|
end
|
23
27
|
|
24
28
|
ts
|
@@ -28,13 +32,20 @@ module Norton
|
|
28
32
|
define_method("touch_#{name}") do
|
29
33
|
Norton.redis.with do |conn|
|
30
34
|
if options[:digits].present? && options[:digits] == 13
|
31
|
-
conn.set(
|
35
|
+
conn.set(self.norton_redis_key(name), (Time.now.to_f * 1000).to_i)
|
32
36
|
else
|
33
|
-
conn.set(
|
37
|
+
conn.set(self.norton_redis_key(name), Time.now.to_i)
|
34
38
|
end
|
35
39
|
end
|
36
40
|
end
|
37
41
|
|
42
|
+
define_method("remove_#{name}") do
|
43
|
+
Norton.redis.with do |conn|
|
44
|
+
conn.del("#{self.class.to_s.pluralize.underscore}:#{self.id}:#{name}")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
send(:after_destroy, "remove_#{name}".to_sym) if respond_to? :after_destroy
|
48
|
+
|
38
49
|
# Add callback
|
39
50
|
if options[:touch_on].present?
|
40
51
|
options[:touch_on].each do |callback, condition|
|
data/lib/norton/version.rb
CHANGED
data/lib/norton.rb
CHANGED
@@ -3,10 +3,10 @@ require "connection_pool"
|
|
3
3
|
require "active_support/concern"
|
4
4
|
require "active_support/inflector"
|
5
5
|
require "norton/version"
|
6
|
+
require "norton/helper"
|
6
7
|
require "norton/timestamp"
|
7
8
|
require "norton/counter"
|
8
9
|
require "norton/timed_value"
|
9
|
-
require "norton/helper"
|
10
10
|
require "norton/objects/hash"
|
11
11
|
require "norton/hash_map"
|
12
12
|
|
@@ -11,17 +11,10 @@ class HashMapObject
|
|
11
11
|
end
|
12
12
|
|
13
13
|
describe Norton::HashMap do
|
14
|
-
describe "#
|
14
|
+
describe "#norton_redis_key" do
|
15
15
|
it "generates the correct field key" do
|
16
16
|
object = HashMapObject.new
|
17
|
-
expect(object.
|
18
|
-
end
|
19
|
-
|
20
|
-
it "raises NilObjectId if the object id is nil" do
|
21
|
-
object = HashMapObject.new
|
22
|
-
allow(object).to receive(:id) { nil }
|
23
|
-
|
24
|
-
expect { object.norton_field_key(:profile) }.to raise_error(Norton::NilObjectId)
|
17
|
+
expect(object.norton_redis_key(:profile)).to eq("hash_map_objects:99:profile")
|
25
18
|
end
|
26
19
|
end
|
27
20
|
|
data/spec/norton/helper_spec.rb
CHANGED
@@ -3,7 +3,6 @@ require 'spec_helper'
|
|
3
3
|
class Dummy
|
4
4
|
include Norton::Counter
|
5
5
|
include Norton::Timestamp
|
6
|
-
include Norton::Helper
|
7
6
|
|
8
7
|
counter :counter1
|
9
8
|
counter :counter2
|
@@ -16,12 +15,50 @@ class Dummy
|
|
16
15
|
end
|
17
16
|
end
|
18
17
|
|
18
|
+
module HolyLight
|
19
|
+
class Spammer
|
20
|
+
include Norton::Counter
|
21
|
+
|
22
|
+
def id
|
23
|
+
@id ||= Random.rand(10000)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
19
28
|
describe Norton::Helper do
|
20
|
-
describe "
|
21
|
-
it
|
29
|
+
describe "#norton_redis_key" do
|
30
|
+
it do
|
31
|
+
n = SecureRandom.hex(3)
|
32
|
+
|
33
|
+
dummy = Dummy.new
|
34
|
+
expect(dummy.norton_redis_key(n)).to eq("dummies:#{dummy.id}:#{n}")
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should raise `Norton::NilObjectId` if id returns nil" do
|
38
|
+
dummy = Dummy.new
|
39
|
+
allow(dummy).to receive(:id) { nil }
|
40
|
+
|
41
|
+
expect { dummy.norton_prefix }.to raise_error(Norton::NilObjectId)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "#norton_prefix" do
|
46
|
+
it "should return correctly for `Dummy`" do
|
47
|
+
dummy = Dummy.new
|
48
|
+
expect(dummy.norton_prefix).to eq("dummies:#{dummy.id}")
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should return correctly for `HolyLight::Spammer`" do
|
52
|
+
spammer = HolyLight::Spammer.new
|
53
|
+
expect(spammer.norton_prefix).to eq("holy_light/spammers:#{spammer.id}")
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "#norton_vals" do
|
58
|
+
it "should respond to `:norton_vals`" do
|
22
59
|
dummy = Dummy.new
|
23
60
|
|
24
|
-
expect(dummy).to respond_to(:
|
61
|
+
expect(dummy).to respond_to(:norton_vals)
|
25
62
|
end
|
26
63
|
|
27
64
|
it "should return the specific values" do
|
@@ -33,7 +70,7 @@ describe Norton::Helper do
|
|
33
70
|
|
34
71
|
dummy.touch_time1
|
35
72
|
|
36
|
-
values = dummy.
|
73
|
+
values = dummy.norton_vals(:counter1, :time1)
|
37
74
|
|
38
75
|
expect(values).to include(:counter1, :time1)
|
39
76
|
expect(values.size).to eq(2)
|
@@ -50,7 +87,7 @@ describe Norton::Helper do
|
|
50
87
|
|
51
88
|
dummy.touch_time1
|
52
89
|
|
53
|
-
values = dummy.
|
90
|
+
values = dummy.norton_vals(:counter1, :counter2, :time2)
|
54
91
|
|
55
92
|
expect(values).to include(:counter1, :counter2, :time2)
|
56
93
|
expect(values.size).to eq(3)
|
@@ -24,6 +24,11 @@ describe Norton::Timestamp do
|
|
24
24
|
expect(dummy).to respond_to(:born_at)
|
25
25
|
expect(dummy).to respond_to(:touch_born_at)
|
26
26
|
end
|
27
|
+
|
28
|
+
it "should create remove method" do
|
29
|
+
dummy = Dummy.new
|
30
|
+
expect(dummy).to respond_to(:remove_born_at)
|
31
|
+
end
|
27
32
|
end
|
28
33
|
|
29
34
|
describe "#touch_born_at" do
|
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.24
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Larry Zhao
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|