Class: Boltless::Result
- Inherits:
-
Struct
- Object
- Struct
- Boltless::Result
- Includes:
- Enumerable
- Defined in:
- lib/boltless/result.rb
Overview
A lightweight struct representation of a single result object from the neo4j HTTP API.
Instance Attribute Summary collapse
-
#columns ⇒ Object
Returns the value of attribute columns.
-
#rows ⇒ Object
Returns the value of attribute rows.
-
#stats ⇒ Object
Returns the value of attribute stats.
Class Method Summary collapse
-
.from(hash) ⇒ Boltless::Result
Build a mapped result structure from the given raw result hash.
Instance Method Summary collapse
-
#each(&block) {|a| ... } ⇒ Array<Boltless::ResultRow>
Yields each row of the result.
-
#pretty_print(pp) ⇒ Object
Pretty print the result structure in a meaningful way.
-
#value ⇒ Mixed
A convenience shortcut for the first row of the result, and its first value.
-
#values ⇒ Array<Hash{Symbol => Mixed}>
A convenience method to access all mapped result rows as hashes.
Instance Attribute Details
#columns ⇒ Object
Returns the value of attribute columns
6 7 8 |
# File 'lib/boltless/result.rb', line 6 def columns @columns end |
#rows ⇒ Object
Returns the value of attribute rows
6 7 8 |
# File 'lib/boltless/result.rb', line 6 def rows @rows end |
#stats ⇒ Object
Returns the value of attribute stats
6 7 8 |
# File 'lib/boltless/result.rb', line 6 def stats @stats end |
Class Method Details
.from(hash) ⇒ Boltless::Result
Build a mapped result structure from the given raw result hash.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/boltless/result.rb', line 12 def self.from(hash) # We setup an empty result struct first cols = hash[:columns].map(&:to_sym) Result.new(cols, [], hash[:stats]).tap do |res| # Then we re-map each row from the given hash res.rows = hash[:data].map do |datum| ResultRow.new(res, datum[:row], datum[:meta], datum[:graph]) end end end |
Instance Method Details
#each(&block) {|a| ... } ⇒ Array<Boltless::ResultRow>
Yields each row of the result. This is the foundation to the Enumerable
interface, so all its methods (eg. #count
, etc) are available with this. If no block is given, an Enumerator is returned.
34 35 36 |
# File 'lib/boltless/result.rb', line 34 def each(&block) rows.each(&block) end |
#pretty_print(pp) ⇒ Object
Pretty print the result structure in a meaningful way.
rubocop:disable Metrics/MethodLength because of the pretty
printing logic
rubocop:disable Metrics/AbcSize dito
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/boltless/result.rb', line 71 def pretty_print(pp) pp.object_group(self) do pp.breakable pp.text('columns=') pp.pp(columns) pp.comma_breakable pp.text('rows=') if rows.count > 1 pp.group(1, '[', ']') do pp.pp(first) pp.comma_breakable pp.text("[+#{rows.count - 1} ..]") end else pp.pp(rows) end pp.comma_breakable pp.text('stats=') pp.pp(stats) end end |
#value ⇒ Mixed
A convenience shortcut for the first row of the result, and its first value. This comes in very handy for single-value/single-row Cypher statements like RETURN date() AS date. Or probing Cypher statements like MATCH (n:User { name: $name }) RETURN 1 LIMIT 1.
47 48 49 |
# File 'lib/boltless/result.rb', line 47 def value rows.first.values.first end |
#values ⇒ Array<Hash{Symbol => Mixed}>
A convenience method to access all mapped result rows as hashes.
*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. (Pro Tip: Iterate over the rows and pluck/[]
the result keys you are interested in, instead of the “grab everything” style)
60 61 62 |
# File 'lib/boltless/result.rb', line 60 def values rows.map(&:to_h) end |