Module: MongoMapper::EmbeddedDocument::ClassMethods

Defined in:
lib/mongo_mapper/embedded_document.rb

Instance Method Summary collapse

Instance Method Details

#embeddable?Boolean

Returns:



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

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

#from_mongo(instance_or_hash) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/mongo_mapper/embedded_document.rb', line 80

def from_mongo(instance_or_hash)
  return nil if instance_or_hash.nil?
  
  if instance_or_hash.is_a?(self)
    instance_or_hash
  else
    new(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
59
60
61
62
63
# File 'lib/mongo_mapper/embedded_document.rb', line 49

def key(*args)
  key = Key.new(*args)

  if keys[key.name].blank?
    keys[key.name] = key

    create_accessors_for(key)
    create_key_in_subclasses(*args)
    create_validations_for(key)
    
    key
  end
  
  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

#parent_modelObject



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

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



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

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