Class: Boltless::ResultRow
- Inherits:
-
Struct
- Object
- Struct
- Boltless::ResultRow
- Includes:
- Enumerable
- Defined in:
- lib/boltless/result_row.rb
Overview
A lightweight result row, used for convenient result data access.
rubocop:disable Lint/StructNewOverride because we have an
own implementation for +#values+
Instance Attribute Summary collapse
-
#graph ⇒ Object
Returns the value of attribute graph.
-
#meta ⇒ Object
Returns the value of attribute meta.
-
#result ⇒ Object
Returns the value of attribute result.
-
#values ⇒ Object
Returns the value of attribute values.
Instance Method Summary collapse
-
#[](key) ⇒ Mixed
Return the value of the requested key.
-
#as_json ⇒ Hash{String => Mixed}
Returns a JSON hash representation the result row.
-
#each {|a, a| ... } ⇒ Array<Boltless::ResultRow>
(also: #each_pair)
Calls the user given block once for each key/columns of the row, passing the key-value pair as parameters.
-
#pretty_print(pp) ⇒ Object
Pretty print the result row structure in a meaningful way.
-
#to_h ⇒ Hash{Symbol => Mixed}
Return the assembled row as Ruby hash.
-
#value ⇒ Mixed
A convenience shortcut for the first value of the row.
Instance Attribute Details
#graph ⇒ Object
Returns the value of attribute graph
8 9 10 |
# File 'lib/boltless/result_row.rb', line 8 def graph @graph end |
#meta ⇒ Object
Returns the value of attribute meta
8 9 10 |
# File 'lib/boltless/result_row.rb', line 8 def @meta end |
#result ⇒ Object
Returns the value of attribute result
8 9 10 |
# File 'lib/boltless/result_row.rb', line 8 def result @result end |
#values ⇒ Object
Returns the value of attribute values
8 9 10 |
# File 'lib/boltless/result_row.rb', line 8 def values @values end |
Instance Method Details
#[](key) ⇒ Mixed
Return the value of the requested key. When the given key is unknown, we just return nil
. The given key is normalized to its Symbol form, in order to allow performant indifferent hash access.
18 19 20 21 22 23 24 25 |
# File 'lib/boltless/result_row.rb', line 18 def [](key) # When the requested key was not found, we return +nil+, no need to # perfom the actual lookup return unless (idx = columns.index(key.to_sym)) # Otherwise return the value from the slot values[idx] end |
#as_json ⇒ Hash{String => Mixed}
Returns a JSON hash representation the result row. This works like #to_h
but the resulting hash uses string keys instead.
51 52 53 |
# File 'lib/boltless/result_row.rb', line 51 def as_json(*) columns.map(&:to_s).zip(values).to_h end |
#each {|a, a| ... } ⇒ Array<Boltless::ResultRow> Also known as: each_pair
Calls the user given block once for each key/columns of the row, passing the key-value pair as parameters. If no block is given, an enumerator is returned instead.
67 68 69 70 71 |
# File 'lib/boltless/result_row.rb', line 67 def each columns.each_with_index do |column, idx| yield(column, values[idx]) end end |
#pretty_print(pp) ⇒ Object
Pretty print the result row structure in a meaningful way.
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/boltless/result_row.rb', line 77 def pretty_print(pp) pp.object_group(self) do pp.breakable %i[columns values meta graph].each_with_index do |key, idx| pp.text("#{key}=") pp.pp(send(key)) pp.comma_breakable if idx < 3 end end end |
#to_h ⇒ Hash{Symbol => Mixed}
Return the assembled row as Ruby hash.
*Heads up!* This method is quite costly (time and heap memory) on large result sets, as it merges the column data with the row data in order to return an assembled hash. Use with caution.
43 44 45 |
# File 'lib/boltless/result_row.rb', line 43 def to_h columns.zip(values).to_h end |
#value ⇒ Mixed
A convenience shortcut for the first value of the row. This comes in very handy for single-value/single-row Cypher statements like RETURN date() AS date.
32 33 34 |
# File 'lib/boltless/result_row.rb', line 32 def value values.first end |