Class: Blendris::RedisNode
- Inherits:
-
Object
- Object
- Blendris::RedisNode
- Includes:
- RedisAccessor
- Defined in:
- lib/blendris/node.rb
Overview
RedisNode is used to compose all Redis value wrapper classes.
Direct Known Subclasses
RedisInteger, RedisList, RedisReferenceBase, RedisSet, RedisSortedSet, RedisString
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Instance Method Summary collapse
- #clear ⇒ Object
- #exists? ⇒ Boolean
-
#get ⇒ Object
Retrieve the value of this object and cast it.
-
#initialize(key, options = {}) ⇒ RedisNode
constructor
Initialize a new node, which represents a basic Redis data object.
- #notify_changed ⇒ Object
-
#rename(newkey) ⇒ Object
Rename this key to the given new key name.
-
#set(value) ⇒ Object
Set this object’s value to be the given value.
- #type ⇒ Object
Methods included from RedisAccessor
#generate_key, #in_temporary_set, redis, #redis
Methods included from Utils
#blank, #camelize, #constantize, #pairify, #sanitize_key
Constructor Details
#initialize(key, options = {}) ⇒ RedisNode
Initialize a new node, which represents a basic Redis data object.
Parameters ===
-
:on_change - Pass a block to be run within the context of this object when its value is changed.
-
:default - Use the given value as a default value.
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/blendris/node.rb', line 18 def initialize(key, = {}) @key = sanitize_key(key) @default = [:default] @options = @on_change = [:on_change] if @default && !redis.exists(self.key) redis.set key, self.class.cast_to_redis(@default, @options) end end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
9 10 11 |
# File 'lib/blendris/node.rb', line 9 def key @key end |
Instance Method Details
#clear ⇒ Object
63 64 65 66 67 |
# File 'lib/blendris/node.rb', line 63 def clear redis.del key ensure notify_changed end |
#exists? ⇒ Boolean
73 74 75 |
# File 'lib/blendris/node.rb', line 73 def exists? redis.exists key end |
#get ⇒ Object
Retrieve the value of this object and cast it.
-
The method cast_from_redis should be overridden by classes that include this module. It is used to convert the redis string to this specific object type.
50 51 52 |
# File 'lib/blendris/node.rb', line 50 def get self.class.cast_from_redis redis.get(self.key), @options end |
#notify_changed ⇒ Object
77 78 79 |
# File 'lib/blendris/node.rb', line 77 def notify_changed @on_change.call if @on_change end |
#rename(newkey) ⇒ Object
Rename this key to the given new key name.
55 56 57 58 59 60 61 |
# File 'lib/blendris/node.rb', line 55 def rename(newkey) redis.rename @key, sanitize_key(newkey) @key = newkey ensure notify_changed end |
#set(value) ⇒ Object
Set this object’s value to be the given value.
-
The method cast_to_redis should be overridden by classes that include this module, as it is used to determine how to convert the given value to a redis string.
-
The on_change block is always called after this method.
35 36 37 38 39 40 41 42 43 |
# File 'lib/blendris/node.rb', line 35 def set(value) if value redis.set key, self.class.cast_to_redis(value, @options) else redis.del key end ensure notify_changed end |
#type ⇒ Object
69 70 71 |
# File 'lib/blendris/node.rb', line 69 def type redis.type key end |