Module: Mongo::Model::Misc

Defined in:
lib/mongo/model/misc.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#_cacheObject



11
12
13
# File 'lib/mongo/model/misc.rb', line 11

def _cache
  @_cache ||= {}
end

#dom_idObject



15
16
17
18
# File 'lib/mongo/model/misc.rb', line 15

def dom_id
  # new_record? ? "new_#{self.class.name.underscore}" : to_param
  to_param
end

#originalObject



33
34
35
36
37
38
39
# File 'lib/mongo/model/misc.rb', line 33

def original
  unless _cache[:original_cached]
    _cache[:original_cached] = true
    _cache[:original] = id && self.class.by_id(id)
  end
  _cache[:original]
end

#reloadObject



24
25
26
27
28
29
30
31
# File 'lib/mongo/model/misc.rb', line 24

def reload
  obj = self.class.by_id!(id || raise("can't reload new document (#{self})!"))
  instance_variables.each{|n| remove_instance_variable n}
  obj.instance_variables.each do |n|
    instance_variable_set n, obj.instance_variable_get(n)
  end
  nil
end

#to_paramObject



20
21
22
# File 'lib/mongo/model/misc.rb', line 20

def to_param
  id.try :to_s
end

#update_created_atObject



2
# File 'lib/mongo/model/misc.rb', line 2

def update_created_at; self.created_at = Time.now.utc end

#update_timestampsObject



5
6
7
8
9
# File 'lib/mongo/model/misc.rb', line 5

def update_timestamps
  now = Time.now.utc
  self.created_at ||= now
  self.updated_at = now
end

#update_updated_atObject



3
# File 'lib/mongo/model/misc.rb', line 3

def update_updated_at; self.updated_at = Time.now.utc end