Module: Familia::InstanceMethods

Defined in:
lib/familia/object.rb

Instance Method Summary collapse

Instance Method Details

#allkeysObject

def rediskeys

self.class.redis_objects.each do |redis_object_definition|

end

end



361
362
363
364
365
366
367
368
# File 'lib/familia/object.rb', line 361

def allkeys
  # TODO: Use redis_objects instead
  keynames = []
  self.class.suffixes.each do |sfx| 
    keynames << rediskey(sfx)
  end
  keynames
end

#destroy!Object



396
397
398
399
400
401
402
403
# File 'lib/familia/object.rb', line 396

def destroy!
  ret = self.object.delete
  if Familia.debug?
    Familia.trace :DELETED, Familia.redis(self.class.uri), "#{rediskey}: #{ret}", caller.first if Familia.debug?
  end
  self.class.instances.rem self if ret > 0
  ret
end

#exists?Boolean

Returns:

  • (Boolean)


351
352
353
# File 'lib/familia/object.rb', line 351

def exists?
  Familia.redis(self.class.uri).exists rediskey
end

#expire(ttl = nil) ⇒ Object



457
458
459
460
# File 'lib/familia/object.rb', line 457

def expire(ttl=nil)
  ttl ||= self.class.ttl
  Familia.redis(self.class.uri).expire rediskey, ttl.to_i
end

#indexObject



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/familia/object.rb', line 404

def index
  case self.class.index
  when Proc
    self.class.index.call(self)
  when Array
    parts = self.class.index.collect { |meth| 
      unless self.respond_to? meth
        raise NoIndex, "No such method: `#{meth}' for #{self.class}"
      end
      self.send(meth) 
    }
    parts.join Familia.delim
  when Symbol, String
    if self.class.redis_object?(self.class.index.to_sym)
      raise Familia::NoIndex, "Cannot use a RedisObject as an index"
    else
      unless self.respond_to? self.class.index
        raise NoIndex, "No such method: `#{self.class.index}' for #{self.class}"
      end
      self.send( self.class.index)
    end
  else
    raise Familia::NoIndex, self
  end
end

#index=(v) ⇒ Object



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/familia/object.rb', line 429

def index=(v)
  case self.class.index
  when Proc
    raise ArgumentError, "Cannot set a Proc index"
  when Array
    unless Array === v && v.size == self.class.index.size
      raise ArgumentError, "Index mismatch (#{v.size} for #{self.class.index.size})"
    end
    parts = self.class.index.each_with_index { |meth,idx| 
      unless self.respond_to? "#{meth}="
        raise NoIndex, "No such method: `#{meth}=' for #{self.class}"
      end
      self.send("#{meth}=", v[idx]) 
    }
  when Symbol, String
    if self.class.redis_object?(self.class.index.to_sym)
      raise Familia::NoIndex, "Cannot use a RedisObject as an index"
    else
      unless self.respond_to? "#{self.class.index}="
        raise NoIndex, "No such method: `#{self.class.index}=' for #{self.class}"
      end
      self.send("#{self.class.index}=", v)
    end
  else
    raise Familia::NoIndex, self
  end
  
end

#initialize(*args) ⇒ Object

A default initialize method. This will be replaced if a class defines its own initialize method after including Familia. In that case, the replacement must call initialize_redis_objects.



311
312
313
314
# File 'lib/familia/object.rb', line 311

def initialize *args
  initialize_redis_objects
  init *args if respond_to? :init
end

#initialize_redis_objectsObject

This needs to be called in the initialize method of any class that includes Familia.



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/familia/object.rb', line 318

def initialize_redis_objects
  # Generate instances of each RedisObject. These need to be
  # unique for each instance of this class so they can refer
  # to the index of this specific instance.
  #
  # i.e. 
  #     familia_object.rediskey              == v1:bone:INDEXVALUE:object
  #     familia_object.redis_object.rediskey == v1:bone:INDEXVALUE:name
  #
  # See RedisObject.install_redis_object
  self.class.redis_objects.each_pair do |name, redis_object_definition|
    klass, opts = redis_object_definition.klass, redis_object_definition.opts
    opts ||= {}
    opts[:parent] ||= self
    redis_object = klass.new name, opts
    redis_object.freeze
    self.instance_variable_set "@#{name}", redis_object
  end
end

#raw(suffix = nil) ⇒ Object



470
471
472
473
# File 'lib/familia/object.rb', line 470

def raw(suffix=nil)
  suffix ||= :object
  Familia.redis(self.class.uri).get rediskey(suffix)
end

#realttlObject



461
462
463
# File 'lib/familia/object.rb', line 461

def realttl
  Familia.redis(self.class.uri).ttl rediskey
end

#redisObject



338
339
340
# File 'lib/familia/object.rb', line 338

def redis
  self.class.redis
end

#redisinfoObject



342
343
344
345
346
347
348
349
350
# File 'lib/familia/object.rb', line 342

def redisinfo
  info = {
    :uri  => self.class.uri,
    :db   => self.class.db,
    :key  => rediskey,
    :type => redistype,
    :ttl  => realttl
  }
end

#rediskey(suffix = nil, ignored = nil) ⇒ Object

suffix is the value to be used at the end of the redis key + ignored+ is literally ignored. It’s around to maintain consistency with the class version of this method. (RedisObject#rediskey may call against a class or instance).

Raises:



373
374
375
376
377
378
379
380
381
382
# File 'lib/familia/object.rb', line 373

def rediskey(suffix=nil, ignored=nil)
  Familia.info "[#{self.class}] something was ignored" unless ignored.nil?
  raise Familia::NoIndex, self.class if index.to_s.empty?
  if suffix.nil?
    suffix = self.class.suffix.kind_of?(Proc) ? 
                 self.class.suffix.call(self) : 
                 self.class.suffix
  end
  self.class.rediskey self.index, suffix
end

#redistype(suffix = nil) ⇒ Object



480
481
482
# File 'lib/familia/object.rb', line 480

def redistype(suffix=nil)
  Familia.redis(self.class.uri).type rediskey(suffix)
end

#redisuri(suffix = nil) ⇒ Object



474
475
476
477
478
479
# File 'lib/familia/object.rb', line 474

def redisuri(suffix=nil)
  u = URI.parse self.class.uri.to_s
  u.db ||= self.class.db.to_s
  u.key = rediskey(suffix)
  u
end

#saveObject



383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/familia/object.rb', line 383

def save
  #Familia.trace :SAVE, Familia.redis(self.class.uri), redisuri, caller.first if Familia.debug?
  preprocess if respond_to?(:preprocess)
  self.update_time if self.respond_to?(:update_time)
  # TODO: Check here (run checkup)
  ret = self.object.set self               # object is a name reserved by Familia
  unless ret.nil?
    now = Time.now.utc.to_i
    self.class.instances.add now, self     # use this set instead of Klass.keys
    self.object.update_expiration self.ttl # does nothing unless if not specified
  end
  true
end

#shortidObject

Finds the shortest available unique key (lower limit of 6)



484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/familia/object.rb', line 484

def shortid
  len = 6
  loop do
    begin
      self.class.expand(@id.shorten(len))
      break
    rescue Familia::NonUniqueKey
      len += 1
    end
  end
  @id.shorten(len) 
end

#ttlObject



467
468
469
# File 'lib/familia/object.rb', line 467

def ttl
  @ttl || self.class.ttl
end

#ttl=(v) ⇒ Object



464
465
466
# File 'lib/familia/object.rb', line 464

def ttl=(v)
  @ttl = v.to_i
end