Class: Boltless::Result

Inherits:
Struct
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#columnsObject

Returns the value of attribute columns

Returns:

  • (Object)

    the current value of columns



6
7
8
# File 'lib/boltless/result.rb', line 6

def columns
  @columns
end

#rowsObject

Returns the value of attribute rows

Returns:

  • (Object)

    the current value of rows



6
7
8
# File 'lib/boltless/result.rb', line 6

def rows
  @rows
end

#statsObject

Returns the value of attribute stats

Returns:

  • (Object)

    the current value of 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.

Parameters:

  • hash (Hash{Symbol => Mixed})

    the raw neo4j result hash for a single statement

Returns:



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.

Parameters:

  • block (Proc)

    the block which is called for each result row

Yield Parameters:

Returns:



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

Parameters:

  • pp (PP)

    a pretty printer instance to use



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

#valueMixed

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.

Returns:

  • (Mixed)

    the first value of the first result row



47
48
49
# File 'lib/boltless/result.rb', line 47

def value
  rows.first.values.first
end

#valuesArray<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)

Returns:

  • (Array<Hash{Symbol => Mixed}>)

    the mapped result rows



60
61
62
# File 'lib/boltless/result.rb', line 60

def values
  rows.map(&:to_h)
end