nest-redis 4.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: ba39bd9976d7c71cd6f78aabf7d793a13d869977
4
+ data.tar.gz: ad2c9222489edf0b207885279d80a6ebad123a66
5
+ SHA512:
6
+ metadata.gz: 8091caf8e05ce72682eb9a024ebd9526ae52d899269ac8bc11f3a6a95c5457bbcc2d8e9fad3924566e455ea7a0ad01e480684ee50e5ca3955dcff27e6740a98a
7
+ data.tar.gz: 88978a2dcaf65c3d28a3780d29d087add69d5ba9271bb590096f9de0ccd44c9ad6221753d71ee79b70242becb1cb74c24c3e6a216d2ef98c17d99504ff121fd2
data/.gems ADDED
@@ -0,0 +1,2 @@
1
+ redic -v 1.5.1
2
+ cutest -v 1.2.3
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ /pkg
data/CHANGELOG.md ADDED
@@ -0,0 +1,27 @@
1
+ ## 3.1.2
2
+
3
+ - Define to_json
4
+
5
+ This solves issues that arise when a third party library redefines
6
+ `to_json` for all objects.
7
+
8
+ ## 3.1.1
9
+
10
+ - Define to_a and to_ary
11
+
12
+ These method definitions are needed for interacting with array-casting
13
+ operations like Array#flatten and Array().
14
+
15
+ ## 3.1.0
16
+
17
+ - Forward missing methods to Redis as commands
18
+
19
+ ## 3.0.0
20
+
21
+ - Use `call` for interacting with Redis
22
+
23
+ In previous versions, all the allowed Redis commands were defined
24
+ in Ruby. The downside of that approach was the fact Nest had to
25
+ be kept in sync with Redis as new commands were added to the
26
+ later. The new approach is more verbose, but the maintenance is
27
+ now close to non-existent.
data/CONTRIBUTING ADDED
@@ -0,0 +1,19 @@
1
+ This code tries to solve a particular problem with a very simple
2
+ implementation. We try to keep the code to a minimum while making
3
+ it as clear as possible. The design is very likely finished, and
4
+ if some feature is missing it is possible that it was left out on
5
+ purpose. That said, new usage patterns may arise, and when that
6
+ happens we are ready to adapt if necessary.
7
+
8
+ A good first step for contributing is to meet us on IRC and discuss
9
+ ideas. We spend a lot of time on #lesscode at freenode, always ready
10
+ to talk about code and simplicity. If connecting to IRC is not an
11
+ option, you can create an issue explaining the proposed change and
12
+ a use case. We pay a lot of attention to use cases, because our
13
+ goal is to keep the code base simple. Usually the result of a
14
+ conversation is the creation of a different tool.
15
+
16
+ Please don't start the conversation with a pull request. The code
17
+ should come at last, and even though it may help to convey an idea,
18
+ more often than not it draws the attention to a particular
19
+ implementation.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010 Michel Martens & Damian Janowski
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,163 @@
1
+ nest-redis
2
+ ====
3
+
4
+ Object Oriented Keys for Redis.
5
+
6
+ Forewords:
7
+ In our experience, redic based nest is suffering racing with thin server.
8
+ We don't have time to reproduce it so I just swap redic with redis,
9
+ hopefully it will solve our problem (by us stopped to see the racing
10
+ issue)
11
+
12
+ Require from your Gemfile
13
+ -------------------------
14
+
15
+ gem 'nest-redis', require: 'nest'
16
+
17
+ Description
18
+ -----------
19
+
20
+ If you are familiar with databases like [Redis](http://redis.io)
21
+ and libraries like [Ohm](http://ohm.keyvalue.org) you already know how
22
+ important it is to craft the keys that will hold the data.
23
+
24
+ ```ruby
25
+ >> redis = Redis.new
26
+ >> redis.hset("Event:3", "name", "Redis Meetup")
27
+ >> redis.hget("Event:3", "name")
28
+ => ["Redis Meetup"]
29
+ ```
30
+
31
+ It is a design pattern in key-value databases to use the key to simulate
32
+ structure, and you can read more about this in the [case study for a
33
+ Twitter clone](http://redis.io/topics/twitter-clone).
34
+
35
+ Nest helps you generate those keys by providing chainable namespaces that are
36
+ already connected to Redis:
37
+
38
+ ```ruby
39
+ >> event = Nest.new("Event")
40
+ >> event[3].call("HSET", "name", "Redis Meetup")
41
+ >> event[3].call("HGET", "name")
42
+ => ["Redis Meetup"]
43
+ ```
44
+
45
+ Alternatively, you can send the Redis commands as messages to Nest,
46
+ and if the method definition is missing it will forward the command
47
+ to Redis:
48
+
49
+ ```ruby
50
+ >> event = Nest.new("Event")
51
+ >> event[3].hset("name", "Redis Meetup")
52
+ >> event[3].hget("name")
53
+ => ["Redis Meetup"]
54
+ ```
55
+
56
+ Usage
57
+ -----
58
+
59
+ To create a new namespace:
60
+
61
+ ```ruby
62
+ >> ns = Nest.new("foo")
63
+ => "foo"
64
+
65
+ >> ns["bar"]
66
+ => "foo:bar"
67
+
68
+ >> ns["bar"]["baz"]["qux"]
69
+ => "foo:bar:baz:qux"
70
+ ```
71
+
72
+ And you can use any object as a key, not only strings:
73
+
74
+ ```ruby
75
+ >> ns[:bar][42]
76
+ => "foo:bar:42"
77
+ ```
78
+
79
+ In a more realistic tone, lets assume you are working with Redis and
80
+ dealing with events:
81
+
82
+ ```ruby
83
+ >> event = Nest.new("Event")
84
+ => "Event"
85
+
86
+ >> id = event[:id].incr
87
+ => 1
88
+
89
+ >> event[id].hset("name", "Redis Meetup")
90
+ => 1
91
+
92
+ >> meetup = event[id]
93
+ => "Event:1"
94
+
95
+ >> meetup.hget("name")
96
+ => ["Redis Meetup"]
97
+ ```
98
+
99
+ Supplying your existing Redis instance
100
+ --------------------------------------
101
+
102
+ You can supply a [Redis](https://github.com/amakawa/redic) instance as
103
+ a second parameter. If you don't, a default instance is created for you:
104
+
105
+ ```ruby
106
+ >> redis = Redis.new(url: "redis://localhost:6379")
107
+ => #<Redis:0x007fa640845f10 ...>
108
+
109
+ >> event = Nest.new("Event", redis)
110
+ => "Event"
111
+
112
+ >> event[:id].call("TYPE")
113
+ => "string"
114
+ ```
115
+
116
+ `Nest` objects respond to `redis` and return a `Redis` instance. It is
117
+ automatically reused when you create a new namespace, and you can reuse it when
118
+ creating a new instance of Nest:
119
+
120
+ ```ruby
121
+ >> event = Nest.new("Event", meetup.redis)
122
+ => "Event"
123
+ ```
124
+
125
+ Nest allows you to execute all the Redis commands that expect a key as the
126
+ first parameter. If you use any other command, the result can be unexpected.
127
+
128
+ Differences with redis-namespace
129
+ --------------------------------
130
+
131
+ [redis-namespace](http://github.com/defunkt/redis-namespace) wraps Redis
132
+ and translates the keys back and forth transparently.
133
+
134
+ Use redis-namespace when you want all your application keys to live in a
135
+ different scope.
136
+
137
+ Use Nest when you want to use the keys to represent structure.
138
+
139
+ Tip: instead of using redis-namespace, it is recommended that you run a
140
+ different instance of `redis-server`. Translating keys back and forth is not
141
+ only delicate, but unnecessary and counterproductive.
142
+
143
+ Differences with Ohm
144
+ --------------------
145
+
146
+ [Ohm](http://ohm.keyvalue.org) lets you map Ruby objects to Redis with
147
+ little effort. It not only alleviates you from the pain of generating
148
+ keys for each object, but also helps you when dealing with references
149
+ between objects.
150
+
151
+ Use Ohm when you want to use Redis as your database.
152
+
153
+ Use Nest when mapping objects with Ohm is not possible or overkill.
154
+
155
+ Tip: Ohm uses Nest internally to deal with keys. Having a good knowledge
156
+ of Nest will let you extend Ohm to suit your needs.
157
+
158
+ Installation
159
+ ------------
160
+
161
+ ```
162
+ $ gem install nest
163
+ ```
data/lib/nest.rb ADDED
@@ -0,0 +1,67 @@
1
+ # Copyright (c) 2010 Michel Martens
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+ #
21
+ require "redis"
22
+
23
+ class Nest
24
+ def initialize(ns, rc = Redis.new)
25
+ @ns = ns.to_s
26
+ @rc = rc
27
+ end
28
+
29
+ def [](key)
30
+ Nest.new("#{@ns}:#{key}", @rc)
31
+ end
32
+
33
+ def redis
34
+ @rc
35
+ end
36
+
37
+ def hash
38
+ @ns.hash
39
+ end
40
+
41
+ def to_ary
42
+ nil
43
+ end
44
+
45
+ def to_str
46
+ @ns
47
+ end
48
+
49
+ alias to_s to_str
50
+ alias to_a to_ary
51
+
52
+ def to_json(*args)
53
+ @ns.to_json(*args)
54
+ end
55
+
56
+ def call(command, *args)
57
+ @rc.call(command, to_s, *args)
58
+ end
59
+
60
+ def inspect
61
+ @ns.inspect
62
+ end
63
+
64
+ def method_missing(m, *args)
65
+ call(m, *args)
66
+ end
67
+ end
data/makefile ADDED
@@ -0,0 +1,7 @@
1
+ .PHONY: test
2
+
3
+ test:
4
+ cutest ./test/*.rb
5
+
6
+ console:
7
+ irb -r ./lib/nest
@@ -0,0 +1,15 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "nest-redis"
3
+ s.version = "4.0.0"
4
+ s.summary = "Object-oriented keys for Redis."
5
+ s.description = "It is a design pattern in key-value databases to use the key to simulate structure, and Nest can take care of that."
6
+ s.license = "MIT"
7
+ s.authors = ["Michel Martens", "Phuong Nguyen"]
8
+ s.email = ["michel@soveran.com", "phuongnd08@gmail.com"]
9
+ s.homepage = "http://github.com/remitano/nest-redis"
10
+
11
+ s.files = `git ls-files`.split("\n")
12
+
13
+ s.add_dependency "redis"
14
+ s.add_development_dependency "cutest"
15
+ end
data/test/helper.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative "../lib/nest"
data/test/nest_test.rb ADDED
@@ -0,0 +1,108 @@
1
+ require_relative "helper"
2
+
3
+ # Creating namespaces.
4
+ scope do
5
+ test "return the namespace" do
6
+ n1 = Nest.new("foo", Redis.new)
7
+ assert "foo" == n1.to_s
8
+ end
9
+
10
+ test "prepend the namespace" do
11
+ n1 = Nest.new("foo")
12
+ assert "foo:bar" == n1["bar"].to_s
13
+ end
14
+
15
+ test "work in more than one level" do
16
+ n1 = Nest.new("foo")
17
+ n2 = Nest.new(n1["bar"])
18
+ assert "foo:bar:baz" == n2["baz"].to_s
19
+ end
20
+
21
+ test "be chainable" do
22
+ n1 = Nest.new("foo")
23
+ assert "foo:bar:baz" == n1["bar"]["baz"].to_s
24
+ end
25
+
26
+ test "accept symbols" do
27
+ n1 = Nest.new(:foo)
28
+ assert "foo:bar" == n1[:bar].to_s
29
+ end
30
+
31
+ test "accept numbers" do
32
+ n1 = Nest.new("foo")
33
+ assert "foo:3" == n1[3].to_s
34
+ end
35
+ end
36
+
37
+ # Operating with Redis.
38
+ scope do
39
+ prepare do
40
+ @redis = Redis.new
41
+ @redis.call("FLUSHDB")
42
+ end
43
+
44
+ test "work if no redis instance was passed" do
45
+ n1 = Nest.new("foo")
46
+ n1.call("SET", "s1")
47
+
48
+ assert "s1" == n1.call("GET")
49
+ end
50
+
51
+ test "work if a redis instance is supplied" do
52
+ n1 = Nest.new("foo", @redis)
53
+ n1.call("SET", "s1")
54
+
55
+ assert "s1" == n1.call("GET")
56
+ end
57
+
58
+ test "pass the redis instance to new keys" do
59
+ n1 = Nest.new("foo", @redis)
60
+
61
+ assert @redis.object_id == n1["bar"].redis.object_id
62
+ end
63
+ end
64
+
65
+
66
+ # Operating with Redis with dynamic methods.
67
+ scope do
68
+ prepare do
69
+ @redis = Redis.new
70
+ @redis.call("FLUSHDB")
71
+ end
72
+
73
+ test "relay missing methods as Redis commands" do
74
+ n1 = Nest.new("foo")
75
+ n1.set("s1")
76
+
77
+ assert "s1" == n1.get
78
+ end
79
+ end
80
+
81
+ # Operations that call to_a and to_ary
82
+ scope do
83
+ test "interaction with array-casting operations" do
84
+ n1 = Nest.new("foo")
85
+
86
+ assert_equal [n1], [n1].flatten
87
+ assert_equal [n1], Array(n1)
88
+ end
89
+ end
90
+
91
+ # Operations that call to_str and to_ary
92
+ scope do
93
+ test "interaction with string-casting operations" do
94
+ n1 = Nest.new("foo")
95
+ s1 = "bar"
96
+
97
+ s2 = s1 + n1
98
+
99
+ assert_equal "barfoo", s1 + n1
100
+ end
101
+
102
+ test "interaction with array-casting operations" do
103
+ n1 = Nest.new("foo")
104
+
105
+ assert_equal [n1], [n1].flatten
106
+ assert_equal [n1], Array(n1)
107
+ end
108
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nest-redis
3
+ version: !ruby/object:Gem::Version
4
+ version: 4.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Michel Martens
8
+ - Phuong Nguyen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2019-04-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: redis
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: cutest
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ description: It is a design pattern in key-value databases to use the key to simulate
43
+ structure, and Nest can take care of that.
44
+ email:
45
+ - michel@soveran.com
46
+ - phuongnd08@gmail.com
47
+ executables: []
48
+ extensions: []
49
+ extra_rdoc_files: []
50
+ files:
51
+ - ".gems"
52
+ - ".gitignore"
53
+ - CHANGELOG.md
54
+ - CONTRIBUTING
55
+ - LICENSE
56
+ - README.md
57
+ - lib/nest.rb
58
+ - makefile
59
+ - nest-redis.gemspec
60
+ - test/helper.rb
61
+ - test/nest_test.rb
62
+ homepage: http://github.com/remitano/nest-redis
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.4.5.2
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Object-oriented keys for Redis.
86
+ test_files: []