Class: Google::Cloud::Firestore::FieldValue

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/firestore/field_value.rb

Overview

FieldValue

Represents a change to be made to fields in document data in the Firestore API.

Examples:

require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new

user_snap = firestore.doc("users/frank").get

# TODO

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.array_delete(*values) ⇒ FieldValue

Creates a sentinel value to indicate the removal of the given values with an array.

Examples:

require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new

# Get a document reference
nyc_ref = firestore.doc "cities/NYC"

array_delete = Google::Cloud::Firestore::FieldValue.array_delete(
  7, 8, 9
)

nyc_ref.update({ name: "New York City",
                 lucky_numbers: array_delete })

Parameters:

  • values (Object)

    The values to remove from the array. Required.

Returns:

  • (FieldValue)

    The array delete field value object.

Raises:

  • (ArgumentError)


196
197
198
199
200
201
202
203
204
# File 'lib/google/cloud/firestore/field_value.rb', line 196

def self.array_delete *values
  # We can flatten the values because arrays don't support sub-arrays
  values.flatten!
  raise ArgumentError, "values must be provided" if values.nil?
  # verify the values are the correct types
  Convert.raw_to_value values

  new :array_delete, values
end

.array_union(*values) ⇒ FieldValue

Creates a sentinel value to indicate the union of the given values with an array.

Examples:

require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new

# Get a document reference
nyc_ref = firestore.doc "cities/NYC"

array_union = Google::Cloud::Firestore::FieldValue.array_union(
  1, 2, 3
)

nyc_ref.update({ name: "New York City",
                 lucky_numbers: array_union })

Parameters:

  • values (Object)

    The values to add to the array. Required.

Returns:

  • (FieldValue)

    The array union field value object.

Raises:

  • (ArgumentError)


163
164
165
166
167
168
169
170
171
# File 'lib/google/cloud/firestore/field_value.rb', line 163

def self.array_union *values
  # We can flatten the values because arrays don't support sub-arrays
  values.flatten!
  raise ArgumentError, "values must be provided" if values.nil?
  # verify the values are the correct types
  Convert.raw_to_value values

  new :array_union, values
end

.deleteFieldValue

Creates a field value object representing the deletion of a field in document data.

Examples:

require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new

# Get a document reference
nyc_ref = firestore.doc "cities/NYC"

field_delete = Google::Cloud::Firestore::FieldValue.delete

nyc_ref.update({ name: "New York City",
                 trash: field_delete })

Returns:



113
114
115
# File 'lib/google/cloud/firestore/field_value.rb', line 113

def self.delete
  new :delete
end

.server_timeFieldValue

Creates a field value object representing set a field's value to the server timestamp when accessing the document data.

Examples:

require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new

# Get a document reference
nyc_ref = firestore.doc "cities/NYC"

field_server_time = Google::Cloud::Firestore::FieldValue.server_time

nyc_ref.update({ name: "New York City",
                 updated_at: field_server_time })

Returns:

  • (FieldValue)

    The server time field value object.



136
137
138
# File 'lib/google/cloud/firestore/field_value.rb', line 136

def self.server_time
  new :server_time
end

Instance Method Details

#typeSymbol

The type of change to make to an individual field in document data.

Examples:

require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new

# Get a document reference
nyc_ref = firestore.doc "cities/NYC"

field_delete = Google::Cloud::Firestore::FieldValue.delete
field_delete.type #=> :delete

nyc_ref.update({ name: "New York City",
                 trash: field_delete })

Returns:

  • (Symbol)

    The type.



62
63
64
# File 'lib/google/cloud/firestore/field_value.rb', line 62

def type
  @type
end