redis-wrap 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1f4f0dc09510ead01d649792f68ab44732e1bc27
4
+ data.tar.gz: f6c44a5e02ea93445f72d851b3bc5743f453728a
5
+ SHA512:
6
+ metadata.gz: d63a3117b2860184ef8e9df704ec41cb118a375dad5ad76635a1e213067891c5e331816a2512ced54a40fb5de58b8a8c23b4b736409f7bf229873ee37dda14dd
7
+ data.tar.gz: 1db64e7604d159cd20f93201b22a4faec1f64430ff6dc2aaaf6672273b5754bd545b7fe42786cefe32da208fd5352b2568c8cc5a4a347ca2c251856bfb437be1
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+ ruby '2.1.0'
3
+
4
+ gem 'redis', '~> 3'
5
+
6
+ group :development do
7
+ gem 'require_all'
8
+ gem 'rake'
9
+ gem 'minitest'
10
+ gem 'nokogiri'
11
+ end
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # `redis-wrap`
2
+
3
+ Every web application needs Redis. The problem is that Redis does not have a schema, which forces the developer to establish conventions across the code base. The goal of `redis-wrap` is to have one key declaration per object to avoid spaghetti code.
4
+
5
+ ### Examples
6
+
7
+ Here's how it works. Set up Redis in initialization code.
8
+
9
+ ```ruby
10
+ require 'redis'
11
+ require 'redis-wrap'
12
+
13
+ Redis.current = Redis.new()
14
+ ```
15
+
16
+ State the key once and then access methods.
17
+
18
+ ```ruby
19
+ redis = Redis.current
20
+
21
+ counter = redis['counter']
22
+ # => #<Redis::Wrap @key="counter">
23
+
24
+ counter.incr
25
+ # => 1
26
+
27
+ counter.incr
28
+ # => 2
29
+
30
+ counter.del
31
+ counter.incr
32
+ # => 1
33
+ ```
34
+
35
+ Wraps may be used interchangably as keys.
36
+
37
+ ```ruby
38
+ class User
39
+ def interests
40
+ Redis::Wrap.new("users:#{id}:interests")
41
+ end
42
+ end
43
+
44
+ jack.interests.sadd(%w(music cooking gardening dancing))
45
+ jean.interests.sadd(%w(movies music dancing))
46
+
47
+ jack.interests.sinter(jean.interests)
48
+ # => [ "music", "dancing" ]
49
+ ```
50
+
51
+ The wrap is simply a ruby object so you can extend functionality.
52
+
53
+ ```ruby
54
+ require 'marshal'
55
+
56
+ class CachedFind < Redis::Wrap
57
+ def initialize(klass, id)
58
+ @klass = klass
59
+ @id = id
60
+ end
61
+
62
+ def key
63
+ "#{@klass.underscore}:#{@id}")
64
+ end
65
+
66
+ def fetch
67
+ binary = get
68
+ if binary.present?
69
+ Marshal.load(binary)
70
+ else
71
+ klass.find(id).tap do |model|
72
+ set(Marshal.dump(model))
73
+ end
74
+ end
75
+ end
76
+ end
77
+
78
+ CachedFind.new(User, params[:id]).fetch
79
+ ```
80
+
81
+ ### Install
82
+
83
+ **TODO.**
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift(File.join(__dir__, 'lib'))
2
+
3
+ require 'require_all'
4
+
5
+ task default: :test
6
+
7
+ task :test do
8
+ require_relative 'test/helper.rb'
9
+ require_rel 'test/**/*_spec.rb'
10
+ end
data/lib/redis-wrap.rb ADDED
@@ -0,0 +1 @@
1
+ require 'redis/wrap'
data/lib/redis/wrap.rb ADDED
@@ -0,0 +1,29 @@
1
+ class Redis
2
+ def [](key)
3
+ Wrap.new(key)
4
+ end
5
+
6
+ class Wrap
7
+ attr_reader :key
8
+
9
+ def initialize(key)
10
+ @key = key
11
+ end
12
+
13
+ def method_missing(command, *arguments, &block)
14
+ arguments = arguments.map do |argument|
15
+ if argument.is_a?(Redis::Wrap)
16
+ argument.key
17
+ else
18
+ argument
19
+ end
20
+ end
21
+
22
+ redis.send(command, key, *arguments, &block)
23
+ end
24
+
25
+ def redis
26
+ Redis.current
27
+ end
28
+ end
29
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'redis'
2
+ require 'minitest/autorun'
3
+
4
+ Redis.current = Redis.new(db: 0)
5
+
6
+ class Minitest::Spec
7
+ after(:each) do
8
+ redis.flushdb
9
+ end
10
+
11
+ def redis
12
+ Redis.current
13
+ end
14
+ end
15
+
@@ -0,0 +1,61 @@
1
+ require 'redis/wrap'
2
+
3
+ describe Redis do
4
+ describe '#[]' do
5
+ it 'should return a wrap' do
6
+ assert_equal(redis['test'].class, Redis::Wrap)
7
+ end
8
+
9
+ it 'should pass itself' do
10
+ assert_equal(redis['test'].redis, redis)
11
+ end
12
+ end
13
+ end
14
+
15
+ describe Redis::Wrap do
16
+ describe 'string' do
17
+ let(:string) { redis['string'] }
18
+
19
+ it 'should set and get' do
20
+ string.set('hello')
21
+ assert_equal(string.get, 'hello')
22
+ end
23
+ end
24
+
25
+ describe 'counter' do
26
+ let(:counter) { redis['counter'] }
27
+
28
+ it 'should increment' do
29
+ assert_equal(counter.incr, 1)
30
+ end
31
+
32
+ it 'should decrement' do
33
+ counter.incr
34
+ assert_equal(counter.decr, 0)
35
+ end
36
+ end
37
+
38
+ describe 'set' do
39
+ it 'should intersect' do
40
+ s1 = redis['set:1']
41
+ s2 = redis['set:2']
42
+ s3 = redis['set:3']
43
+
44
+ s1.sadd(%w(1 2 3))
45
+ s2.sadd(%w(2 3 4))
46
+ s3.sadd(%w(3 4 5))
47
+
48
+ assert_equal(s1.sinter(s2, s3), %w(3))
49
+ end
50
+ end
51
+
52
+ describe 'leaderboard' do
53
+ let(:leaderboard) { redis['leaderboard'] }
54
+
55
+ it 'should get top scores' do
56
+ leaderboard.zadd([ [ 7, 'abc' ], [ 23, 'xyz' ] ])
57
+ leaders = leaderboard.zrevrange(0, -1)
58
+ assert_equal(leaders, %w(xyz abc))
59
+ end
60
+ end
61
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis-wrap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - "@aj0strow"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-15 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'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3'
27
+ description: ''
28
+ email:
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - ".gitignore"
34
+ - Gemfile
35
+ - README.md
36
+ - Rakefile
37
+ - lib/redis-wrap.rb
38
+ - lib/redis/wrap.rb
39
+ - test/helper.rb
40
+ - test/redis/wrap_spec.rb
41
+ homepage: https://github.com/aj0strow/redis-wrap
42
+ licenses:
43
+ - MIT
44
+ metadata: {}
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 2.2.0
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: object-oriented redis
65
+ test_files:
66
+ - test/helper.rb
67
+ - test/redis/wrap_spec.rb