Class: Boltless::ResultRow

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

Instance Method Summary collapse

Instance Attribute Details

#graphObject

Returns the value of attribute graph



8
9
10
# File 'lib/boltless/result_row.rb', line 8

def graph
  @graph
end

#metaObject

Returns the value of attribute meta



8
9
10
# File 'lib/boltless/result_row.rb', line 8

def meta
  @meta
end

#resultObject

Returns the value of attribute result



8
9
10
# File 'lib/boltless/result_row.rb', line 8

def result
  @result
end

#valuesObject

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_jsonHash{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.

Yield Parameters:

  • a (Symbol)

    row column/key

  • a (Mixed)

    row value for the column/key



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_hHash{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

#valueMixed

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