Class: Familia::RedisType
- Inherits:
-
Object
- Object
- Familia::RedisType
- 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: Commands, Serialization
Class Attribute Summary collapse
- .db(val = nil) ⇒ Object
-
.parent ⇒ Object
Returns the value of attribute parent.
-
.registered_types ⇒ Object
readonly
Returns the value of attribute registered_types.
- .ttl(val = nil) ⇒ Object
- .uri(val = nil) ⇒ Object
-
.valid_options ⇒ Object
readonly
Returns the value of attribute valid_options.
Instance Attribute Summary collapse
- #dump_method ⇒ Object
- #load_method ⇒ Object
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Class Method Summary collapse
- .inherited(obj) ⇒ Object
-
.register(klass, methname) ⇒ Object
To be called inside every class that inherits RedisType
methnameis the term used for the class and instance methods that are created for the givenklass(e.g. set, list, etc). - .valid_keys_only(opts) ⇒ Object
Instance Method Summary collapse
- #class? ⇒ Boolean
- #db ⇒ Object
-
#initialize(name, opts = {}) ⇒ RedisType
constructor
name: If parent is set, this will be used as the suffix for rediskey. - #parent? ⇒ Boolean
- #parent_class? ⇒ Boolean
- #parent_instance? ⇒ Boolean
- #redis ⇒ Object
-
#rediskey ⇒ Object
Produces the full redis key for this object.
- #ttl ⇒ Object
- #uri ⇒ Object
Methods included from Serialization
#from_redis, #multi_from_redis, #multi_from_redis_with_nil, #to_redis, #update_expiration
Methods included from Commands
#delete!, #echo, #exists?, #expire, #expireat, #move, #persist, #realttl, #rename, #renamenx, #type
Methods included from Base
Constructor Details
#initialize(name, opts = {}) ⇒ RedisType
name: 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).
Uses the redis connection of the parent or the value of opts or Familia.redis (in that order).
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/familia/redistype.rb', line 95 def initialize(name, opts = {}) #Familia.ld " [initializing] #{self.class} #{opts}" @name = name @name = @name.join(Familia.delim) if @name.is_a?(Array) # Remove all keys from the opts that are not in the allowed list @opts = opts || {} @opts = RedisType.valid_keys_only(@opts) init if respond_to? :init end |
Class Attribute Details
.db(val = nil) ⇒ Object
41 42 43 44 |
# File 'lib/familia/redistype.rb', line 41 def db(val = nil) @db = val unless val.nil? @db || parent&.db end |
.parent ⇒ Object
Returns the value of attribute parent.
24 25 26 |
# File 'lib/familia/redistype.rb', line 24 def parent @parent end |
.registered_types ⇒ Object (readonly)
Returns the value of attribute registered_types.
23 24 25 |
# File 'lib/familia/redistype.rb', line 23 def registered_types @registered_types end |
.ttl(val = nil) ⇒ Object
36 37 38 39 |
# File 'lib/familia/redistype.rb', line 36 def ttl(val = nil) @ttl = val unless val.nil? @ttl || parent&.ttl end |
.uri(val = nil) ⇒ Object
46 47 48 49 |
# File 'lib/familia/redistype.rb', line 46 def uri(val = nil) @uri = val unless val.nil? @uri || (parent ? parent.uri : Familia.uri) end |
.valid_options ⇒ Object (readonly)
Returns the value of attribute valid_options.
23 24 25 |
# File 'lib/familia/redistype.rb', line 23 def @valid_options end |
Instance Attribute Details
#dump_method ⇒ Object
168 169 170 |
# File 'lib/familia/redistype.rb', line 168 def dump_method @dump_method || self.class.dump_method end |
#load_method ⇒ Object
172 173 174 |
# File 'lib/familia/redistype.rb', line 172 def load_method @load_method || self.class.load_method end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
64 65 66 |
# File 'lib/familia/redistype.rb', line 64 def name @name end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
64 65 66 |
# File 'lib/familia/redistype.rb', line 64 def opts @opts end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
64 65 66 |
# File 'lib/familia/redistype.rb', line 64 def parent @parent end |
Class Method Details
.inherited(obj) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/familia/redistype.rb', line 51 def inherited(obj) obj.db = db obj.ttl = ttl obj.uri = uri obj.parent = self super(obj) end |
.register(klass, methname) ⇒ Object
To be called inside every class that inherits RedisType methname is the term used for the class and instance methods that are created for the given klass (e.g. set, list, etc)
30 31 32 33 34 |
# File 'lib/familia/redistype.rb', line 30 def register(klass, methname) Familia.ld "[#{self}] Registering #{klass} as #{methname}" @registered_types[methname] = klass end |
.valid_keys_only(opts) ⇒ Object
59 60 61 |
# File 'lib/familia/redistype.rb', line 59 def valid_keys_only(opts) opts.select { |k, _| RedisType..include? k } end |
Instance Method Details
#class? ⇒ Boolean
136 137 138 |
# File 'lib/familia/redistype.rb', line 136 def class? !@opts[:class].to_s.empty? && @opts[:class].is_a?(Familia) end |
#db ⇒ Object
160 161 162 |
# File 'lib/familia/redistype.rb', line 160 def db @opts[:db] || self.class.db end |
#parent? ⇒ Boolean
148 149 150 |
# File 'lib/familia/redistype.rb', line 148 def parent? parent_class? || parent_instance? end |
#parent_class? ⇒ Boolean
144 145 146 |
# File 'lib/familia/redistype.rb', line 144 def parent_class? parent.is_a?(Class) && parent <= Familia::Horreum end |
#parent_instance? ⇒ Boolean
140 141 142 |
# File 'lib/familia/redistype.rb', line 140 def parent_instance? parent.is_a?(Familia::Horreum) end |
#redis ⇒ Object
107 108 109 110 111 |
# File 'lib/familia/redistype.rb', line 107 def redis return @redis if @redis parent? ? parent.redis : Familia.redis(opts[:db]) end |
#rediskey ⇒ Object
Produces the full redis key for this object.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/familia/redistype.rb', line 114 def rediskey Familia.ld "[rediskey] #{name} for #{self.class} (#{opts})" # 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(name) elsif parent_class? # This is a class-level redistype object so the parent class' rediskey # method is defined in Familia::Horreum::ClassMethods. parent.rediskey(name, nil) else # This is a standalone RedisType object where it's name # is the full key. name end end |
#ttl ⇒ Object
156 157 158 |
# File 'lib/familia/redistype.rb', line 156 def ttl @opts[:ttl] || self.class.ttl end |
#uri ⇒ Object
164 165 166 |
# File 'lib/familia/redistype.rb', line 164 def uri @opts[:uri] || self.class.uri end |