class Google::Cloud::Firestore::DocumentChange

# DocumentChange

A DocumentChange object represents a change to the document matching a query. It contains the document affected and the type of change that occurred (added, modifed, or removed).

See {Query#listen} and {QuerySnapshot#changes}.

@example

require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new

# Create a query
query = firestore.col(:cities).order(:population, :desc)

listener = query.listen do |snapshot|
  puts "The query snapshot has #{snapshot.docs.count} documents "
  puts "and has #{snapshot.changes.count} changes."
end

# When ready, stop the listen operation and close the stream.
listener.stop

Public Class Methods

from_doc(doc, old_index, new_index) click to toggle source

@private New DocumentChange from a Google::Cloud::Firestore::DocumentSnapshot object.

# File lib/google/cloud/firestore/document_change.rb, line 114
def self.from_doc doc, old_index, new_index
  new.tap do |s|
    s.instance_variable_set :@doc, doc
    s.instance_variable_set :@old_index, old_index
    s.instance_variable_set :@new_index, new_index
  end
end

Public Instance Methods

added?() click to toggle source

Determines whether the document was added.

@return [Boolean] Whether the document was added.

# File lib/google/cloud/firestore/document_change.rb, line 71
def added?
  type == :added
end
doc() click to toggle source

The document snapshot object for the data.

@return [DocumentSnapshot] document snapshot.

# File lib/google/cloud/firestore/document_change.rb, line 50
def doc
  @doc
end
Also aliased as: document
document()
Alias for: doc
modified?() click to toggle source

Determines whether the document was modified.

@return [Boolean] Whether the document was modified.

# File lib/google/cloud/firestore/document_change.rb, line 80
def modified?
  type == :modified
end
new_index() click to toggle source

The index in the documents array after the change.

@return [Integer, nil] The new index

# File lib/google/cloud/firestore/document_change.rb, line 107
def new_index
  @new_index
end
old_index() click to toggle source

The index in the documents array prior to the change.

@return [Integer, nil] The old index

# File lib/google/cloud/firestore/document_change.rb, line 98
def old_index
  @old_index
end
removed?() click to toggle source

Determines whether the document was removed.

@return [Boolean] Whether the document was removed.

# File lib/google/cloud/firestore/document_change.rb, line 89
def removed?
  type == :removed
end
type() click to toggle source

The type of change (':added', ':modified', or ':removed').

@return [Symbol] The type of change.

# File lib/google/cloud/firestore/document_change.rb, line 60
def type
  return :removed if @new_index.nil?
  return :added if @old_index.nil?
  :modified
end