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

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

Instance Method Summary collapse

Instance Method Details

#collectionObject



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

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

#collection_nameObject



104
105
106
107
108
109
110
# File 'lib/mongomodel/document/persistence.rb', line 104

def collection_name
  if superclass.abstract_class?
    @_collection_name || name.tableize.gsub(/\//, '.')
  else
    superclass.collection_name
  end
end

#collection_name=(name) ⇒ Object



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

def collection_name=(name)
  @_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



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

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



136
137
138
# File 'lib/mongomodel/document/persistence.rb', line 136

def save_safely=(val)
  @_save_safely = val
end

#save_safely?Boolean

Returns:



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

def save_safely?
  @_save_safely
end

#type_selectorObject



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

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

#use_type_selector?Boolean

Returns:



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

def use_type_selector?
  !superclass.abstract_class?
end