Class: RedisStruct

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/redis-struct.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil, prefix = 'redis_struct', suffix = nil, database) ⇒ RedisStruct

An RedisStruct wraps the OpenStruct class, which uses method_missing to store values in a hash. Instead, RedisStruct stores these values in Redis, providing a pleasant way of interfacing with $redis.

#When $redis = Redis.new :host => ‘0.0.0.0’, :port => ‘6379’ example = RedisStruct.new($redis)

example.color = ‘blue’

example.color # => ‘blue’

The hash will load any data you want into the RedisStruct, without having to iterate over it. And the prefix is what’s used to store in $redis. Good luck!



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/redis-struct.rb', line 152

def initialize(hash=nil, prefix = 'redis_struct', suffix = nil, database)
  @table = RedisHash.new
@table.config_database database
@table.config_prefix prefix

if suffix
	@table.config_suffix suffix
end

# Enables the user to load a hash into the database
# Hash will be compiled later - once the RedisStruct's name is known
@table.config_hash hash
@table.add_hash if hash

end

Instance Method Details

#get_redis_hash_object_idObject



168
169
170
# File 'lib/redis-struct.rb', line 168

def get_redis_hash_object_id
	@table.object_id
end

#inspectObject



181
182
183
# File 'lib/redis-struct.rb', line 181

def inspect
	"#<redis-struct : #{to_h}>"
end

#to_hObject

Rewriting so RedisStruct#to_h doesn’t call RedisHash#dup



177
178
179
# File 'lib/redis-struct.rb', line 177

def to_h
	@table.to_h
end

#to_strObject



172
173
174
# File 'lib/redis-struct.rb', line 172

def to_str
	to_h.to_s
end