class FirestoreODM::Document

Attributes

changes[R]
data[R]
document[RW]

Public Class Methods

new(document = nil) click to toggle source

Constructor. @param document [Google::Cloud::Firestore::DocumentReference] the document reference.

# File lib/firestore-odm/document.rb, line 10
def initialize document = nil
  unless document.nil?
    @document = document
    @data     = document.get.data
  end

  discard_changes
end

Public Instance Methods

[](key)
Alias for: get
[]=(key, value)
Alias for: set
delete() click to toggle source

Deletes the document.

# File lib/firestore-odm/document.rb, line 46
def delete
  document.delete
end
discard_changes() click to toggle source

Discards any changes made to the document.

# File lib/firestore-odm/document.rb, line 40
def discard_changes
  @changes = {}
  return
end
get(key) click to toggle source

Gets a field. @param key [String, Symbol] the field's name. @return the field's value.

# File lib/firestore-odm/document.rb, line 53
def get key
  changes[key] or data[key]
end
Also aliased as: []
id() click to toggle source

Gets the document id. @return [String] the document id.

# File lib/firestore-odm/document.rb, line 21
def id
  document.document_id
end
path() click to toggle source

Gets the document path @return [String] the document path.

# File lib/firestore-odm/document.rb, line 27
def path
  document.document_path
end
save() click to toggle source

Saves any changes made to the document.

# File lib/firestore-odm/document.rb, line 32
def save
  document.update changes
  @data = document.get.data
  discard_changes
  return
end
set(key, value) click to toggle source

Sets a field. @param key [String, Symbol] the field's name. @param value the field's value.

# File lib/firestore-odm/document.rb, line 60
def set key, value
  changes[key] = value
  return
end
Also aliased as: []=
to_json(opts = nil) click to toggle source

Converts the document to a JSON string. @param opts options to be passed to {JSON#generate}. @return [String] a JSON string.

# File lib/firestore-odm/document.rb, line 68
def to_json opts = nil
  data.merge(changes).to_json(opts)
end