Class: ActiveCypher::Bolt::Result
- Inherits:
-
Object
- Object
- ActiveCypher::Bolt::Result
- Includes:
- Enumerable
- Defined in:
- lib/active_cypher/bolt/result.rb
Overview
Handles query results, streams records, and provides summary information. Represents the result of a Cypher query execution.
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#qid ⇒ Object
readonly
Returns the value of attribute qid.
-
#summary_metadata ⇒ Object
readonly
Returns the value of attribute summary_metadata.
Instance Method Summary collapse
-
#consume ⇒ Object
Marks the result as fully consumed.
-
#each ⇒ Object
Allows iterating over the records using ‘each`.
-
#initialize(fields, records, summary_metadata, qid = -1)) ⇒ Result
constructor
A new instance of Result.
-
#open? ⇒ Boolean
Checks if the result stream is still open (i.e., not fully consumed).
-
#single ⇒ Hash
Retrieves a single record.
-
#summary ⇒ Hash
Provides summary information about the query execution.
-
#to_a ⇒ Array<Hash>
Retrieves all records as an array of hashes.
Constructor Details
#initialize(fields, records, summary_metadata, qid = -1)) ⇒ Result
Returns a new instance of Result.
16 17 18 19 20 21 22 23 |
# File 'lib/active_cypher/bolt/result.rb', line 16 def initialize(fields, records, , qid = -1) @fields = fields || [] # Ensure fields is an array @records = records @summary_metadata = || {} @qid = qid @consumed = false @record_index = 0 end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
10 11 12 |
# File 'lib/active_cypher/bolt/result.rb', line 10 def fields @fields end |
#qid ⇒ Object (readonly)
Returns the value of attribute qid.
10 11 12 |
# File 'lib/active_cypher/bolt/result.rb', line 10 def qid @qid end |
#summary_metadata ⇒ Object (readonly)
Returns the value of attribute summary_metadata.
10 11 12 |
# File 'lib/active_cypher/bolt/result.rb', line 10 def @summary_metadata end |
Instance Method Details
#consume ⇒ Object
Marks the result as fully consumed.
68 69 70 71 |
# File 'lib/active_cypher/bolt/result.rb', line 68 def consume @consumed = true # Potentially release resources if streaming was implemented differently end |
#each ⇒ Object
Allows iterating over the records using ‘each`. Yields each record as a Hash with field names as keys (symbols).
27 28 29 30 31 32 33 34 35 |
# File 'lib/active_cypher/bolt/result.rb', line 27 def each raise 'Result already consumed or closed' if @consumed return enum_for(:each) unless block_given? # Return enumerator if no block @records.each do |record_values| yield @fields.map(&:to_sym).zip(record_values).to_h end consume # Mark as consumed after successful iteration end |
#open? ⇒ Boolean
Checks if the result stream is still open (i.e., not fully consumed).
63 64 65 |
# File 'lib/active_cypher/bolt/result.rb', line 63 def open? !@consumed end |
#single ⇒ Hash
Retrieves a single record. Raises error if no records or more than one.
40 41 42 43 44 45 46 47 |
# File 'lib/active_cypher/bolt/result.rb', line 40 def single raise 'Result already consumed or closed' if @consumed raise "Expected exactly one record, but found #{@records.size}" unless @records.size == 1 record = @fields.map(&:to_sym).zip(@records.first).to_h consume record end |
#summary ⇒ Hash
Provides summary information about the query execution.
75 76 77 78 79 |
# File 'lib/active_cypher/bolt/result.rb', line 75 def summary # TODO: Parse summary_metadata into a more structured Summary object? consume unless @consumed # Ensure result is consumed before accessing summary @summary_metadata end |
#to_a ⇒ Array<Hash>
Retrieves all records as an array of hashes.
51 52 53 54 55 56 57 58 59 |
# File 'lib/active_cypher/bolt/result.rb', line 51 def to_a raise 'Result already consumed or closed' if @consumed result_array = @records.map do |record_values| @fields.map(&:to_sym).zip(record_values).to_h end consume result_array end |