Class: Amalgalite::Result::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/amalgalite/result/row.rb

Overview

The class that represents a single row from a query.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result: nil, field_map:, values:) ⇒ Row

Returns a new instance of Row.



24
25
26
27
28
29
# File 'lib/amalgalite/result/row.rb', line 24

def initialize(result: nil, field_map:, values:)
  @result = result
  @field_map = field_map
  @values = values
  self.freeze
end

Instance Attribute Details

#field_mapObject (readonly)

A Hash that maps the field names to indexes in values This may be linked to shared field from the result



18
19
20
# File 'lib/amalgalite/result/row.rb', line 18

def field_map
  @field_map
end

#resultObject (readonly)

The result object this row is retrivved from, nil if all values are materialized



14
15
16
# File 'lib/amalgalite/result/row.rb', line 14

def result
  @result
end

#valuesObject (readonly) Also known as: to_a

An array containing the values from the row



21
22
23
# File 'lib/amalgalite/result/row.rb', line 21

def values
  @values
end

Instance Method Details

#fetch(field_name_or_index) ⇒ Object Also known as: []



45
46
47
48
49
50
51
52
53
54
# File 'lib/amalgalite/result/row.rb', line 45

def fetch(field_name_or_index)
  case field_name_or_index
  when Integer
    @values[field_name_or_index]
  when Symbol, String
    @values[field_map[field_name_or_index]]
  else
    raise Amalgalite::Error, "Unknown type (#{field_name_or_index.class}) of key for a Row value: #{field_name_or_index}"
  end
end

#fieldsObject



31
32
33
# File 'lib/amalgalite/result/row.rb', line 31

def fields
  @field_map.keys
end

#firstObject



57
58
59
# File 'lib/amalgalite/result/row.rb', line 57

def first
  fetch(0)
end

#lengthObject



61
62
63
# File 'lib/amalgalite/result/row.rb', line 61

def length
  @values.size
end

#store(field_name_or_index, value) ⇒ Object Also known as: []=



39
40
41
42
# File 'lib/amalgalite/result/row.rb', line 39

def store(field_name_or_index, value)
  index = field_name_or_index_to_index(field_name_or_index)
  @values[index] = value
end

#store_by_index(index, value) ⇒ Object



35
36
37
# File 'lib/amalgalite/result/row.rb', line 35

def store_by_index(index, value)
  @values[index] = value
end

#to_hObject



65
66
67
# File 'lib/amalgalite/result/row.rb', line 65

def to_h
  Hash[@field_map.keys.zip(values)]
end