Class: Google::Cloud::Spanner::Fields
- Inherits:
-
Object
- Object
- Google::Cloud::Spanner::Fields
- Defined in:
- lib/google/cloud/spanner/fields.rb
Overview
Instance Method Summary collapse
-
#[](key) ⇒ Symbol?
Returns the type code for the provided name (String) or index (Integer).
-
#duplicate_names? ⇒ Boolean
Detects duplicate names in the keys for the fields.
-
#keys ⇒ Array<(String,Integer)>
Returns the names of the data 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 types as an array of arrays.
-
#to_a ⇒ Array<Symbol>
Returns the type codes as an array.
-
#to_h ⇒ Hash<(String,Integer)=>Symbol>
Returns the names or indexes and corresponding type codes as a hash.
-
#types ⇒ Array<Symbol>
Returns the types of the data.
Instance Method Details
#[](key) ⇒ Symbol?
Returns the type code 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. (See #duplicate_names?)
135 136 137 138 139 140 141 142 |
# File 'lib/google/cloud/spanner/fields.rb', line 135 def [] key return types[key] if key.is_a? Integer name_count = @fields.find_all { |f| f.name == String(key) }.count return nil if name_count == 0 fail DuplicateNameError if name_count > 1 index = @fields.find_index { |f| f.name == String(key) } types[index] end |
#duplicate_names? ⇒ Boolean
Detects duplicate names in the keys for the fields.
107 108 109 |
# File 'lib/google/cloud/spanner/fields.rb', line 107 def duplicate_names? keys.count != keys.uniq.count end |
#keys ⇒ Array<(String,Integer)>
Returns the names of the data values, or in cases in which values are unnamed, the zero-based index position of values.
92 93 94 95 96 97 98 99 100 |
# File 'lib/google/cloud/spanner/fields.rb', line 92 def keys @fields.each_with_index.map do |field, index| if field.name.empty? index else field.name.to_sym end end end |
#pairs ⇒ Array<Array>
Returns the names or positions and their corresponding types as an array of arrays.
118 119 120 |
# File 'lib/google/cloud/spanner/fields.rb', line 118 def pairs keys.zip types end |
#to_a ⇒ Array<Symbol>
Returns the type codes as an array. Do not use this method if the data has more than one member with the same name. (See #duplicate_names?)
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/google/cloud/spanner/fields.rb', line 153 def to_a keys.count.times.map { |i| self[i] }.map do |field| if field.is_a? Fields field.to_h elsif field.is_a? Array field.map { |f| f.is_a?(Fields) ? f.to_h : f } else field end end end |
#to_h ⇒ Hash<(String,Integer)=>Symbol>
Returns the names or indexes and corresponding type codes as a hash.
171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/google/cloud/spanner/fields.rb', line 171 def to_h fail DuplicateNameError if duplicate_names? hashified_pairs = pairs.map do |key, value| if value.is_a? Fields [key, value.to_h] elsif value.is_a? Array [key, value.map { |v| v.is_a?(Fields) ? 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.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/google/cloud/spanner/fields.rb', line 68 def types @fields.map(&:type).map do |type| if type.code == :ARRAY if type.array_element_type.code == :STRUCT [Fields.from_grpc(type.array_element_type.struct_type.fields)] else [type.array_element_type.code] end elsif type.code == :STRUCT Fields.from_grpc type.struct_type.fields else type.code end end end |