ar_redis 0.1.0

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: a15374d7f8e3c5015ac2fcd4213345b3b7fdde3e
4
+ data.tar.gz: dd765a699ce6db97a8d4f7b8e189c19deede15f8
5
+ SHA512:
6
+ metadata.gz: cefadb4f221d15f461a7652e1c1c35967674601f012b2da1d33d585887e6c7f250eb6d8c8c48fe6328c33c246c02d29e3d36a9f6537b48241f76f299d978b0a6
7
+ data.tar.gz: 475ac05fa76e64bb2867cc134ea846b62fb9a12f626e2d3e96541bb299c58e69583518f5ed9e19b384838acc4c749a6231d05c4ef973616997a62ddf07fce8df
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 Nick Weiland
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # ArRedis
2
+ ArRedis allows you to easily create and remove nested, namespaced Redis keys under ActiveRecord objects and classes. The functionality is inspired by [this RailsConf presentation](https://www.youtube.com/watch?v=dH6VYRMRQFw) by Obie Fernandez. Also, [Nest](https://github.com/soveran/nest) is a similar gem that does not hook into ActiveRecord.
3
+
4
+ ## Installation
5
+ Add this line to the Gemfile of an application that uses ActiveRecord:
6
+
7
+ ```ruby
8
+ gem 'ar_redis'
9
+ ```
10
+
11
+ And then execute:
12
+ ```bash
13
+ $ bundle
14
+ ```
15
+
16
+ If you are not using Redis in you application yet, setup the [redis-rb gem](https://github.com/redis/redis-rb).
17
+
18
+ ## Usage
19
+
20
+ All ActiveRecord models will be provided with the method ```#redis```.
21
+
22
+ Given:
23
+
24
+ ```ruby
25
+ class TestModel < ActiveRecord::Base
26
+ end
27
+ ```
28
+
29
+ You can now create nested, namespaced Redis keys that have access to all [Redis commands](https://redis.io/commands).
30
+
31
+ ```ruby
32
+ >> test_model = TestModel.create
33
+ >> #<TestModel:0x007fc08a466890 id: 1>
34
+
35
+ >> test_model.redis.key
36
+ => "TestModel:1"
37
+
38
+ >> test_model.redis[:reports]["01/2018"].key
39
+ => "TestModel:1:reports:01/2018"
40
+
41
+ >> test_model.redis[:reports]["01/2018"].call(:set, { clicks: 12, views: 100 }.to_json)
42
+ => "OK"
43
+
44
+ >> JSON.parse(test_model.redis[:reports]["01/2018"].call(:get))
45
+ => { clicks: 12, views: 100 }
46
+ ```
47
+
48
+ ### Configuring your Redis Client
49
+ Here's an example of how you can pass a specific Redis client to ArRedis
50
+
51
+ ```ruby
52
+ # initializers/redis.rb
53
+ $redis = Redis.new(host: "10.0.1.1", port: 6380, db: 15)
54
+ ArRedis.redis = $redis
55
+ ```
56
+
57
+ ## Contributing
58
+
59
+ Write some code!
60
+
61
+ 1. [Fork](https://help.github.com/articles/fork-a-repo) ArRedis
62
+ 2. Create a topic branch - `git checkout -b my_branch`
63
+ 3. Push to your branch - `git push origin my_branch`
64
+ 4. Create a [Pull Request](http://help.github.com/pull-requests/) from your
65
+ branch
66
+ 5. That's it!
67
+
68
+ If you're not doing some sort of refactoring, a CHANGELOG entry is appropriate.
69
+ Please include them in pull requests adding features or fixing bugs.
70
+
71
+ Tests
72
+ -----
73
+
74
+ We use rspec for testing.
75
+
76
+ A simple `bundle exec rspec` will run all the tests. Make sure they pass when
77
+ you submit a pull request.
78
+
79
+ ## License
80
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'bundler/gem_tasks'
8
+
9
+ require 'rake/testtask'
10
+
11
+ Rake::TestTask.new(:spec) do |t|
12
+ t.libs << 'spec'
13
+ t.pattern = 'spec/**/*_spec.rb'
14
+ t.verbose = false
15
+ end
16
+
17
+ task default: :spec
data/lib/ar_redis.rb ADDED
@@ -0,0 +1,21 @@
1
+ require "redis"
2
+ require "active_support"
3
+ require "ar_redis/version"
4
+ require "ar_redis/base"
5
+ require "ar_redis/active_record_ext"
6
+
7
+ module ArRedis
8
+ extend self
9
+
10
+ def redis
11
+ return @redis if @redis
12
+ self.redis = Redis.new
13
+ self.redis
14
+ end
15
+
16
+ def redis=(server)
17
+ if server.is_a?(Redis)
18
+ @redis = server
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,11 @@
1
+ ActiveSupport.on_load(:active_record) do
2
+ ::ActiveRecord::Base.class_eval do
3
+ def redis
4
+ ArRedis::Base.new(self.class.name)[id]
5
+ end
6
+
7
+ def self.redis
8
+ ArRedis::Base.new(name)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,30 @@
1
+ module ArRedis
2
+ class Base
3
+ attr_reader :key, :redis_client
4
+
5
+ def initialize(key)
6
+ @key = key.to_s
7
+ @redis_client = ArRedis.redis
8
+ end
9
+
10
+ def [](next_key)
11
+ ArRedis::Base.new("#{key}:#{next_key}")
12
+ end
13
+
14
+ def call(command, *arguments)
15
+ redis_client.call(command, key, *arguments)
16
+ end
17
+
18
+ def delete_all
19
+ keys = redis_client.keys("#{self}*")
20
+
21
+ unless keys.blank?
22
+ redis_client.del(keys)
23
+ end
24
+ end
25
+
26
+ def to_s
27
+ key
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module ArRedis
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,164 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ar_redis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nick Weiland
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: redis
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.4
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 3.0.4
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
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: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
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
+ description: Adds out-of-the-box namespaced and nested Redis keys for ActiveRecord
126
+ objects.
127
+ email:
128
+ - nickweiland@gmail.com
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - MIT-LICENSE
134
+ - README.md
135
+ - Rakefile
136
+ - lib/ar_redis.rb
137
+ - lib/ar_redis/active_record_ext.rb
138
+ - lib/ar_redis/base.rb
139
+ - lib/ar_redis/version.rb
140
+ homepage: https://github.com/weilandia/ar_redis
141
+ licenses:
142
+ - MIT
143
+ metadata: {}
144
+ post_install_message:
145
+ rdoc_options: []
146
+ require_paths:
147
+ - lib
148
+ required_ruby_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ requirements: []
159
+ rubyforge_project:
160
+ rubygems_version: 2.5.1
161
+ signing_key:
162
+ specification_version: 4
163
+ summary: ActiveRecord keys for Redis.
164
+ test_files: []