Module: Zdi::Collection::ClassMethods

Included in:
Zdi::Collection
Defined in:
lib/zentradi/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



5
6
7
# File 'lib/zentradi/collection.rb', line 5

def collection
  @collection
end

Instance Method Details

#build(attributes = {}) ⇒ Object



23
24
25
# File 'lib/zentradi/collection.rb', line 23

def build(attributes = {})
  DocumentWrapper.new(self, attributes)
end

#create(attributes = {}) ⇒ Object



27
28
29
# File 'lib/zentradi/collection.rb', line 27

def create(attributes = {})
  build(attributes).tap { |o| o.save }
end

#delegate_to_collection(methods) ⇒ Object



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

def delegate_to_collection(methods)
  Array(methods).each { |meth| extend(Module.new { module_eval(<<-METHOD, __FILE__, __LINE__ + 1) }) }
    def #{ meth }(*args, &block)
      Zdi::DocumentWrapper.mongo_collection_method_wrap(collection, #{ meth.inspect }, self, *args, &block)
    end
  METHOD
end

#drop_collectionObject



15
16
17
# File 'lib/zentradi/collection.rb', line 15

def drop_collection
  collection.drop
end

#ensure_collectionObject



11
12
13
# File 'lib/zentradi/collection.rb', line 11

def ensure_collection
  database.create_collection(collection.name)
end

#find_or_build(attributes) ⇒ Object



31
32
33
# File 'lib/zentradi/collection.rb', line 31

def find_or_build(attributes)
  find_one(attributes) || build(attributes)
end

#find_or_create(attributes) ⇒ Object



35
36
37
# File 'lib/zentradi/collection.rb', line 35

def find_or_create(attributes)
  update(attributes, attributes, :upsert => true)
end

#inherited(base) ⇒ Object



7
8
9
# File 'lib/zentradi/collection.rb', line 7

def inherited(base)
  base.collection = self.collection
end

#recreate_collectionObject



19
20
21
# File 'lib/zentradi/collection.rb', line 19

def recreate_collection
  drop_collection; ensure_collection
end