norton 0.0.18 → 0.0.19

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: c89415440b323b271c5897927a73e8d272a16561
4
- data.tar.gz: f4a82ea218cb03c067e222f3d7a3cdafaee9d9c0
3
+ metadata.gz: 6535a6a6aa0b6aafc903750f29736fdb867e436e
4
+ data.tar.gz: f6a843aaa3aac77be86602b45dc8f4181cb03a10
5
5
  SHA512:
6
- metadata.gz: cb0ba052090f2b4d687f1f11def1d3f9ada80ca4ac894427bffa94938b0485574a8a99c633e10fc69a803892b1359b379beea8531f879d3ac13fc32e7ab1e9c0
7
- data.tar.gz: a79e8d1b49987a9fe0dca3eb7e692fe20fa600b377e7df88d197edfbf17e2e2555cd6601b32f7e050e56b58b263d118275d2607cc295de367d3a057a1a5a5d6e
6
+ metadata.gz: ec3d694c9179a844a3f4b48915f7a62e32ce0d130d14b5e4685f08f8d87d8e9a05c4f68693af28d02dd07037af0b8b8e3b60f8de4d7c248fe7eea1b5e05acd1b
7
+ data.tar.gz: e86b51e2e964fbd2a646ef88cbbd59504506fe7bd553be74c688ffb8463a8ed1478038a7bee66d77ad671942ea73b0efab9f27546f9a601d841e1d58e98c6875
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format Fuubar
2
+ --color
@@ -29,6 +29,18 @@ module Norton
29
29
  end
30
30
  end
31
31
 
32
+ define_method("incr_#{name}_by") do |increment|
33
+ Norton.redis.with do |conn|
34
+ conn.incrby("#{self.class.to_s.pluralize.underscore}:#{self.id}:#{name}", increment)
35
+ end
36
+ end
37
+
38
+ define_method("decr_#{name}_by") do |decrement|
39
+ Norton.redis.with do |conn|
40
+ conn.decrby("#{self.class.to_s.pluralize.underscore}:#{self.id}:#{name}", decrement)
41
+ end
42
+ end
43
+
32
44
  define_method("#{name}=") do |v|
33
45
  Norton.redis.with do |conn|
34
46
  conn.set("#{self.class.to_s.pluralize.underscore}:#{self.id}:#{name}", v)
@@ -48,7 +60,7 @@ module Norton
48
60
  conn.del("#{self.class.to_s.pluralize.underscore}:#{self.id}:#{name}")
49
61
  end
50
62
  end
51
- send(:after_destroy, "remove_#{name}".to_sym)
63
+ send(:after_destroy, "remove_#{name}".to_sym) if respond_to? :after_destroy
52
64
 
53
65
  # Add Increment callback
54
66
  unless options[:incr].nil?
@@ -66,4 +78,4 @@ module Norton
66
78
  end
67
79
  end
68
80
  end
69
- end
81
+ end
@@ -0,0 +1,29 @@
1
+ module Norton
2
+ module Helper
3
+ extend ActiveSupport::Concern
4
+
5
+ #
6
+ # 批量取出当前对象的多个 Norton value
7
+ #
8
+ # @param [Array] *value_keys 直接传入需要的值的 key,例如: :key1, :key2, :key3
9
+ #
10
+ # @return [Hash]
11
+ #
12
+ def norton_values(*keys)
13
+ ret = {}
14
+
15
+ redis_keys = keys.map { |k| "#{self.class.to_s.pluralize.underscore}:#{self.id}:#{k}" }
16
+
17
+ redis_values = Norton.redis.with do |conn|
18
+ conn.mget(redis_keys)
19
+ end
20
+
21
+ keys.each_with_index do |key, index|
22
+ ret[key] = redis_values[index].try(:to_i)
23
+ end
24
+
25
+ ret
26
+ end
27
+
28
+ end
29
+ end
@@ -1,3 +1,3 @@
1
1
  module Norton
2
- VERSION = "0.0.18"
2
+ VERSION = "0.0.19"
3
3
  end
data/lib/norton.rb CHANGED
@@ -6,6 +6,7 @@ require "norton/version"
6
6
  require "norton/timestamp"
7
7
  require "norton/counter"
8
8
  require "norton/timed_value"
9
+ require "norton/helper"
9
10
 
10
11
  module Norton
11
12
  class << self
data/norton.gemspec CHANGED
@@ -24,7 +24,10 @@ Gem::Specification.new do |spec|
24
24
 
25
25
  spec.add_development_dependency "bundler", "~> 1.5"
26
26
  spec.add_development_dependency "rake"
