redis-struct 1.0.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 +7 -0
- data/lib/redis-struct.rb +228 -0
- metadata +59 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 60698b761c41da3306c3a7cbf4c5c0b70a743193
|
4
|
+
data.tar.gz: 71db081e8f77f9143992dcaf5d87821fd0fedfb7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 164b1bf8c7bb4bae8376c16d73d1db1e23788c9221de2807bc66de665825fdd79f1cfe0383724541f91d1316e96aaa295e1260b58dd6074183e643acac121764
|
7
|
+
data.tar.gz: 1c2ee1c445cddfb9d1b903960f7f5cdca5f7722628531c1c9a2f87a89b2bf63864c5b3ab422f2d668da90b9413a67b932119fde237774cd28eaa7f1af4142f45
|
data/lib/redis-struct.rb
ADDED
@@ -0,0 +1,228 @@
|
|
1
|
+
|
2
|
+
require 'ostruct'
|
3
|
+
require 'redis'
|
4
|
+
|
5
|
+
class RedisHash < Hash
|
6
|
+
|
7
|
+
@fixed = false
|
8
|
+
|
9
|
+
def config_database(database)
|
10
|
+
@database = database
|
11
|
+
end
|
12
|
+
|
13
|
+
def config_prefix(prefix)
|
14
|
+
@prefix = prefix
|
15
|
+
end
|
16
|
+
|
17
|
+
def config_suffix(suffix)
|
18
|
+
@suffix = suffix
|
19
|
+
end
|
20
|
+
|
21
|
+
# def config_redis_struct(redis_struct)
|
22
|
+
# @redis_struct = redis_struct
|
23
|
+
# end
|
24
|
+
|
25
|
+
def config_hash(hash)
|
26
|
+
@hash = hash
|
27
|
+
end
|
28
|
+
|
29
|
+
def []=(key,value)
|
30
|
+
@database.set prefix(key), value
|
31
|
+
end
|
32
|
+
|
33
|
+
def [](key)
|
34
|
+
@database.get prefix(key)
|
35
|
+
end
|
36
|
+
|
37
|
+
def each_key(&block)
|
38
|
+
keys = @database.keys prefix("*")
|
39
|
+
keys.each do |key|
|
40
|
+
yield unprefix(key)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def each_pair # (&block) ## I don't think this works.
|
45
|
+
each_key do |key|
|
46
|
+
value = self[key]
|
47
|
+
yield unprefix(key), value
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def dup
|
52
|
+
self
|
53
|
+
end
|
54
|
+
|
55
|
+
def to_h
|
56
|
+
hash = {}
|
57
|
+
each_key do |key|
|
58
|
+
hash[key] = self[key]
|
59
|
+
end
|
60
|
+
hash
|
61
|
+
end
|
62
|
+
|
63
|
+
def prefix(key)
|
64
|
+
"#{@prefix}:#{get_suffix}:#{key}"
|
65
|
+
end
|
66
|
+
|
67
|
+
def get_suffix
|
68
|
+
@suffix || self.object_id
|
69
|
+
end
|
70
|
+
|
71
|
+
def unprefix(key)
|
72
|
+
key.sub /^#{@prefix}:#{get_suffix}:/, ''
|
73
|
+
end
|
74
|
+
|
75
|
+
def prefix_with_id(key)
|
76
|
+
"#{@prefix}:#{self.object_id}:#{key}"
|
77
|
+
end
|
78
|
+
|
79
|
+
def add_hash
|
80
|
+
@hash.each_pair do |key,value|
|
81
|
+
self[key] = value
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def delete(key)
|
86
|
+
@database.del prefix(key)
|
87
|
+
end
|
88
|
+
#
|
89
|
+
# private
|
90
|
+
#
|
91
|
+
# # An internal method that uses the name of @redis_struct when possible,
|
92
|
+
# # otherwise the RedisHash's object id.
|
93
|
+
# def fix
|
94
|
+
# begin
|
95
|
+
# unless @fixed
|
96
|
+
# @fixed = true
|
97
|
+
# add_hash
|
98
|
+
# end
|
99
|
+
# result = @redis_struct.very_old_name
|
100
|
+
# # puts 'not rescued'
|
101
|
+
# rescue Exception => e
|
102
|
+
# p e
|
103
|
+
# # puts 'rescued'
|
104
|
+
# result = self.object_id
|
105
|
+
# end
|
106
|
+
# result
|
107
|
+
# end
|
108
|
+
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
|
113
|
+
class RedisStruct < OpenStruct
|
114
|
+
|
115
|
+
|
116
|
+
# This resets unnecessary methods for users'
|
117
|
+
# convenience.
|
118
|
+
|
119
|
+
# Uncomment these to access old methods
|
120
|
+
|
121
|
+
# alias :very_old_class :class
|
122
|
+
# alias :very_old_display :display
|
123
|
+
# alias :very_old_clone :clone
|
124
|
+
# alias :very_old_trust :trust
|
125
|
+
# alias :very_old_method :method
|
126
|
+
# alias :very_old_taint :taint
|
127
|
+
|
128
|
+
undef :taint
|
129
|
+
undef :method
|
130
|
+
undef :trust
|
131
|
+
undef :clone
|
132
|
+
undef :display
|
133
|
+
undef :class
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
# An RedisStruct wraps the OpenStruct class, which uses method_missing
|
138
|
+
# to store values in a hash. Instead, RedisStruct stores these values
|
139
|
+
# in Redis, providing a pleasant way of interfacing with $redis.
|
140
|
+
#
|
141
|
+
# #When $redis = Redis.new :host => '0.0.0.0', :port => '6379'
|
142
|
+
# example = RedisStruct.new($redis)
|
143
|
+
#
|
144
|
+
# example.color = 'blue'
|
145
|
+
#
|
146
|
+
# example.color # => 'blue'
|
147
|
+
#
|
148
|
+
# The hash will load any data you want into the RedisStruct, without
|
149
|
+
# having to iterate over it. And the prefix is what's used to
|
150
|
+
# store in $redis. Good luck!
|
151
|
+
|
152
|
+
def initialize(hash=nil, prefix = 'redis_struct', suffix = nil, database)
|
153
|
+
@table = RedisHash.new
|
154
|
+
@table.config_database database
|
155
|
+
@table.config_prefix prefix
|
156
|
+
|
157
|
+
if suffix
|
158
|
+
@table.config_suffix suffix
|
159
|
+
end
|
160
|
+
|
161
|
+
# Enables the user to load a hash into the database
|
162
|
+
# Hash will be compiled later - once the RedisStruct's name is known
|
163
|
+
@table.config_hash hash
|
164
|
+
@table.add_hash if hash
|
165
|
+
|
166
|
+
end
|
167
|
+
|
168
|
+
def get_redis_hash_object_id
|
169
|
+
@table.object_id
|
170
|
+
end
|
171
|
+
|
172
|
+
def to_str
|
173
|
+
to_h.to_s
|
174
|
+
end
|
175
|
+
|
176
|
+
# Rewriting so RedisStruct#to_h doesn't call RedisHash#dup
|
177
|
+
def to_h
|
178
|
+
@table.to_h
|
179
|
+
end
|
180
|
+
|
181
|
+
def inspect
|
182
|
+
"#<redis-struct : #{to_h}>"
|
183
|
+
end
|
184
|
+
|
185
|
+
end
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
|
190
|
+
# Examples
|
191
|
+
|
192
|
+
# It's generally better to use an RedisStruct than make
|
193
|
+
# a RedisHash directly. RedisHashes are more adapters
|
194
|
+
# than anything else.
|
195
|
+
#
|
196
|
+
#
|
197
|
+
#
|
198
|
+
# $redis = Redis.new :host => '0.0.0.0', :port => '6379'
|
199
|
+
#
|
200
|
+
# icecream = RedisHash.new
|
201
|
+
# icecream.config_database $redis
|
202
|
+
# icecream.config_prefix 'hello'
|
203
|
+
# icecream['color'] = 'blue'
|
204
|
+
#
|
205
|
+
# p icecream['color']
|
206
|
+
# puts
|
207
|
+
# puts '-------------'
|
208
|
+
# puts
|
209
|
+
#
|
210
|
+
#
|
211
|
+
#
|
212
|
+
# Snowcone = RedisStruct.new({name: 'blue'}, $redis)
|
213
|
+
#
|
214
|
+
# Snowcone.class = 'hi'
|
215
|
+
#
|
216
|
+
# p Snowcone.class
|
217
|
+
# p Snowcone.name
|
218
|
+
#
|
219
|
+
# Snowcone.kind = 'sheep'
|
220
|
+
# Snowcone.blue = 'animal'
|
221
|
+
#
|
222
|
+
# p Snowcone.kind
|
223
|
+
# p Snowcone.blue
|
224
|
+
#
|
225
|
+
# p Snowcone.color
|
226
|
+
#
|
227
|
+
#
|
228
|
+
# puts Snowcone.name
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redis-struct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Charles Chamberlain
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-10 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
|
+
description: OpenStructs meet Redis
|
28
|
+
email: charles@charlesetc.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- lib/redis-struct.rb
|
34
|
+
homepage: https://github.com/Charlesetc/redis-struct#redis-struct
|
35
|
+
licenses:
|
36
|
+
- MIT
|
37
|
+
metadata: {}
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 2.0.5
|
55
|
+
signing_key:
|
56
|
+
specification_version: 4
|
57
|
+
summary: OpenStructs meet Redis
|
58
|
+
test_files: []
|
59
|
+
has_rdoc:
|