Class: Redis::LazyHash

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/redis/lazy_hash.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = nil) ⇒ LazyHash

Returns a new instance of LazyHash.



9
10
11
12
# File 'lib/redis/lazy_hash.rb', line 9

def initialize(args = nil)
  @hash = NativeHash.new(args)
  @loaded = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# 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

Class Method Details

.find(args) ⇒ Object



54
55
56
57
58
59
60
61
# 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

Instance Method Details

#inspectObject



26
27
28
29
# File 'lib/redis/lazy_hash.rb', line 26

def inspect
  lazy_load!
  @hash.inspect
end

#loaded?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/redis/lazy_hash.rb', line 35

def loaded?
  @loaded
end

#saveObject



31
32
33
# File 'lib/redis/lazy_hash.rb', line 31

def save
  @hash.save if loaded?
end

#to_hashObject



39
40
41
# File 'lib/redis/lazy_hash.rb', line 39

def to_hash
  self
end