Class: DB::Records

Inherits:
Object
  • Object
show all
Defined in:
lib/db/records.rb

Overview

A buffer of records.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(columns, rows) ⇒ Records

Initialize a new Records instance with columns and rows.



24
25
26
27
# File 'lib/db/records.rb', line 24

def initialize(columns, rows)
  @columns = columns
  @rows = rows
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



40
41
42
# File 'lib/db/records.rb', line 40

def columns
  @columns
end

#rowsObject (readonly)

Returns the value of attribute rows.



41
42
43
# File 'lib/db/records.rb', line 41

def rows
  @rows
end

Class Method Details

.wrap(result) ⇒ Object

Wrap a database result into a Records instance.



12
13
14
15
16
17
18
19
# File 'lib/db/records.rb', line 12

def self.wrap(result)
  # We want to avoid extra memory allocations when there are no columns:
  if result.field_count == 0
    return nil
  end
  
  return self.new(result.field_names, result.to_a)
end

Instance Method Details

#freezeObject

Freeze the Records instance and its internal data structures.



31
32
33
34
35
36
37
38
# File 'lib/db/records.rb', line 31

def freeze
  return self if frozen?
  
  @columns.freeze
  @rows.freeze
  
  super
end

#to_aObject

Get the rows as an array.



45
46
47
# File 'lib/db/records.rb', line 45

def to_a
  @rows
end