Class: Google::Cloud::Spanner::Data
- Inherits:
-
Object
- Object
- Google::Cloud::Spanner::Data
- Defined in:
- lib/google/cloud/spanner/data.rb
Overview
# Data
Represents a row in a result from Cloud Spanner. Provides access to data in a hash-like structure. Values can be retrieved by name (String), or in cases in which values are unnamed, by zero-based index position (Integer).
Class Method Summary collapse
-
.from_grpc(grpc_values, grpc_fields) ⇒ Object
Spanner values and fields.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#[](key) ⇒ Object?
Returns the value object for the provided name (String) or index (Integer).
-
#fields ⇒ Array<Array>
Returns the configuration object (Fields) of the names and types of the data.
- #inspect ⇒ Object
-
#keys ⇒ Array<(String,Integer)>
Returns the names of values, or in cases in which values are unnamed, the zero-based index position of values.
-
#pairs ⇒ Array<Array>
Returns the names or positions and their corresponding values as an array of arrays.
-
#to_a ⇒ Array<Object>
Returns the values as an array.
- #to_grpc_type ⇒ Object
- #to_grpc_value ⇒ Object
- #to_grpc_value_and_type ⇒ Object
-
#to_h ⇒ Hash<(String,Integer)=>Object>
Returns the names or indexes and corresponding values of the data as a hash.
- #to_s ⇒ Object
-
#types ⇒ Array<Symbol>
Returns the types of the data.
-
#values ⇒ Array<Object>
Returns the values of the data.
Class Method Details
.from_grpc(grpc_values, grpc_fields) ⇒ Object
Spanner values and fields.
223 224 225 226 227 228 |
# File 'lib/google/cloud/spanner/data.rb', line 223 def self.from_grpc grpc_values, grpc_fields new.tap do |d| d.instance_variable_set :@grpc_values, Array(grpc_values) d.instance_variable_set :@grpc_fields, Array(grpc_fields) end end |
Instance Method Details
#==(other) ⇒ Object
167 168 169 170 |
# File 'lib/google/cloud/spanner/data.rb', line 167 def == other return false unless other.is_a? Data pairs == other.pairs end |
#[](key) ⇒ Object?
Returns the value object for the provided name (String) or index (Integer). Do not pass a name to this method if the data has more than one member with the same name.
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/google/cloud/spanner/data.rb', line 111 def [] key if key.is_a? Integer return Convert.grpc_value_to_object(@grpc_values[key], @grpc_fields[key].type) end name_count = @grpc_fields.find_all { |f| f.name == String(key) }.count return nil if name_count.zero? raise DuplicateNameError if name_count > 1 index = @grpc_fields.find_index { |f| f.name == String(key) } Convert.grpc_value_to_object(@grpc_values[index], @grpc_fields[index].type) end |
#fields ⇒ Array<Array>
Returns the configuration object (Fields) of the names and types of the data.
50 51 52 |
# File 'lib/google/cloud/spanner/data.rb', line 50 def fields @fields ||= Fields.from_grpc @grpc_fields end |
#inspect ⇒ Object
185 186 187 |
# File 'lib/google/cloud/spanner/data.rb', line 185 def inspect "#<#{self.class.name} #{self}>" end |
#keys ⇒ Array<(String,Integer)>
Returns the names of values, or in cases in which values are unnamed, the zero-based index position of values.
74 75 76 |
# File 'lib/google/cloud/spanner/data.rb', line 74 def keys fields.keys end |
#pairs ⇒ Array<Array>
Returns the names or positions and their corresponding values as an array of arrays.
94 95 96 |
# File 'lib/google/cloud/spanner/data.rb', line 94 def pairs keys.zip values end |
#to_a ⇒ Array<Object>
Returns the values as an array.
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/google/cloud/spanner/data.rb', line 129 def to_a values.map do |value| if value.is_a? Data value.to_h elsif value.is_a? Array value.map { |v| v.is_a?(Data) ? v.to_h : v } else value end end end |
#to_grpc_type ⇒ Object
205 206 207 208 209 210 211 212 |
# File 'lib/google/cloud/spanner/data.rb', line 205 def to_grpc_type Google::Spanner::V1::Type.new( code: :STRUCT, struct_type: Google::Spanner::V1::StructType.new( fields: @grpc_fields ) ) end |
#to_grpc_value ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/google/cloud/spanner/data.rb', line 191 def to_grpc_value if @grpc_values.nil? return Google::Protobuf::Value.new null_value: :NULL_VALUE end Google::Protobuf::Value.new( list_value: Google::Protobuf::ListValue.new( values: @grpc_values ) ) end |
#to_grpc_value_and_type ⇒ Object
216 217 218 |
# File 'lib/google/cloud/spanner/data.rb', line 216 def to_grpc_value_and_type [to_grpc_value, to_grpc_type] end |
#to_h ⇒ Hash<(String,Integer)=>Object>
Returns the names or indexes and corresponding values of the data as a hash. Do not use this method if the data has more than one member with the same name.
152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/google/cloud/spanner/data.rb', line 152 def to_h raise DuplicateNameError if fields.duplicate_names? hashified_pairs = pairs.map do |key, value| if value.is_a? Data [key, value.to_h] elsif value.is_a? Array [key, value.map { |v| v.is_a?(Data) ? v.to_h : v }] else [key, value] end end Hash[hashified_pairs] end |
#to_s ⇒ Object
173 174 175 176 177 178 179 180 181 182 |
# File 'lib/google/cloud/spanner/data.rb', line 173 def to_s named_values = pairs.map do |key, value| if key.is_a? Integer value.inspect else "(#{key})#{value.inspect}" end end "(#{named_values.join ', '})" end |
#types ⇒ Array<Symbol>
Returns the types of the data.
See [Data types](cloud.google.com/spanner/docs/data-definition-language#data_types).
62 63 64 |
# File 'lib/google/cloud/spanner/data.rb', line 62 def types fields.types end |
#values ⇒ Array<Object>
Returns the values of the data.
83 84 85 |
# File 'lib/google/cloud/spanner/data.rb', line 83 def values Array.new(keys.count) { |i| self[i] } end |