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).
Instance Method Summary collapse
-
#[](key) ⇒ Object?
Returns the value object for the provided name (String) or index (Integer).
-
#fields ⇒ Array<Array>
Returns the names and values of the data as an array of field objects.
-
#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_h ⇒ Hash<(String,Integer)=>Object>
Returns the names or indexes and corresponding values of the data as a hash.
-
#types ⇒ Array<Symbol>
Returns the types of the data.
-
#values ⇒ Array<Object>
Returns the values of the data.
Instance Method Details
#[](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.
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/google/cloud/spanner/data.rb', line 110 def [] key if key.is_a? Integer return Convert.value_to_raw(@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 == 0 fail DuplicateNameError if name_count > 1 index = @grpc_fields.find_index { |f| f.name == String(key) } Convert.value_to_raw(@grpc_values[index], @grpc_fields[index].type) end |
#fields ⇒ Array<Array>
Returns the names and values of the data as an array of field objects.
49 50 51 |
# File 'lib/google/cloud/spanner/data.rb', line 49 def fields @fields ||= Fields.from_grpc @grpc_fields 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.
73 74 75 |
# File 'lib/google/cloud/spanner/data.rb', line 73 def keys fields.keys end |
#pairs ⇒ Array<Array>
Returns the names or positions and their corresponding values as an array of arrays.
93 94 95 |
# File 'lib/google/cloud/spanner/data.rb', line 93 def pairs keys.zip values end |
#to_a ⇒ Array<Object>
Returns the values as an array.
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/google/cloud/spanner/data.rb', line 127 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_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.
150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/google/cloud/spanner/data.rb', line 150 def to_h fail 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 |
#types ⇒ Array<Symbol>
Returns the types of the data.
See Data types.
61 62 63 |
# File 'lib/google/cloud/spanner/data.rb', line 61 def types fields.types end |
#values ⇒ Array<Object>
Returns the values of the data.
82 83 84 |
# File 'lib/google/cloud/spanner/data.rb', line 82 def values keys.count.times.map { |i| self[i] } end |