Module: MongoMapper::EmbeddedDocument::ClassMethods

Defined in:
lib/mongo_mapper/embedded_document.rb

Instance Method Summary collapse

Instance Method Details

#embeddable?Boolean

Returns:



67
68
69
# File 'lib/mongo_mapper/embedded_document.rb', line 67

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

#from_mongo(value) ⇒ Object



82
83
84
85
# File 'lib/mongo_mapper/embedded_document.rb', line 82

def from_mongo(value)
  return nil if value.nil?
  value.is_a?(self) ? value : initialize_doc(value)
end

#inherited(subclass) ⇒ Object



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

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

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

#key(*args) ⇒ Object



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

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



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

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

#loggerObject



23
24
25
# File 'lib/mongo_mapper/embedded_document.rb', line 23

def logger
  MongoMapper.logger
end

#object_id_key?(name) ⇒ Boolean

Returns:



62
63
64
65
# File 'lib/mongo_mapper/embedded_document.rb', line 62

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

#parent_modelObject



71
72
73
74
75
# File 'lib/mongo_mapper/embedded_document.rb', line 71

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

#subclassesObject



35
36
37
# File 'lib/mongo_mapper/embedded_document.rb', line 35

def subclasses
  @subclasses
end

#to_mongo(instance) ⇒ Object



77
78
79
80
# File 'lib/mongo_mapper/embedded_document.rb', line 77

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

#using_object_id?Boolean

Returns:



58
59
60
# File 'lib/mongo_mapper/embedded_document.rb', line 58

def using_object_id?
  object_id_key?(:_id)
end