Class: Mongoid::CachedDocument

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/cached_document.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ CachedDocument

Returns a new instance of CachedDocument.



32
33
34
# File 'lib/mongoid/cached_document.rb', line 32

def initialize(attributes)
  @cached_attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mongoid/cached_document.rb', line 59

def method_missing(name, *args, &block)
  if @document
    _document.send name, *args, &block
  elsif @cached_attributes && @cached_attributes.has_key?(name.to_s)
    @cached_attributes[name.to_s]
  elsif _document
    if defined? Rails
      Rails.logger.debug("#{@cached_attributes['_type']}[:#{name}] is not cached (called from #{caller(1).first})")
    end
    
    _document.send name, *args, &block
  else
    super
  end
end

Class Method Details

.get(value) ⇒ Object



47
48
49
# File 'lib/mongoid/cached_document.rb', line 47

def get(value)
  self.new(value)
end

.set(value) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/mongoid/cached_document.rb', line 37

def set(value)
  if value.nil?
    nil
  elsif value.respond_to? :cachable_attributes
    value.cachable_attributes.merge({ '_type' => value.class.to_s, '_id' => value.id })
  else
    { '_type' => value.class.to_s, '_id' => value.id }
  end
end

Instance Method Details

#==(value) ⇒ Object Also known as: eql?, equal?



52
53
54
# File 'lib/mongoid/cached_document.rb', line 52

def ==(value)
  _document == value
end