redis_attr 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in redis_attr.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jason Haruska
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.
@@ -0,0 +1,39 @@
1
+ # RedisAttr
2
+
3
+ RedisAttr builds on the great Redis::Objects project. It provides more meta-
4
+ programming info around the redis attributes, some nice helper methods for
5
+ grabbing the values, and ability to auto-cleanup redis on destroy of the
6
+ object.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'redis_attr'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install redis_attr
21
+
22
+ ## Usage
23
+
24
+ To use on any ruby object, just include `RedisAttr::Model`. It will then
25
+ provide you with enhanced Redis::Objects.
26
+
27
+ class MyObj
28
+ include RedisAttr::Model
29
+
30
+ counter :num_calls
31
+ end
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'lib' << 'test'
6
+ test.pattern = 'test/**/*_test.rb'
7
+ test.verbose = true
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,5 @@
1
+ require 'redis_attr/version'
2
+ require 'redis_attr/model'
3
+
4
+ module RedisAttr
5
+ end
@@ -0,0 +1,53 @@
1
+ require 'active_support/concern'
2
+ require 'redis-objects'
3
+
4
+ module RedisAttr
5
+ module Model
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ after_destroy :clean_redis_attributes if respond_to?(:after_destroy)
10
+ end
11
+
12
+ def redis_attributes
13
+ self.class.redis_attributes
14
+ end
15
+
16
+ def set_redis_keys_expire(seconds = 3.days)
17
+ self.redis_attributes.each do |attr|
18
+ send(attr).expire(seconds)
19
+ end
20
+ end
21
+
22
+ def clean_redis_attributes
23
+ set_redis_keys_expire(1.minute)
24
+ end
25
+
26
+ module ClassMethods
27
+ attr_writer :redis_attributes
28
+ def redis_attributes
29
+ @redis_attributes ||=[]
30
+ end
31
+
32
+
33
+ def counter(name, opts={})
34
+ counter_name = "#{name}_counter".to_sym
35
+ super counter_name, opts
36
+ define_method(name) { send(counter_name).to_i }
37
+
38
+ self.redis_attributes << counter_name
39
+ end
40
+
41
+ def hash_key(name, opts={})
42
+ super name, opts
43
+ self.redis_attributes << name
44
+ end
45
+
46
+ def value(name, opts={})
47
+ super name, opts
48
+ self.redis_attributes << name
49
+ end
50
+ end
51
+ end
52
+ end
53
+
@@ -0,0 +1,3 @@
1
+ module RedisAttr
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'redis_attr/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "redis_attr"
8
+ spec.version = RedisAttr::VERSION
9
+ spec.authors = ["Jason Haruska"]
10
+ spec.email = ["jason@haruska.com"]
11
+ spec.description = %q{Ruby model attributes backed by redis}
12
+ spec.summary = %q{An opinionated Redis::Objects}
13
+ spec.homepage = "https://github.com/haruska/redis_attr"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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_runtime_dependency "activesupport", ">= 3.0.2", "< 4.1"
22
+ spec.add_runtime_dependency "redis-objects"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "test-unit"
27
+ spec.add_development_dependency "shoulda-context"
28
+ spec.add_development_dependency "factory_girl"
29
+ spec.add_development_dependency "active_attr"
30
+ spec.add_development_dependency "activerecord"
31
+ spec.add_development_dependency "sqlite3"
32
+ spec.add_development_dependency "faker"
33
+ spec.add_development_dependency "minitest"
34
+ end
@@ -0,0 +1,41 @@
1
+ require 'bundler/setup'
2
+
3
+ require 'test/unit'
4
+ require 'shoulda-context'
5
+ require 'factory_girl'
6
+ require 'active_attr'
7
+ require 'active_attr/model'
8
+ require 'faker'
9
+ require 'active_record'
10
+
11
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
12
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'unit'))
13
+
14
+ require 'redis_attr'
15
+
16
+ ActiveRecord::Base.establish_connection(
17
+ :adapter => 'sqlite3',
18
+ :database => File.expand_path(File.dirname(__FILE__) + '/testing_redis_attr.sqlite3')
19
+ )
20
+
21
+ ActiveRecord::Schema.define do
22
+ create_table :my_runs, :force => true
23
+ end
24
+
25
+ class MyRun < ActiveRecord::Base
26
+ include Redis::Objects
27
+ include RedisAttr::Model
28
+
29
+ counter :jobs_remaining
30
+ hash_key :next_high_watermark
31
+ value :still_active, :default => true
32
+ end
33
+
34
+ FactoryGirl.define do
35
+ factory :my_run do
36
+ after(:create) do |r|
37
+ r.jobs_remaining_counter.incr(rand(5))
38
+ r.next_high_watermark[Faker::Lorem.word] = Faker::Lorem.sentence
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,45 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper.rb'))
2
+
3
+ module RedisAttr
4
+ class ModelTest < Test::Unit::TestCase
5
+ setup do
6
+ @run = FactoryGirl.create(:my_run)
7
+ end
8
+
9
+ teardown do
10
+ @run.set_redis_keys_expire(1)
11
+ end
12
+
13
+ should 'keep track of redis attribute names' do
14
+ [:jobs_remaining_counter, :next_high_watermark, :still_active].each do |attr|
15
+ assert @run.redis_attributes.include?(attr), "#{attr} not included in #{@run.redis_attributes}"
16
+ end
17
+ end
18
+
19
+ should 'add helper method for counters that returns current integer value' do
20
+ assert_equal @run.jobs_remaining_counter.value.to_i, @run.jobs_remaining
21
+ end
22
+
23
+ should 'clean redis attributes' do
24
+ @run.redis_attributes.each {|a| assert_equal -1, @run.send(a).ttl}
25
+
26
+ @run.clean_redis_attributes
27
+
28
+ @run.redis_attributes.each do |a|
29
+ assert @run.send(a).ttl > 0
30
+ assert @run.send(a).ttl <= 60
31
+ end
32
+ end
33
+
34
+ should 'hook into after_destroy if exists' do
35
+ @run.redis_attributes.each {|a| assert_equal -1, @run.send(a).ttl}
36
+
37
+ @run.destroy
38
+
39
+ @run.redis_attributes.each do |a|
40
+ assert @run.send(a).ttl > 0
41
+ assert @run.send(a).ttl <= 60
42
+ end
43
+ end
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,258 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis_attr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jason Haruska
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ type: :runtime
16
+ name: activesupport
17
+ prerelease: false
18
+ requirement: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 3.0.2
23
+ - - <
24
+ - !ruby/object:Gem::Version
25
+ version: '4.1'
26
+ none: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ! '>='
30
+ - !ruby/object:Gem::Version
31
+ version: 3.0.2
32
+ - - <
33
+ - !ruby/object:Gem::Version
34
+ version: '4.1'
35
+ none: false
36
+ - !ruby/object:Gem::Dependency
37
+ type: :runtime
38
+ name: redis-objects
39
+ prerelease: false
40
+ requirement: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ none: false
46
+ version_requirements: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ none: false
52
+ - !ruby/object:Gem::Dependency
53
+ type: :development
54
+ name: bundler
55
+ prerelease: false
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ version: '1.3'
61
+ none: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ~>
65
+ - !ruby/object:Gem::Version
66
+ version: '1.3'
67
+ none: false
68
+ - !ruby/object:Gem::Dependency
69
+ type: :development
70
+ name: rake
71
+ prerelease: false
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ none: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ none: false
84
+ - !ruby/object:Gem::Dependency
85
+ type: :development
86
+ name: test-unit
87
+ prerelease: false
88
+ requirement: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ none: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ none: false
100
+ - !ruby/object:Gem::Dependency
101
+ type: :development
102
+ name: shoulda-context
103
+ prerelease: false
104
+ requirement: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ none: false
110
+ version_requirements: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ none: false
116
+ - !ruby/object:Gem::Dependency
117
+ type: :development
118
+ name: factory_girl
119
+ prerelease: false
120
+ requirement: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ none: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ! '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ none: false
132
+ - !ruby/object:Gem::Dependency
133
+ type: :development
134
+ name: active_attr
135
+ prerelease: false
136
+ requirement: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ! '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ none: false
142
+ version_requirements: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ! '>='
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ none: false
148
+ - !ruby/object:Gem::Dependency
149
+ type: :development
150
+ name: activerecord
151
+ prerelease: false
152
+ requirement: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ! '>='
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ none: false
158
+ version_requirements: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ! '>='
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ none: false
164
+ - !ruby/object:Gem::Dependency
165
+ type: :development
166
+ name: sqlite3
167
+ prerelease: false
168
+ requirement: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ! '>='
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ none: false
174
+ version_requirements: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ! '>='
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ none: false
180
+ - !ruby/object:Gem::Dependency
181
+ type: :development
182
+ name: faker
183
+ prerelease: false
184
+ requirement: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - ! '>='
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
189
+ none: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ! '>='
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ none: false
196
+ - !ruby/object:Gem::Dependency
197
+ type: :development
198
+ name: minitest
199
+ prerelease: false
200
+ requirement: !ruby/object:Gem::Requirement
201
+ requirements:
202
+ - - ! '>='
203
+ - !ruby/object:Gem::Version
204
+ version: '0'
205
+ none: false
206
+ version_requirements: !ruby/object:Gem::Requirement
207
+ requirements:
208
+ - - ! '>='
209
+ - !ruby/object:Gem::Version
210
+ version: '0'
211
+ none: false
212
+ description: Ruby model attributes backed by redis
213
+ email:
214
+ - jason@haruska.com
215
+ executables: []
216
+ extensions: []
217
+ extra_rdoc_files: []
218
+ files:
219
+ - .gitignore
220
+ - Gemfile
221
+ - LICENSE.txt
222
+ - README.md
223
+ - Rakefile
224
+ - lib/redis_attr.rb
225
+ - lib/redis_attr/model.rb
226
+ - lib/redis_attr/version.rb
227
+ - redis_attr.gemspec
228
+ - test/test_helper.rb
229
+ - test/unit/model_test.rb
230
+ homepage: https://github.com/haruska/redis_attr
231
+ licenses:
232
+ - MIT
233
+ post_install_message:
234
+ rdoc_options: []
235
+ require_paths:
236
+ - lib
237
+ required_ruby_version: !ruby/object:Gem::Requirement
238
+ requirements:
239
+ - - ! '>='
240
+ - !ruby/object:Gem::Version
241
+ version: '0'
242
+ none: false
243
+ required_rubygems_version: !ruby/object:Gem::Requirement
244
+ requirements:
245
+ - - ! '>='
246
+ - !ruby/object:Gem::Version
247
+ version: '0'
248
+ none: false
249
+ requirements: []
250
+ rubyforge_project:
251
+ rubygems_version: 1.8.23
252
+ signing_key:
253
+ specification_version: 3
254
+ summary: An opinionated Redis::Objects
255
+ test_files:
256
+ - test/test_helper.rb
257
+ - test/unit/model_test.rb
258
+ has_rdoc: