Module: MongoMapper::EmbeddedDocument::ClassMethods

Defined in:
lib/mongo_mapper/embedded_document.rb

Instance Method Summary collapse

Instance Method Details

#embeddable?Boolean

Returns:



69
70
71
# File 'lib/mongo_mapper/embedded_document.rb', line 69

def embeddable?
  !self.ancestors.include?(Document)
end

#from_mongo(instance_or_hash) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/mongo_mapper/embedded_document.rb', line 84

def from_mongo(instance_or_hash)
  return nil if instance_or_hash.nil?
  
  if instance_or_hash.is_a?(self)
    instance_or_hash
  else
    initialize_doc(instance_or_hash)
  end
end

#inherited(subclass) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/mongo_mapper/embedded_document.rb', line 29

def inherited(subclass)
  unless subclass.embeddable?
    subclass.set_collection_name(collection_name)
  end

  (@subclasses ||= []) << subclass
end

#key(*args) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/mongo_mapper/embedded_document.rb', line 49

def key(*args)
  key = Key.new(*args)
  keys[key.name] = key

  create_accessors_for(key)
  create_key_in_subclasses(*args)
  create_validations_for(key)

  key
end

#keysObject



41
42
43
44
45
46
47
# File 'lib/mongo_mapper/embedded_document.rb', line 41

def keys
  @keys ||= if parent = parent_model
    parent.keys.dup
  else
    HashWithIndifferentAccess.new
  end
end

#loggerObject



25
26
27
# File 'lib/mongo_mapper/embedded_document.rb', line 25

def logger
  MongoMapper.logger
end

#object_id_key?(name) ⇒ Boolean

Returns:



64
65
66
67
# File 'lib/mongo_mapper/embedded_document.rb', line 64

def object_id_key?(name)
  key = keys[name.to_s]
  key && key.type == ObjectId
end

#parent_modelObject



73
74
75
76
77
# File 'lib/mongo_mapper/embedded_document.rb', line 73

def parent_model
  (ancestors - [self,EmbeddedDocument]).find do |parent_class|
    parent_class.ancestors.include?(EmbeddedDocument)
  end
end

#subclassesObject



37
38
39
# File 'lib/mongo_mapper/embedded_document.rb', line 37

def subclasses
  @subclasses
end

#to_mongo(instance) ⇒ Object



79
80
81
82
# File 'lib/mongo_mapper/embedded_document.rb', line 79

def to_mongo(instance)
  return nil if instance.nil?
  instance.to_mongo
end

#using_object_id?Boolean

Returns:



60
61
62
# File 'lib/mongo_mapper/embedded_document.rb', line 60

def using_object_id?
  object_id_key?(:_id)
end