Module: MongoModel::DocumentExtensions::Persistence::ClassMethods

Defined in:
lib/mongomodel/document/persistence.rb

Instance Method Summary collapse

Instance Method Details

#collectionObject



121
122
123
# File 'lib/mongomodel/document/persistence.rb', line 121

def collection
  @_collection ||= InstrumentedCollection.new(database.collection(collection_name))
end

#collection_nameObject



104
105
106
# File 'lib/mongomodel/document/persistence.rb', line 104

def collection_name
  @_collection_name || inferred_collection_name
end

#collection_name=(name) ⇒ Object



108
109
110
111
# File 'lib/mongomodel/document/persistence.rb', line 108

def collection_name=(name)
  @_collection = nil
  @_collection_name = name
end

#create(attributes = {}, &block) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/mongomodel/document/persistence.rb', line 88

def create(attributes={}, &block)
  if attributes.is_a?(Array)
    attributes.map { |attrs| create(attrs, &block) }
  else
    instance = new(attributes, &block)
    instance.save
    instance
  end
end

#databaseObject



125
126
127
# File 'lib/mongomodel/document/persistence.rb', line 125

def database
  MongoModel.database
end

#from_mongo(hash) ⇒ Object



98
99
100
101
102
# File 'lib/mongomodel/document/persistence.rb', line 98

def from_mongo(hash)
  instance = super
  instance.send(:instantiate) if instance
  instance
end

#save_safely=(val) ⇒ Object



133
134
135
# File 'lib/mongomodel/document/persistence.rb', line 133

def save_safely=(val)
  @_save_safely = val
end

#save_safely?Boolean

Returns:



129
130
131
# File 'lib/mongomodel/document/persistence.rb', line 129

def save_safely?
  defined?(@_save_safely) ? @_save_safely : true
end

#type_selectorObject



117
118
119
# File 'lib/mongomodel/document/persistence.rb', line 117

def type_selector
  [self.to_s] + descendants.map { |m| m.to_s }
end

#use_type_selector?Boolean

Returns:



113
114
115
# File 'lib/mongomodel/document/persistence.rb', line 113

def use_type_selector?
  !superclass.abstract_class?
end