class RedisStruct

Public Class Methods

new(hash=nil, prefix = 'redis_struct', suffix = nil, database) click to toggle source

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!

# 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

Public Instance Methods

get_redis_hash_object_id() click to toggle source
# File lib/redis-struct.rb, line 168
def get_redis_hash_object_id
        @table.object_id
end
inspect() click to toggle source
# File lib/redis-struct.rb, line 181
def inspect
        "#<redis-struct : #{to_h}>"
end
to_h() click to toggle source

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

# File lib/redis-struct.rb, line 177
def to_h
        @table.to_h
end
to_str() click to toggle source
# File lib/redis-struct.rb, line 172
def to_str
        to_h.to_s
end