Class: RR::TypeCastingCursor

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyrep/type_casting_cursor.rb

Overview

Provides functionality to cast a query result value into the correct ruby type. Requires originating table and column to be known.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, table, cursor) ⇒ TypeCastingCursor

Creates a new TypeCastingCursor based on provided database connection and table name for the provided database query cursor



18
19
20
21
22
# File 'lib/rubyrep/type_casting_cursor.rb', line 18

def initialize(connection, table, cursor)
  self.org_cursor = cursor
  self.columns = {}
  connection.columns(table).each {|c| columns[c.name] = c}
end

Instance Attribute Details

#columnsObject

A column_name => Column cache



14
15
16
# File 'lib/rubyrep/type_casting_cursor.rb', line 14

def columns
  @columns
end

#org_cursorObject

The original cursor object



11
12
13
# File 'lib/rubyrep/type_casting_cursor.rb', line 11

def org_cursor
  @org_cursor
end

Instance Method Details

#clearObject



8
# File 'lib/rubyrep/type_casting_cursor.rb', line 8

def clear; org_cursor.clear end

#next?Boolean

Delegate the uninteresting methods to the original cursor

Returns:

  • (Boolean)


7
# File 'lib/rubyrep/type_casting_cursor.rb', line 7

def next?; org_cursor.next? end

#next_rowObject

Reads the next row from the original cursor and returns the row with the type casted row values.



25
26
27
28
29
# File 'lib/rubyrep/type_casting_cursor.rb', line 25

def next_row
  row = org_cursor.next_row
  row.each {|column, value| row[column] = columns[column].type_cast value}
  row
end