norton 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 66c77627990e541e8db55316cc1c8c401f9dc6de
4
+ data.tar.gz: b15b760ab01ae29de4dc5d925ccb09a2afa9935c
5
+ SHA512:
6
+ metadata.gz: 6d713c60206ea1a4082b93e154077d72f65a751fc470f360e25818104f06466e8fdb50bac5c7b7fc445db08a5d594f8047c317dc41c14777f24c8e5bb72c504f
7
+ data.tar.gz: ec2c55cc1a55f1af8abe70d3a34f55498b295400af5980b10cb0f040458ddb3412e989323462d2806f1da5810d9f5aa2abfc6468e1096266c0a5df2563dba478
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .ruby-gemset
5
+ .ruby-version
6
+ .config
7
+ .yardoc
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in norton.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Larry Zhao
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Norton
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'norton'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install norton
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/norton/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'spec'
6
+ t.pattern = "spec/**/*_spec.rb"
7
+ end
@@ -0,0 +1,53 @@
1
+ module Norton
2
+ module Counter
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ #
7
+ # [counter description]
8
+ # @param name [type] [description]
9
+ # @param touches={} [type] [description]
10
+ #
11
+ # @return [type] [description]
12
+ def counter(name, options={})
13
+ define_method(name) do
14
+ Norton.redis.with do |conn|
15
+ conn.set("#{self.class.to_s.pluralize.downcase}:#{self.id}:#{name}", Time.now.to_i).try(:to_i) || 0
16
+ end
17
+ end
18
+
19
+ define_method("incr_#{name}") do
20
+ Norton.redis.with do |conn|
21
+ conn.incr("#{self.class.to_s.pluralize.downcase}:#{self.id}:#{name}")
22
+ end
23
+ end
24
+
25
+ define_method("decr_#{name}") do
26
+ Norton.redis.with do |conn|
27
+ conn.decr("#{self.class.to_s.pluralize.downcase}:#{self.id}:#{name}")
28
+ end
29
+ end
30
+
31
+ define_method("#{name}=") do |v|
32
+ Norton.redis.with do |conn|
33
+ conn.set("#{self.class.to_s.pluralize.downcase}:#{self.id}:#{name}", v)
34
+ end
35
+ end
36
+
37
+ # Add Increment callback
38
+ unless options[:incr].empty?
39
+ options[:incr].each do |callback|
40
+ self.send callback, proc{ instance_eval("incr_#{name}".to_sym) }
41
+ end
42
+ end
43
+
44
+ # Add Decrement callback
45
+ unless options[:decr].empty?
46
+ options[:decr].each do |callback|
47
+ self.send callback, proc{ instance_eval("decr_#{name}".to_sym) }
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,34 @@
1
+ module Norton
2
+ module Timestamp
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ #
7
+ # [timestamp Define a timestamp]
8
+ # @param name [type] [description]
9
+ # @param touches={} [type] [description]
10
+ #
11
+ # @return [type] [description]
12
+ def timestamp(name, touches={})
13
+ define_method(name) do
14
+ Norton.redis.with do |conn|
15
+ conn.set("#{self.class.to_s.pluralize.downcase}:#{self.id}:#{name}", Time.now.to_i).try(:to_i) || Time.now.to_i
16
+ end
17
+ end
18
+
19
+ define_method("touch_#{name}") do
20
+ Norton.redis.with do |conn|
21
+ conn.set("#{self.class.to_s.pluralize.downcase}:#{self.id}:#{name}", Time.now.to_i)
22
+ end
23
+ end
24
+
25
+ # Add callback
26
+ unless touches.empty?
27
+ touches.each do |callback, condition|
28
+ self.send callback, proc{ if instance_eval(&condition) then instance_eval("touch_#{name}".to_sym) end }
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module Norton
2
+ VERSION = "0.0.1"
3
+ end
data/lib/norton.rb ADDED
@@ -0,0 +1,29 @@
1
+ require "redis"
2
+ require "connection_pool"
3
+ require "active_support/concern"
4
+ require "active_support/inflector"
5
+ require "norton/version"
6
+ require "norton/timestamp"
7
+ require "norton/value"
8
+
9
+ module Norton
10
+ class << self
11
+ attr_accessor :redis
12
+
13
+ #
14
+ # Setup your redis connection.
15
+ # @param options={} [Hash] [Redis connection configuration]
16
+ # url - Redis connection url
17
+ # pool - Connection pool size
18
+ # timeout - Connection pool timeout
19
+ # @return [nil] [description]
20
+ def setup(options={})
21
+ pool_size = (options[:pool] || 1).to_i
22
+ timeout = (options[:timeout] || 2).to_i
23
+
24
+ Norton.redis = ConnectionPool.new(size: pool_size, timeout: timeout) do
25
+ Redis.new(url: options[:url])
26
+ end
27
+ end
28
+ end
29
+ end
data/norton.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'norton/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "norton"
8
+ spec.version = Norton::VERSION
9
+ spec.authors = ["Larry Zhao"]
10
+ spec.email = ["thehiddendepth@gmail.com"]
11
+ spec.summary = %q{Provide simple helpers on persist values in redis for performance. Works with ActiveRecord.}
12
+ spec.description = %q{}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "redis", "~> 3.1.0"
22
+ spec.add_dependency "connection_pool", "~> 2.1.0"
23
+ spec.add_dependency "activesupport", "~> 4.1.8"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.5"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "minitest", "~> 5.5.1"
28
+ spec.add_development_dependency "activerecord", "~> 4.1.8"
29
+ spec.add_development_dependency "activerecord-nulldb-adapter"
30
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ class Dummy
4
+ include Norton::Timestamp
5
+
6
+ timestamp :born_at
7
+ timestamp :graduated_at, before_save: -> { title_changed? || content_changed? }
8
+
9
+ def id
10
+ 1
11
+ end
12
+ end
13
+
14
+ describe Norton::Timestamp do
15
+ describe "#timestamp" do
16
+ it "should add a class method timestamp to the class" do
17
+ Dummy.must_respond_to :timestamp
18
+ end
19
+
20
+ it 'should create timestamp accessors and touch method' do
21
+ dummy = Dummy.new
22
+
23
+ dummy.must_respond_to :born_at
24
+ # dummy.must_respond_to :'born_at='
25
+ dummy.must_respond_to :touch_born_at
26
+ end
27
+ end
28
+
29
+ describe ".touch_born_at" do
30
+ it 'should set timestamp in redis' do
31
+ dummy = Dummy.new
32
+ dummy.touch_born_at
33
+
34
+ Norton.redis.with do |conn|
35
+ born_at = conn.get("#{Dummy.to_s.pluralize.downcase}:#{dummy.id}:born_at")
36
+ born_at.to_i.must_be :>, 0
37
+ end
38
+ end
39
+ end
40
+
41
+ describe ".born_at" do
42
+ it 'should get the timestamp' do
43
+ dummy = Dummy.new
44
+ dummy.touch_born_at
45
+ dummy.born_at.wont_be_nil
46
+ dummy.born_at.must_be_kind_of Fixnum
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Norton do
4
+ describe "#setup" do
5
+ it "should setup a redis connection pool" do
6
+ Norton.setup url: 'redis://localhost:6379/0'
7
+
8
+ Norton.redis.wont_be_nil
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'active_record'
2
+ require 'nulldb'
3
+ require 'norton'
4
+ require 'minitest/spec'
5
+ require 'minitest/autorun'
6
+ require 'minitest/mock'
7
+ require 'minitest/pride'
8
+
9
+ Norton.setup url: 'redis://localhost:6379/0'
10
+
11
+ ActiveRecord::Base.establish_connection :adapter => :nulldb
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: norton
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Larry Zhao
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: connection_pool
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 4.1.8
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 4.1.8
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 5.5.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 5.5.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: activerecord
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 4.1.8
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 4.1.8
111
+ - !ruby/object:Gem::Dependency
112
+ name: activerecord-nulldb-adapter
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
+ description: ''
126
+ email:
127
+ - thehiddendepth@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - Gemfile
134
+ - LICENSE.txt
135
+ - README.md
136
+ - Rakefile
137
+ - lib/norton.rb
138
+ - lib/norton/counter.rb
139
+ - lib/norton/timestamp.rb
140
+ - lib/norton/version.rb
141
+ - norton.gemspec
142
+ - spec/norton/timestamp_spec.rb
143
+ - spec/norton_spec.rb
144
+ - spec/spec_helper.rb
145
+ homepage: ''
146
+ licenses:
147
+ - MIT
148
+ metadata: {}
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubyforge_project:
165
+ rubygems_version: 2.4.5
166
+ signing_key:
167
+ specification_version: 4
168
+ summary: Provide simple helpers on persist values in redis for performance. Works
169
+ with ActiveRecord.
170
+ test_files:
171
+ - spec/norton/timestamp_spec.rb
172
+ - spec/norton_spec.rb
173
+ - spec/spec_helper.rb