Class: Gcloud::Search::FieldValues
- Inherits:
-
Object
- Object
- Gcloud::Search::FieldValues
- Includes:
- Enumerable
- Defined in:
- lib/gcloud/search/field_values.rb
Overview
# FieldValues
The list of values for a field.
Each field has a name (String) and a list of values. Each field name consists of only ASCII characters, must be unique within the document and is case sensitive. A field name must start with a letter and can contain letters, digits, or underscore, with a maximum of 500 characters.
Each field on a document can have multiple values. FieldValues is the object that manages the multiple values. Values can be the same or different types; however, it cannot have multiple datetime (DateTime) or number (Float) values. (See FieldValue)
Class Method Summary collapse
Instance Method Summary collapse
-
#[](index) ⇒ FieldValue?
Returns the element at index, or returns a subarray starting at the start index and continuing for length elements, or returns a subarray specified by range of indices.
-
#add(value, type: nil, lang: nil) ⇒ FieldValue
Add a new value.
-
#delete(value, &block) ⇒ FieldValue?
Deletes all values that are equal to value.
-
#delete_at(index) ⇒ FieldValue
Deletes the value at the specified index, returning that FieldValue, or ‘nil` if the index is out of range.
-
#each(&block) ⇒ Object
Calls the given block once for each field value, passing the field value as a parameter.
-
#empty? ⇒ Boolean
Returns ‘true` if there are no values.
-
#initialize(name, values = []) ⇒ FieldValues
constructor
A new instance of FieldValues.
- #to_raw ⇒ Object
Constructor Details
#initialize(name, values = []) ⇒ FieldValues
Returns a new instance of FieldValues.
61 62 63 64 |
# File 'lib/gcloud/search/field_values.rb', line 61 def initialize name, values = [] @name = name @values = values end |
Class Method Details
.from_raw(name, values) ⇒ Object
221 222 223 224 |
# File 'lib/gcloud/search/field_values.rb', line 221 def self.from_raw name, values field_values = values.map { |value| FieldValue.from_raw value, name } FieldValues.new name, field_values end |
Instance Method Details
#[](index) ⇒ FieldValue?
Returns the element at index, or returns a subarray starting at the start index and continuing for length elements, or returns a subarray specified by range of indices.
Negative indices count backward from the end of the array (-1 is the last element). For start and range cases the starting index is just before an element. Additionally, an empty array is returned when the starting index for an element range is at the end of the array.
78 79 80 |
# File 'lib/gcloud/search/field_values.rb', line 78 def [] index @values[index] end |
#add(value, type: nil, lang: nil) ⇒ FieldValue
Add a new value. If the field name does not exist it will be added. If the field value is a DateTime or Numeric, or the type is set to ‘:datetime` or `:number`, then the added value will replace any existing values of the same type (since there can be only one).
131 132 133 134 135 136 137 |
# File 'lib/gcloud/search/field_values.rb', line 131 def add value, type: nil, lang: nil new_field = FieldValue.new value, type: type, lang: lang, name: @name if [:datetime, :number].include? new_field.type @values.delete_if { |v| v.type == new_field.type } end @values << new_field end |
#delete(value, &block) ⇒ FieldValue?
Deletes all values that are equal to value.
159 160 161 162 |
# File 'lib/gcloud/search/field_values.rb', line 159 def delete value, &block fv = @values.detect { |v| v == value } @values.delete fv, &block end |
#delete_at(index) ⇒ FieldValue
Deletes the value at the specified index, returning that FieldValue, or ‘nil` if the index is out of range.
186 187 188 |
# File 'lib/gcloud/search/field_values.rb', line 186 def delete_at index @values.delete_at index end |
#each(&block) ⇒ Object
Calls the given block once for each field value, passing the field value as a parameter.
An Enumerator is returned if no block is given.
209 210 211 |
# File 'lib/gcloud/search/field_values.rb', line 209 def each &block @values.each(&block) end |
#empty? ⇒ Boolean
Returns ‘true` if there are no values.
215 216 217 |
# File 'lib/gcloud/search/field_values.rb', line 215 def empty? @values.empty? end |
#to_raw ⇒ Object
228 229 230 |
# File 'lib/gcloud/search/field_values.rb', line 228 def to_raw { "values" => @values.map(&:to_raw) } end |