Class: RedisStruct
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- RedisStruct
- Defined in:
- lib/redis-struct.rb
Instance Method Summary collapse
- #get_redis_hash_object_id ⇒ Object
-
#initialize(hash = nil, prefix = 'redis_struct', suffix = nil, database) ⇒ RedisStruct
constructor
An RedisStruct wraps the OpenStruct class, which uses method_missing to store values in a hash.
- #inspect ⇒ Object
-
#to_h ⇒ Object
Rewriting so RedisStruct#to_h doesn’t call RedisHash#dup.
- #to_str ⇒ Object
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_id ⇒ Object
168 169 170 |
# File 'lib/redis-struct.rb', line 168 def get_redis_hash_object_id @table.object_id end |
#inspect ⇒ Object
181 182 183 |
# File 'lib/redis-struct.rb', line 181 def inspect "#<redis-struct : #{to_h}>" end |
#to_h ⇒ Object
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_str ⇒ Object
172 173 174 |
# File 'lib/redis-struct.rb', line 172 def to_str to_h.to_s end |