Class: Gcloud::Search::FieldValue
- Inherits:
-
Object
- Object
- Gcloud::Search::FieldValue
- Defined in:
- lib/gcloud/search/field_value.rb
Overview
# FieldValue
FieldValue is used to represent a value that belongs to a field. (See Fields and FieldValues)
A field value must have a type. A value that is a Numeric will default to ‘:number`, while a DateTime will default to `:datetime`. If a type is not provided it will be determined by looking at the value.
String values (text, html, atom) can also specify a lang value, which is an [ISO 639-1 code](en.wikipedia.org/wiki/List_of_ISO_639-1_codes).
Instance Attribute Summary collapse
-
#lang ⇒ Object
readonly
Returns the value of attribute lang.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(value, type: nil, lang: nil, name: nil) ⇒ FieldValue
constructor
A new instance of FieldValue.
-
#string_type? ⇒ Boolean
Determines if the value a string type.
- #to_raw ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(value, type: nil, lang: nil, name: nil) ⇒ FieldValue
Returns a new instance of FieldValue.
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/gcloud/search/field_value.rb', line 81 def initialize value, type: nil, lang: nil, name: nil super value if type @type = type.to_s.downcase.to_sym else @type = infer_type end @lang = lang if string_type? @name = name end |
Instance Attribute Details
#lang ⇒ Object (readonly)
Returns the value of attribute lang.
49 50 51 |
# File 'lib/gcloud/search/field_value.rb', line 49 def lang @lang end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
49 50 51 |
# File 'lib/gcloud/search/field_value.rb', line 49 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
49 50 51 |
# File 'lib/gcloud/search/field_value.rb', line 49 def type @type end |
Class Method Details
.from_raw(field_value, name = nil) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/gcloud/search/field_value.rb', line 109 def self.from_raw field_value, name = nil value = field_value["stringValue"] type = field_value["stringFormat"] if field_value["timestampValue"] value = DateTime.parse(field_value["timestampValue"]) type = :datetime elsif field_value["geoValue"] value = field_value["geoValue"] type = :geo elsif field_value["numberValue"] value = Float(field_value["numberValue"]) type = :number end fail "No value found in #{raw_field.inspect}" if value.nil? new value, type: type, lang: field_value["lang"], name: name end |
Instance Method Details
#string_type? ⇒ Boolean
Determines if the value a string type. The value is text or html or atom (or default).
97 98 99 |
# File 'lib/gcloud/search/field_value.rb', line 97 def string_type? [:atom, :default, :html, :text].include? type end |
#to_raw ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/gcloud/search/field_value.rb', line 128 def to_raw case type when :atom, :default, :html, :text { "stringFormat" => type.to_s.upcase, "lang" => lang, "stringValue" => to_s }.delete_if { |_, v| v.nil? } when :geo { "geoValue" => to_s } when :number { "numberValue" => to_f } when :datetime { "timestampValue" => rfc3339 } end end |
#value ⇒ Object
103 104 105 |
# File 'lib/gcloud/search/field_value.rb', line 103 def value __getobj__ end |