Auto-extended into a class that includes Familia
# File lib/familia/object.rb, line 167 def all(suffix=:object) # objects that could not be parsed will be nil keys(suffix).collect { |k| from_key(k) }.compact end
# File lib/familia/object.rb, line 171 def any?(filter='*') size(filter) > 0 end
# File lib/familia/object.rb, line 199 def class_redis_objects @class_redis_objects ||= {} @class_redis_objects end
# File lib/familia/object.rb, line 203 def class_redis_objects? name class_redis_objects.has_key? name.to_s.to_sym end
# File lib/familia/object.rb, line 195 def class_redis_objects_order @class_redis_objects_order ||= [] @class_redis_objects_order end
# File lib/familia/object.rb, line 217 def create *args me = from_array *args raise "#{self} exists: #{me.rediskey}" if me.exists? me.save me end
# File lib/familia/object.rb, line 138 def db v=nil @db = v unless v.nil? @db || (parent ? parent.db : nil) end
# File lib/familia/object.rb, line 272 def destroy! idx, suffix=:object ret = Familia.redis(self.uri).del rediskey(idx, suffix) Familia.trace :DELETED, Familia.redis(self.uri), "#{rediskey(idx, suffix)}: #{ret}", caller if Familia.debug? ret end
# File lib/familia/object.rb, line 265 def exists? idx, suffix=:object return false if idx.to_s.empty? objkey = rediskey idx, suffix ret = Familia.redis(self.uri).exists objkey Familia.trace :EXISTS, Familia.redis(self.uri), "#{rediskey(idx, suffix)} #{ret}", caller if Familia.debug? ret end
# File lib/familia/object.rb, line 289 def expand(short_idx, suffix=self.suffix) expand_key = Familia.rediskey(self.prefix, "#{short_idx}*", suffix) Familia.trace :EXPAND, Familia.redis(self.uri), expand_key, caller.first if Familia.debug? list = Familia.redis(self.uri).keys expand_key case list.size when 0 nil when 1 matches = list.first.match(/\A#{Familia.rediskey(prefix)}\:(.+?)\:#{suffix}/) || [] matches[1] else raise Familia::NonUniqueKey, "Short key returned more than 1 match" end end
# File lib/familia/object.rb, line 64 def extended(obj) obj.db = self.db obj.ttl = self.ttl obj.uri = self.uri obj.parent = self obj.class_zset :instances, :class => obj, :reference => true Familia.classes << obj end
# File lib/familia/object.rb, line 277 def find suffix='*' list = Familia.redis(self.uri).keys(rediskey('*', suffix)) || [] end
# File lib/familia/object.rb, line 160 def flushdb Familia.info "flushing #{uri}" redis.flushdb end
Note idx needs to be an appropriate index for the given class. If the index is multi-value it must be passed as an Array in the proper order. Does not call save.
# File lib/familia/object.rb, line 247 def from_index idx obj = new obj.index = idx obj end
# File lib/familia/object.rb, line 252 def from_key objkey raise ArgumentError, "Empty key" if objkey.to_s.empty? Familia.trace :LOAD, Familia.redis(self.uri), objkey, caller if Familia.debug? obj = Familia::String.new objkey, :class => self obj.value end
# File lib/familia/object.rb, line 258 def from_redis idx, suffix=:object return nil if idx.to_s.empty? objkey = rediskey idx, suffix #Familia.trace :FROMREDIS, Familia.redis(self.uri), objkey, caller.first if Familia.debug? me = from_key objkey me end
# File lib/familia/object.rb, line 129 def from_redisdump dump dump # todo end
# File lib/familia/object.rb, line 143 def host(host=nil) @host = host if host; @host end
# File lib/familia/object.rb, line 144 def host=(host) @host = host end
TODO: grab db, ttl, uri from parent
def parent=(a) @parent = a end def parent(a=nil) @parent = a if a; @parent end
# File lib/familia/object.rb, line 187 def index(i=nil, &blk) @index = i || blk if i || !blk.nil? @index ||= Familia.index @index end
# File lib/familia/object.rb, line 55 def inherited(obj) obj.db = self.db obj.uri = self.uri obj.ttl = self.ttl obj.parent = self obj.class_zset :instances, :class => obj, :reference => true Familia.classes << obj super(obj) end
Creates a class method called name that returns an instance of the RedisObject klass
# File lib/familia/object.rb, line 103 def install_class_redis_object name, klass, opts raise ArgumentError, "Name is blank" if name.to_s.empty? name = name.to_s.to_sym opts = opts.nil? ? {} : opts.clone opts[:parent] = self unless opts.has_key?(:parent) # TODO: investigate using metaclass.redis_objects class_redis_objects_order << name class_redis_objects[name] = OpenStruct.new class_redis_objects[name].name = name class_redis_objects[name].klass = klass class_redis_objects[name].opts = opts # An accessor method created in the metclass will # access the instance variables for this class. metaclass.send :attr_reader, name metaclass.send :define_method, "#{name}=" do |v| send(name).replace v end metaclass.send :define_method, "#{name}?" do !send(name).empty? end redis_object = klass.new name, opts redis_object.freeze self.instance_variable_set("@#{name}", redis_object) class_redis_objects[name] end
Creates an instance method called name that returns an instance of the RedisObject klass
# File lib/familia/object.rb, line 75 def install_redis_object name, klass, opts raise ArgumentError, "Name is blank" if name.to_s.empty? name = name.to_s.to_sym opts ||= {} redis_objects_order << name redis_objects[name] = OpenStruct.new redis_objects[name].name = name redis_objects[name].klass = klass redis_objects[name].opts = opts self.send :attr_reader, name define_method "#{name}=" do |v| self.send(name).replace v end define_method "#{name}?" do !self.send(name).empty? end redis_objects[name] end
# File lib/familia/object.rb, line 164 def keys(suffix=nil) self.redis.keys(rediskey('*',suffix)) || [] end
Returns an instance based on idx otherwise it creates and saves a new instance base on idx. See from_index
# File lib/familia/object.rb, line 237 def load_or_create idx return from_redis(idx) if exists?(idx) obj = from_index idx obj.save obj end
# File lib/familia/object.rb, line 223 def multiget(*ids) ids = rawmultiget(*ids) ids.compact.collect { |json| self.from_json(json) }.compact end
# File lib/familia/object.rb, line 145 def port(port=nil) @port = port if port; @port end
# File lib/familia/object.rb, line 146 def port=(port) @port = port end
# File lib/familia/object.rb, line 183 def prefix(a=nil) @prefix = a if a; @prefix || self.name.downcase.gsub('::', Familia.delim).to_sym end
# File lib/familia/object.rb, line 182 def prefix=(a) @prefix = a end
# File lib/familia/object.rb, line 94 def qstamp quantum=nil, pattern=nil, now=Familia.now quantum ||= ttl || 10.minutes pattern ||= '%H%M' rounded = now - (now % quantum) Time.at(rounded).utc.strftime(pattern) end
# File lib/familia/object.rb, line 227 def rawmultiget(*ids) ids.collect! { |objid| rediskey(objid) } return [] if ids.compact.empty? Familia.trace :MULTIGET, self.redis, "#{ids.size}: #{ids}", caller if Familia.debug? ids = self.redis.mget *ids end
# File lib/familia/object.rb, line 157 def redis Familia.redis uri end
# File lib/familia/object.rb, line 206 def redis_object? name redis_objects.has_key? name.to_s.to_sym end
# File lib/familia/object.rb, line 213 def redis_objects @redis_objects ||= {} @redis_objects end
# File lib/familia/object.rb, line 209 def redis_objects_order @redis_objects_order ||= [] @redis_objects_order end
idx can be a value or an Array of values used to create the index. We don't enforce a default suffix; that's left up to the instance. A nil suffix will not be included in the key.
# File lib/familia/object.rb, line 283 def rediskey idx, suffix=self.suffix raise RuntimeError, "No index for #{self}" if idx.to_s.empty? idx = Familia.join *idx if Array === idx idx &&= idx.to_s Familia.rediskey(prefix, idx, suffix) end
# File lib/familia/object.rb, line 174 def size(filter='*') self.redis.keys(rediskey(filter)).compact.size end
# File lib/familia/object.rb, line 177 def suffix(a=nil, &blk) @suffix = a || blk if a || !blk.nil? val = @suffix || Familia.default_suffix val end
# File lib/familia/object.rb, line 192 def suffixes redis_objects.keys.uniq end
# File lib/familia/object.rb, line 133 def ttl v=nil @ttl = v unless v.nil? @ttl || (parent ? parent.ttl : nil) end
Generated with the Darkfish Rdoc Generator 2.