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
357 358 359 360 361 362 363 364 |
# File 'lib/familia/object.rb', line 357 def allkeys # TODO: Use redis_objects instead keynames = [rediskey] self.class.suffixes.each do |sfx| keynames << rediskey(sfx) end keynames end |
#destroy! ⇒ Object
387 388 389 390 391 392 393 394 |
# File 'lib/familia/object.rb', line 387 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
347 348 349 |
# File 'lib/familia/object.rb', line 347 def exists? Familia.redis(self.class.uri).exists rediskey end |
#expire(ttl = nil) ⇒ Object
448 449 450 451 |
# File 'lib/familia/object.rb', line 448 def expire(ttl=nil) ttl ||= self.class.ttl Familia.redis(self.class.uri).expire rediskey, ttl.to_i end |
#index ⇒ Object
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/familia/object.rb', line 395 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
420 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 |
# File 'lib/familia/object.rb', line 420 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 |
# File 'lib/familia/object.rb', line 307 def initialize *args initialize_redis_objects 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.
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/familia/object.rb', line 314 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
461 462 463 464 |
# File 'lib/familia/object.rb', line 461 def raw(suffix=nil) suffix ||= :object Familia.redis(self.class.uri).get rediskey(suffix) end |
#realttl ⇒ Object
452 453 454 |
# File 'lib/familia/object.rb', line 452 def realttl Familia.redis(self.class.uri).ttl rediskey end |
#redis ⇒ Object
334 335 336 |
# File 'lib/familia/object.rb', line 334 def redis self.class.redis end |
#redisinfo ⇒ Object
338 339 340 341 342 343 344 345 346 |
# File 'lib/familia/object.rb', line 338 def redisinfo info = { :uri => self.class.uri, :db => self.class.db, :key => rediskey, :type => redistype, :ttl => realttl } end |
#rediskey(suffix = nil) ⇒ Object
365 366 367 368 369 370 371 372 373 |
# File 'lib/familia/object.rb', line 365 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
471 472 473 |
# File 'lib/familia/object.rb', line 471 def redistype(suffix=nil) Familia.redis(self.class.uri).type rediskey(suffix) end |
#redisuri(suffix = nil) ⇒ Object
465 466 467 468 469 470 |
# File 'lib/familia/object.rb', line 465 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
374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/familia/object.rb', line 374 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)
475 476 477 478 479 480 481 482 483 484 485 486 |
# File 'lib/familia/object.rb', line 475 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
458 459 460 |
# File 'lib/familia/object.rb', line 458 def ttl @ttl || self.class.ttl end |
#ttl=(v) ⇒ Object
455 456 457 |
# File 'lib/familia/object.rb', line 455 def ttl=(v) @ttl = v.to_i end |