Class: Amalgalite::Result::Row
- Inherits:
-
Object
- Object
- Amalgalite::Result::Row
- Defined in:
- lib/amalgalite/result/row.rb
Overview
The class that represents a single row from a query.
Instance Attribute Summary collapse
-
#field_map ⇒ Object
readonly
A Hash that maps the field names to indexes in values This may be linked to shared field from the result.
-
#result ⇒ Object
readonly
The result object this row is retrivved from, nil if all values are materialized.
-
#values ⇒ Object
(also: #to_a)
readonly
An array containing the values from the row.
Instance Method Summary collapse
- #fetch(field_name_or_index) ⇒ Object (also: #[])
- #fields ⇒ Object
- #first ⇒ Object
-
#initialize(result: nil, field_map:, values:) ⇒ Row
constructor
A new instance of Row.
- #length ⇒ Object
- #store(field_name_or_index, value) ⇒ Object (also: #[]=)
- #store_by_index(index, value) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(result: nil, field_map:, values:) ⇒ Row
Returns a new instance of Row.
24 25 26 27 28 29 |
# File 'lib/amalgalite/result/row.rb', line 24 def initialize(result: nil, field_map:, values:) @result = result @field_map = field_map @values = values self.freeze end |
Instance Attribute Details
#field_map ⇒ Object (readonly)
A Hash that maps the field names to indexes in values This may be linked to shared field from the result
18 19 20 |
# File 'lib/amalgalite/result/row.rb', line 18 def field_map @field_map end |
#result ⇒ Object (readonly)
The result object this row is retrivved from, nil if all values are materialized
14 15 16 |
# File 'lib/amalgalite/result/row.rb', line 14 def result @result end |
#values ⇒ Object (readonly) Also known as: to_a
An array containing the values from the row
21 22 23 |
# File 'lib/amalgalite/result/row.rb', line 21 def values @values end |
Instance Method Details
#fetch(field_name_or_index) ⇒ Object Also known as: []
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/amalgalite/result/row.rb', line 45 def fetch(field_name_or_index) case field_name_or_index when Integer @values[field_name_or_index] when Symbol, String @values[field_map[field_name_or_index]] else raise Amalgalite::Error, "Unknown type (#{field_name_or_index.class}) of key for a Row value: #{field_name_or_index}" end end |
#fields ⇒ Object
31 32 33 |
# File 'lib/amalgalite/result/row.rb', line 31 def fields @field_map.keys end |
#first ⇒ Object
57 58 59 |
# File 'lib/amalgalite/result/row.rb', line 57 def first fetch(0) end |
#length ⇒ Object
61 62 63 |
# File 'lib/amalgalite/result/row.rb', line 61 def length @values.size end |
#store(field_name_or_index, value) ⇒ Object Also known as: []=
39 40 41 42 |
# File 'lib/amalgalite/result/row.rb', line 39 def store(field_name_or_index, value) index = field_name_or_index_to_index(field_name_or_index) @values[index] = value end |
#store_by_index(index, value) ⇒ Object
35 36 37 |
# File 'lib/amalgalite/result/row.rb', line 35 def store_by_index(index, value) @values[index] = value end |
#to_h ⇒ Object
65 66 67 |
# File 'lib/amalgalite/result/row.rb', line 65 def to_h Hash[@field_map.keys.zip(values)] end |