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

Returns:

  • (Object)

    the current value of graph



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

def graph
  @graph
end

#metaObject

Returns the value of attribute meta

Returns:

  • (Object)

    the current value of meta



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

def meta
  @meta
end

#resultObject

Returns the value of attribute result

Returns:

  • (Object)

    the current value of result



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

def result
  @result
end

#valuesObject

Returns the value of attribute values

Returns:

  • (Object)

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

Parameters:

  • key (Symbol, String)

    the key to fetch the value for

Returns:

  • (Mixed)

    the value for the given key



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.

Returns:

  • (Hash{String => Mixed})

    the JSON hash representation



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.

Parameters:

  • block (Proc)

    the block which is called for each result row

Yield Parameters:

  • a (Symbol)

    row column/key

  • a (Mixed)

    row value for the column/key

Returns:



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.

Parameters:

  • pp (PP)

    a pretty printer instance to use



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.

Returns:

  • (Hash{Symbol => Mixed})

    the mapped result row



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.

Returns:

  • (Mixed)

    the first value of the row



32
33
34
# File 'lib/boltless/result_row.rb', line 32

def value
  values.first
end