Module: FirestoreODM::Collection

Included in:
Model
Defined in:
lib/firestore-odm/collection.rb

Instance Method Summary collapse

Instance Method Details

#collection(root = nil) ⇒ Google::Cloud::Firestore::CollectionReference

Returns the collection used by the model.

Parameters:

Returns:

  • (Google::Cloud::Firestore::CollectionReference)

    the collection.



8
9
10
# File 'lib/firestore-odm/collection.rb', line 8

def collection root = nil
  (root or Google::Cloud::Firestore.new).collection @path
end

#create(opts = {}) {|doc| ... } ⇒ FirestoreODM::Document

Creates a document.

Parameters:

  • opts (Hash) (defaults to: {})

    a hash with options.

Yields:

  • (doc)

Returns:



23
24
25
26
27
28
# File 'lib/firestore-odm/collection.rb', line 23

def create opts = {}, &block
  block.call o = self.new

  doc = collection(opts[:relative_to]).add o.changes
  self.new doc
end

#create_with_id(id, opts = {}) {|doc| ... } ⇒ FirestoreODM::Document

Creates a document with an specific id.

Parameters:

  • id (String)

    the document id.

  • opts (Hash) (defaults to: {})

Yields:

  • (doc)

Returns:



35
36
37
38
39
40
41
# File 'lib/firestore-odm/collection.rb', line 35

def create_with_id id, opts = {}, &block
  block.call o = self.new

  doc = collection(opts[:relative_to]).doc id
  doc.create o.changes
  self.from_id id, opts
end

#from_id(id, opts = {}) ⇒ FirestoreODM::Document

Gets a document.

Parameters:

  • id (String)
  • opts (Hash) (defaults to: {})

Returns:



47
48
49
50
# File 'lib/firestore-odm/collection.rb', line 47

def from_id id, opts = {}
  doc = collection(opts[:relative_to]).doc id
  self.new doc
end

#path(name) ⇒ Object

Sets the collection name.

Parameters:

  • name (String, Symbol)

    the collection name.



14
15
16
17
# File 'lib/firestore-odm/collection.rb', line 14

def path name
  @path = name
  return
end