27
- spec.add_development_dependency "minitest", "~> 5.5.1"
27
+ spec.add_development_dependency "rspec", "~> 3.5.0"
28
+ spec.add_development_dependency "fuubar"
29
+ spec.add_development_dependency "pry"
30
+ spec.add_development_dependency "pry-nav"
28
31
  spec.add_development_dependency "activerecord", "~> 4.1"
29
32
  spec.add_development_dependency "activerecord-nulldb-adapter"
30
33
  end
@@ -9,7 +9,7 @@ class Dummy
9
9
  end
10
10
 
11
11
  def id
12
- Random.rand(10000)
12
+ @id ||= Random.rand(10000)
13
13
  end
14
14
 
15
15
  def candies
@@ -23,7 +23,7 @@ describe Norton::Counter do
23
23
  dummy = Dummy.new
24
24
  dummy.reset_candies_count
25
25
 
26
- dummy.candies_count.must_equal 15
26
+ expect(dummy.candies_count).to eq(15)
27
27
  end
28
28
  end
29
29
 
@@ -32,7 +32,26 @@ describe Norton::Counter do
32
32
  dummy = Dummy.new
33
33
  dummy.candies_count = 200
34
34
 
35
- dummy.candies_count.must_equal 200
35
+ expect(dummy.candies_count).to eq(200)
36
36
  end
37
37
  end
38
- end
38
+
39
+ describe ".incr_value_by" do
40
+ it "should increase the value of the given amount" do
41
+ dummy = Dummy.new
42
+ dummy.incr_candies_count_by(3)
43
+
44
+ expect(dummy.candies_count).to eq(3)
45
+ end
46
+ end
47
+
48
+ describe ".decr_value_by" do
49
+ it "should decrease the value of the given amount" do
50
+ dummy = Dummy.new
51
+ dummy.candies_count = 15
52
+ dummy.decr_candies_count_by(5)
53
+
54
+ expect(dummy.candies_count).to eq(10)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ class Dummy
4
+ include Norton::Counter
5
+ include Norton::Timestamp
6
+ include Norton::Helper
7
+
8
+ counter :counter1
9
+ counter :counter2
10
+ counter :counter3
11
+
12
+ timestamp :time1
13
+
14
+ def id
15
+ @id ||= Random.rand(10000)
16
+ end
17
+ end
18
+
19
+ describe Norton::Helper do
20
+ describe ".norton_values" do
21
+ it "should respond to `:norton_values`" do
22
+ dummy = Dummy.new
23
+
24
+ expect(dummy).to respond_to(:norton_values)
25
+ end
26
+
27
+ it "should return the specific values" do
28
+ dummy = Dummy.new
29
+
30
+ dummy.counter1 = 10
31
+ dummy.counter2 = 15
32
+ dummy.counter3 = 100
33
+
34
+ dummy.touch_time1
35
+
36
+ values = dummy.norton_values(:counter1, :time1)
37
+
38
+ expect(values).to include(:counter1, :time1)
39
+ expect(values.size).to eq(2)
40
+ expect(values[:counter1]).to eq(dummy.counter1)
41
+ expect(values[:time1]).to eq(dummy.time1)
42
+ end
43
+
44
+ it "should return nil if the specific key does not exist" do
45
+ dummy = Dummy.new
46
+
47
+ dummy.counter1 = 10
48
+ dummy.counter2 = 15
49
+ dummy.counter3 = 100
50
+
51
+ dummy.touch_time1
52
+
53
+ values = dummy.norton_values(:counter1, :counter2, :time2)
54
+
55
+ expect(values).to include(:counter1, :counter2, :time2)
56
+ expect(values.size).to eq(3)
57
+ expect(values[:counter1]).to eq(dummy.counter1)
58
+ expect(values[:counter2]).to eq(dummy.counter2)
59
+ expect(values[:time2]).to be_nil
60
+ end
61
+ end
62
+ end
@@ -7,22 +7,21 @@ class Dummy
7
7
  # timestamp :graduated_at, before_save: -> { title_changed? || content_changed? }
8
8
 
9
9
  def id
10
- 1
10
+ @id ||= Random.rand(10000)
11
11
  end
12
12
  end
13
13
 
14
14
  describe Norton::Timestamp do
15
15
  describe "#timestamp" do
16
16
  it "should add a class method timestamp to the class" do
17
- Dummy.must_respond_to :timestamp
17
+ expect(Dummy).to respond_to(:timestamp)
18
18
  end
19
19
 
20
20
  it 'should create timestamp accessors and touch method' do
21
21
  dummy = Dummy.new
22
22
 
