Module: MongoMapper::EmbeddedDocument::ClassMethods
- Defined in:
- lib/mongomapper/embedded_document.rb
Instance Method Summary collapse
- #add_to_subclasses(*args) ⇒ Object
- #embeddable? ⇒ Boolean
- #ensure_index(name_or_array, options = {}) ⇒ Object
- #from_mongo(instance_or_hash) ⇒ Object
- #inherited(subclass) ⇒ Object
- #key(*args) ⇒ Object
- #keys ⇒ Object
- #parent_model ⇒ Object
- #subclasses ⇒ Object
- #to_mongo(instance) ⇒ Object
Instance Method Details
#add_to_subclasses(*args) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/mongomapper/embedded_document.rb', line 60 def add_to_subclasses(*args) return if subclasses.blank? subclasses.each do |subclass| subclass.key(*args) end end |
#embeddable? ⇒ Boolean
78 79 80 |
# File 'lib/mongomapper/embedded_document.rb', line 78 def !self.ancestors.include?(Document) end |
#ensure_index(name_or_array, options = {}) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/mongomapper/embedded_document.rb', line 68 def ensure_index(name_or_array, ={}) keys_to_index = if name_or_array.is_a?(Array) name_or_array.map { |pair| [pair[0], pair[1]] } else name_or_array end collection.create_index(keys_to_index, .delete(:unique)) end |
#from_mongo(instance_or_hash) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/mongomapper/embedded_document.rb', line 93 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
25 26 27 28 29 30 31 |
# File 'lib/mongomapper/embedded_document.rb', line 25 def inherited(subclass) unless subclass. subclass.set_collection_name(collection_name) end (@subclasses ||= []) << subclass end |
#key(*args) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mongomapper/embedded_document.rb', line 45 def key(*args) key = Key.new(*args) if keys[key.name].blank? keys[key.name] = key create_accessors_for(key) add_to_subclasses(*args) apply_validations_for(key) create_indexes_for(key) key end end |
#keys ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/mongomapper/embedded_document.rb', line 37 def keys @keys ||= if parent = parent_model parent.keys.dup else HashWithIndifferentAccess.new end end |
#parent_model ⇒ Object
82 83 84 85 86 |
# File 'lib/mongomapper/embedded_document.rb', line 82 def parent_model (ancestors - [self,EmbeddedDocument]).find do |parent_class| parent_class.ancestors.include?(EmbeddedDocument) end end |
#subclasses ⇒ Object
33 34 35 |
# File 'lib/mongomapper/embedded_document.rb', line 33 def subclasses @subclasses end |
#to_mongo(instance) ⇒ Object
88 89 90 91 |
# File 'lib/mongomapper/embedded_document.rb', line 88 def to_mongo(instance) return nil if instance.nil? instance.to_mongo end |