Module: Familia::InstanceMethods
- Defined in:
- lib/familia/object.rb
Instance Method Summary collapse
-
#allkeys ⇒ Object
def rediskeys self.class.redis_objects.each do |redis_object_definition| end end.
- #destroy! ⇒ Object
- #exists? ⇒ Boolean
- #expire(ttl = nil) ⇒ Object
- #index ⇒ Object
- #index=(v) ⇒ Object
-
#initialize(*args) ⇒ Object
A default initialize method.
-
#initialize_redis_objects ⇒ Object
This needs to be called in the initialize method of any class that includes Familia.
- #raw(suffix = nil) ⇒ Object
- #realttl ⇒ Object
- #redis ⇒ Object
- #redisinfo ⇒ Object
- #rediskey(suffix = nil) ⇒ Object
- #redistype(suffix = nil) ⇒ Object
- #redisuri(suffix = nil) ⇒ Object
- #save ⇒ Object
-
#shortid ⇒ Object
Finds the shortest available unique key (lower limit of 6).
- #ttl ⇒ Object
- #ttl=(v) ⇒ Object
Instance Method Details
#allkeys ⇒ Object
def rediskeys
self.class.redis_objects.each do |redis_object_definition|
end
end
358 359 360 361 362 363 364 365 |
# File 'lib/familia/object.rb', line 358 def allkeys # TODO: Use redis_objects instead keynames = [rediskey] self.class.suffixes.each do |sfx| keynames << rediskey(sfx) end keynames end |
#destroy! ⇒ Object
388 389 390 391 392 393 394 395 |
# File 'lib/familia/object.rb', line 388 def destroy! ret = self.object.delete if Familia.debug? Familia.trace :DELETED, Familia.redis(self.class.uri), "#{rediskey}: #{ret}", caller.first end self.class.instances.rem self if ret > 0 ret end |
#exists? ⇒ Boolean
348 349 350 |
# File 'lib/familia/object.rb', line 348 def exists? Familia.redis(self.class.uri).exists rediskey end |
#expire(ttl = nil) ⇒ Object
449 450 451 452 |
# File 'lib/familia/object.rb', line 449 def expire(ttl=nil) ttl ||= self.class.ttl Familia.redis(self.class.uri).expire rediskey, ttl.to_i end |
#index ⇒ Object
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/familia/object.rb', line 396 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
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
# File 'lib/familia/object.rb', line 421 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.
307 308 309 310 311 |
# File 'lib/familia/object.rb', line 307 def initialize *args initialize_redis_objects from_array *args if respond_to? :from_array init *args if respond_to? :init end |
#initialize_redis_objects ⇒ Object
This needs to be called in the initialize method of any class that includes Familia.
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/familia/object.rb', line 315 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
462 463 464 465 |
# File 'lib/familia/object.rb', line 462 def raw(suffix=nil) suffix ||= :object Familia.redis(self.class.uri).get rediskey(suffix) end |
#realttl ⇒ Object
453 454 455 |
# File 'lib/familia/object.rb', line 453 def realttl Familia.redis(self.class.uri).ttl rediskey end |
#redis ⇒ Object
335 336 337 |
# File 'lib/familia/object.rb', line 335 def redis self.class.redis end |
#redisinfo ⇒ Object
339 340 341 342 343 344 345 346 347 |
# File 'lib/familia/object.rb', line 339 def redisinfo info = { :uri => self.class.uri, :db => self.class.db, :key => rediskey, :type => redistype, :ttl => realttl } end |
#rediskey(suffix = nil) ⇒ Object
366 367 368 369 370 371 372 373 374 |
# File 'lib/familia/object.rb', line 366 def rediskey(suffix=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
472 473 474 |
# File 'lib/familia/object.rb', line 472 def redistype(suffix=nil) Familia.redis(self.class.uri).type rediskey(suffix) end |
#redisuri(suffix = nil) ⇒ Object
466 467 468 469 470 471 |
# File 'lib/familia/object.rb', line 466 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 |
#save ⇒ Object
375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/familia/object.rb', line 375 def save #Familia.trace :SAVE, Familia.redis(self.class.uri), redisuri, caller.first 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 |
#shortid ⇒ Object
Finds the shortest available unique key (lower limit of 6)
476 477 478 479 480 481 482 483 484 485 486 487 |
# File 'lib/familia/object.rb', line 476 def shortid len = 6 loop do begin self.class.(@id.shorten(len)) break rescue Familia::NonUniqueKey len += 1 end end @id.shorten(len) end |
#ttl ⇒ Object
459 460 461 |
# File 'lib/familia/object.rb', line 459 def ttl @ttl || self.class.ttl end |
#ttl=(v) ⇒ Object
456 457 458 |
# File 'lib/familia/object.rb', line 456 def ttl=(v) @ttl = v.to_i end |