class Redis::LazyHash

Public Class Methods

new(args = nil) click to toggle source
# File lib/redis/lazy_hash.rb, line 9
def initialize(args = nil)
  @hash = NativeHash.new(args)
  @loaded = false
end

Private Class Methods

find(args) click to toggle source
# File lib/redis/lazy_hash.rb, line 54
def find(args)
  case args
  when Hash
    self.new(args)
  when String,Symbol
    self.new(nil=>args)
  end
end

Public Instance Methods

inspect() click to toggle source
# File lib/redis/lazy_hash.rb, line 26
def inspect
  lazy_load!
  @hash.inspect
end
loaded?() click to toggle source
# File lib/redis/lazy_hash.rb, line 35
def loaded?
  @loaded
end
method_missing(meth, *args, &block) click to toggle source
Calls superclass method
# File lib/redis/lazy_hash.rb, line 14
def method_missing(meth, *args, &block)
  if @hash.respond_to?(meth)
    self.class.send(:define_method, meth) do |*args, &block|
      lazy_load!
      @hash.send(meth, *args, &block)
    end
    send(meth, *args, &block)
  else
    super
  end
end
save() click to toggle source
# File lib/redis/lazy_hash.rb, line 31
def save
  @hash.save if loaded?
end
to_hash() click to toggle source
# File lib/redis/lazy_hash.rb, line 39
def to_hash
  self
end

Private Instance Methods

lazy_load!() click to toggle source
# File lib/redis/lazy_hash.rb, line 45
def lazy_load!
  unless loaded?
    reload!
    @hash.retrack!
    @loaded = true
  end
end