23
- dummy.must_respond_to :born_at
24
- # dummy.must_respond_to :'born_at='
25
- dummy.must_respond_to :touch_born_at
23
+ expect(dummy).to respond_to(:born_at)
24
+ expect(dummy).to respond_to(:touch_born_at)
26
25
  end
27
26
  end
28
27
 
@@ -33,7 +32,7 @@ describe Norton::Timestamp do
33
32
 
34
33
  Norton.redis.with do |conn|
35
34
  born_at = conn.get("#{Dummy.to_s.pluralize.downcase}:#{dummy.id}:born_at")
36
- born_at.to_i.must_be :>, 0
35
+ expect(born_at.to_i).to be > 0
37
36
  end
38
37
  end
39
38
  end
@@ -42,8 +41,8 @@ describe Norton::Timestamp do
42
41
  it 'should get the timestamp' do
43
42
  dummy = Dummy.new
44
43
  dummy.touch_born_at
45
- dummy.born_at.wont_be_nil
46
- dummy.born_at.must_be_kind_of Fixnum
44
+ expect(dummy.born_at).not_to be_nil
45
+ expect(dummy.born_at).to be_a(Fixnum)
47
46
  end
48
47
  end
49
- end
48
+ end
data/spec/norton_spec.rb CHANGED
@@ -4,8 +4,7 @@ describe Norton do
4
4
  describe "#setup" do
5
5
  it "should setup a redis connection pool" do
6
6
  Norton.setup url: 'redis://localhost:6379/0'
7
-
8
- Norton.redis.wont_be_nil
7
+ expect(Norton.redis).not_to be_nil
9
8
  end
10
9
  end
11
- end
10
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,11 +1,26 @@
1
1
  require 'active_record'
2
2
  require 'nulldb'
3
3
  require 'norton'
4
- require 'minitest/spec'
5
- require 'minitest/autorun'
6
- require 'minitest/mock'
7
- require 'minitest/pride'
4
+ require 'rspec'
5
+
6
+ RSpec.configure do |config|
7
+ config.expect_with :rspec do |expectations|
8
+ expectations.syntax = :expect
9
+ end
10
+
11
+ config.mock_with :rspec do |mocks|
12
+ mocks.syntax = :expect
13
+ mocks.verify_partial_doubles = true
14
+ end
15
+
16
+ config.order = :random
17
+
18
+ # Clean Up Redis
19
+ config.before(:each) do
20
+ Norton.redis.with { |conn| conn.flushdb }
21
+ end
22
+ end
8
23
 
9
24
  Norton.setup url: 'redis://localhost:6379/0'
10
25
 
11
- ActiveRecord::Base.establish_connection :adapter => :nulldb
26
+ ActiveRecord::Base.establish_connection :adapter => :nulldb
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.18
4
+ version: 0.0.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Larry Zhao
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-24 00:00:00.000000000 Z
11
+ date: 2016-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -81,19 +81,61 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: minitest
84
+ name: rspec
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: 5.5.1
89
+ version: 3.5.0
90
90
  type: :development
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: 5.5.1
96
+ version: 3.5.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: fuubar
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry-nav
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
97
139
  - !ruby/object:Gem::Dependency
98
140
  name: activerecord
99
141
  requirement: !ruby/object:Gem::Requirement
@@ -130,6 +172,7 @@ extensions: []
130
172
  extra_rdoc_files: []
131
173
  files:
132
174
  - ".gitignore"
175
+ - ".rspec"
133
176
  - Gemfile
134
177
  - LICENSE
135
178
  - LICENSE.txt
@@ -137,11 +180,13 @@ files:
137
180
  - Rakefile
138
181
  - lib/norton.rb
139
182
  - lib/norton/counter.rb
183
+ - lib/norton/helper.rb
140
184
  - lib/norton/timed_value.rb
141
185
  - lib/norton/timestamp.rb
142
186
  - lib/norton/version.rb
143
187
  - norton.gemspec
144
188
  - spec/norton/counter_spec.rb
189
+ - spec/norton/helper_spec.rb
145
190
  - spec/norton/timestamp_spec.rb
146
191
  - spec/norton_spec.rb
147
192
  - spec/spec_helper.rb
@@ -172,6 +217,7 @@ summary: Provide simple helpers on persist values in redis for performance. Work
172
217
  with ActiveRecord.
173
218
  test_files:
174
219
  - spec/norton/counter_spec.rb
220
+ - spec/norton/helper_spec.rb
175
221
  - spec/norton/timestamp_spec.rb
176
222
  - spec/norton_spec.rb
177
223
  - spec/spec_helper.rb