Class: Gcloud::Search::Fields
- Inherits:
-
Object
- Object
- Gcloud::Search::Fields
- Includes:
- Enumerable
- Defined in:
- lib/gcloud/search/fields.rb
Overview
# Fields
Fields is the object that provides access to a document’s fields.
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.
A field can have multiple values with same or different types; however, it cannot have multiple datetime (DateTime) or number (Float) values. (See FieldValues and FieldValue)
Class Method Summary collapse
Instance Method Summary collapse
-
#[](name) ⇒ FieldValues
Retrieve the field values associated to a field name.
-
#add(name, value, type: nil, lang: nil) ⇒ Object
Add a new value.
-
#delete(name, &block) ⇒ Object
Deletes a field and all values.
-
#each(&block) ⇒ Object
Calls block once for each field, passing the field name and values pair as parameters.
-
#initialize ⇒ Fields
constructor
A new instance of Fields.
-
#names ⇒ Object
Returns a new array populated with all the field names.
-
#to_raw ⇒ Object
values.
Constructor Details
#initialize ⇒ Fields
Returns a new instance of Fields.
59 60 61 |
# File 'lib/gcloud/search/fields.rb', line 59 def initialize @hash = {} end |
Class Method Details
.from_raw(raw) ⇒ Object
208 209 210 211 212 213 214 215 216 |
# File 'lib/gcloud/search/fields.rb', line 208 def self.from_raw raw hsh = {} raw.each do |k, v| hsh[k] = FieldValues.from_raw k, v["values"] end unless raw.nil? fields = new fields.instance_variable_set "@hash", hsh fields end |
Instance Method Details
#[](name) ⇒ FieldValues
Retrieve the field values associated to a field name.
84 85 86 |
# File 'lib/gcloud/search/fields.rb', line 84 def [] name @hash[name] ||= FieldValues.new name end |
#add(name, value, type: nil, lang: nil) ⇒ Object
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).
136 137 138 139 |
# File 'lib/gcloud/search/fields.rb', line 136 def add name, value, type: nil, lang: nil @hash[name] ||= FieldValues.new name @hash[name].add value, type: type, lang: lang end |
#delete(name, &block) ⇒ Object
Deletes a field and all values.
156 157 158 |
# File 'lib/gcloud/search/fields.rb', line 156 def delete name, &block @hash.delete name, &block end |
#each(&block) ⇒ Object
Calls block once for each field, passing the field name and values pair as parameters. If no block is given an enumerator is returned instead.
180 181 182 183 |
# File 'lib/gcloud/search/fields.rb', line 180 def each &block # Only yield fields that have values. fields_with_values.each(&block) end |
#names ⇒ Object
Returns a new array populated with all the field names.
201 202 203 204 |
# File 'lib/gcloud/search/fields.rb', line 201 def names # Only return fields that have values. fields_with_values.keys end |
#to_raw ⇒ Object
values.
221 222 223 224 225 226 227 |
# File 'lib/gcloud/search/fields.rb', line 221 def to_raw hsh = {} @hash.each do |k, v| hsh[k] = v.to_raw unless v.empty? end hsh end |