Class: Familia::RedisType
- Inherits:
-
Object
- Object
- Familia::RedisType
- Extended by:
- Features, ClassMethods
- Includes:
- Base, Commands, Serialization
- Defined in:
- lib/familia/redistype.rb,
lib/familia/redistype/commands.rb,
lib/familia/redistype/serialization.rb
Overview
rubocop:disable all
Defined Under Namespace
Modules: ClassMethods, Commands, Serialization
Class Attribute Summary collapse
-
.db ⇒ Object
writeonly
Sets the attribute db.
-
.has_relations ⇒ Object
readonly
Returns the value of attribute has_relations.
-
.parent ⇒ Object
Returns the value of attribute parent.
-
.registered_types ⇒ Object
readonly
Returns the value of attribute registered_types.
-
.uri ⇒ Object
writeonly
Sets the attribute uri.
-
.valid_options ⇒ Object
readonly
Returns the value of attribute valid_options.
Instance Attribute Summary collapse
- #dump_method ⇒ Object
-
#keystring ⇒ Object
readonly
Returns the value of attribute keystring.
- #load_method ⇒ Object
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Attributes included from Features
Instance Method Summary collapse
- #class? ⇒ Boolean
- #db ⇒ Object
-
#initialize(keystring, opts = {}) ⇒ RedisType
constructor
keystring
: If parent is set, this will be used as the suffix for rediskey. - #parent? ⇒ Boolean
- #parent_class? ⇒ Boolean
- #parent_instance? ⇒ Boolean
- #redis ⇒ Object
-
#rediskey ⇒ String
Produces the full Redis key for this object.
- #uri ⇒ Object
Methods included from Features
Methods included from ClassMethods
has_relations?, inherited, register, valid_keys_only
Methods included from Serialization
#deserialize_value, #deserialize_values, #deserialize_values_with_nil, #serialize_value
Methods included from Commands
#delete!, #echo, #exists?, #expire, #expireat, #move, #persist, #realttl, #rename, #renamenx, #type
Methods included from Base
add_feature, #generate_id, #update_expiration, #uuid
Constructor Details
#initialize(keystring, opts = {}) ⇒ RedisType
keystring
: If parent is set, this will be used as the suffix for rediskey. Otherwise this becomes the value of the key. If this is an Array, the elements will be joined.
Options:
:class => A class that responds to Familia.load_method and Familia.dump_method. These will be used when loading and saving data from/to redis to unmarshal/marshal the class.
:parent => The Familia object that this redistype object belongs to. This can be a class that includes Familia or an instance.
:ttl => the time to live in seconds. When not nil, this will set the redis expire for this key whenever #save is called. You can also call it explicitly via #update_expiration.
:default => the default value (String-only)
:db => the redis database to use (ignored if :redis is used).
:redis => an instance of Redis.
:key => a hardcoded key to use instead of the deriving the from the name and parent (e.g. a derived key: customer:custid:secret_counter).
:suffix => the suffix to use for the key (e.g. ‘scores’ in customer:custid:scores). :prefix => the prefix to use for the key (e.g. ‘customer’ in customer:custid:scores).
Connection precendence: uses the redis connection of the parent or the value of opts or Familia.redis (in that order).
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/familia/redistype.rb', line 104 def initialize(keystring, opts = {}) #Familia.ld " [initializing] #{self.class} #{opts}" @keystring = keystring @keystring = @keystring.join(Familia.delim) if @keystring.is_a?(Array) # Remove all keys from the opts that are not in the allowed list @opts = opts || {} @opts = RedisType.valid_keys_only(@opts) # Apply the options to instance method setters of the same name @opts.each do |k, v| # Bewarde logging :parent instance here implicitly calls #to_s which for # some classes could include the identifier which could still be nil at # this point. This would result in a Familia::Problem being raised. So # to be on the safe-side here until we have a better understanding of # the issue, we'll just log the class name for each key-value pair. Familia.ld " [setting] #{k} #{v.class}" send(:"#{k}=", v) if respond_to? :"#{k}=" end init if respond_to? :init end |
Class Attribute Details
.db=(value) ⇒ Object (writeonly)
Sets the attribute db
28 29 30 |
# File 'lib/familia/redistype.rb', line 28 def db=(value) @db = value end |
.has_relations ⇒ Object (readonly)
Returns the value of attribute has_relations.
26 27 28 |
# File 'lib/familia/redistype.rb', line 26 def has_relations @has_relations end |
.parent ⇒ Object
Returns the value of attribute parent.
27 28 29 |
# File 'lib/familia/redistype.rb', line 27 def parent @parent end |
.registered_types ⇒ Object (readonly)
Returns the value of attribute registered_types.
26 27 28 |
# File 'lib/familia/redistype.rb', line 26 def registered_types @registered_types end |
.uri=(value) ⇒ Object (writeonly)
Sets the attribute uri
28 29 30 |
# File 'lib/familia/redistype.rb', line 28 def uri=(value) @uri = value end |
.valid_options ⇒ Object (readonly)
Returns the value of attribute valid_options.
26 27 28 |
# File 'lib/familia/redistype.rb', line 26 def @valid_options end |
Instance Attribute Details
#dump_method ⇒ Object
211 212 213 |
# File 'lib/familia/redistype.rb', line 211 def dump_method @dump_method || self.class.dump_method end |
#keystring ⇒ Object (readonly)
Returns the value of attribute keystring.
70 71 72 |
# File 'lib/familia/redistype.rb', line 70 def keystring @keystring end |
#load_method ⇒ Object
215 216 217 |
# File 'lib/familia/redistype.rb', line 215 def load_method @load_method || self.class.load_method end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
70 71 72 |
# File 'lib/familia/redistype.rb', line 70 def opts @opts end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
70 71 72 |
# File 'lib/familia/redistype.rb', line 70 def parent @parent end |
Instance Method Details
#class? ⇒ Boolean
183 184 185 |
# File 'lib/familia/redistype.rb', line 183 def class? !@opts[:class].to_s.empty? && @opts[:class].is_a?(Familia) end |
#db ⇒ Object
203 204 205 |
# File 'lib/familia/redistype.rb', line 203 def db @opts[:db] || self.class.db end |
#parent? ⇒ Boolean
195 196 197 |
# File 'lib/familia/redistype.rb', line 195 def parent? parent_class? || parent_instance? end |
#parent_class? ⇒ Boolean
191 192 193 |
# File 'lib/familia/redistype.rb', line 191 def parent_class? parent.is_a?(Class) && parent <= Familia::Horreum end |
#parent_instance? ⇒ Boolean
187 188 189 |
# File 'lib/familia/redistype.rb', line 187 def parent_instance? parent.is_a?(Familia::Horreum) end |
#redis ⇒ Object
127 128 129 130 131 |
# File 'lib/familia/redistype.rb', line 127 def redis return @redis if @redis parent? ? parent.redis : Familia.redis(opts[:db]) end |
#rediskey ⇒ String
Produces the full Redis key for this object.
This method determines the appropriate Redis key based on the context of the RedisType object:
-
If a hardcoded key is set in the options, it returns that key.
-
For instance-level RedisType objects, it uses the parent instance’s rediskey method.
-
For class-level RedisType objects, it uses the parent class’s rediskey method.
-
For standalone RedisType objects, it uses the keystring as the full Redis key.
For class-level RedisType objects (parent_class? == true):
-
The suffix is optional and used to differentiate between different types of objects.
-
If no suffix is provided, the class’s default suffix is used (via the self.suffix method).
-
If a nil suffix is explicitly passed, it won’t appear in the resulting Redis key.
-
Passing nil as the suffix is how class-level RedisType objects are created without the global default ‘object’ suffix.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/familia/redistype.rb', line 163 def rediskey # Return the hardcoded key if it's set. This is useful for # support legacy keys that aren't derived in the same way. return opts[:key] if opts[:key] if parent_instance? # This is an instance-level redistype object so the parent instance's # rediskey method is defined in Familia::Horreum::InstanceMethods. parent.rediskey(keystring) elsif parent_class? # This is a class-level redistype object so the parent class' rediskey # method is defined in Familia::Horreum::ClassMethods. parent.rediskey(keystring, nil) else # This is a standalone RedisType object where it's keystring # is the full redis key. keystring end end |
#uri ⇒ Object
207 208 209 |
# File 'lib/familia/redistype.rb', line 207 def uri @opts[:uri] || self.class.uri end |