Class: RailsDb::TableData

Inherits:
Object
  • Object
show all
Includes:
Connection, TablePagination
Defined in:
lib/rails_db/table_data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TablePagination

#next_page, #paginate, #previous_page, #total_entries, #total_pages

Methods included from Connection

#connection

Constructor Details

#initialize(table) ⇒ TableData

Returns a new instance of TableData.



12
13
14
# File 'lib/rails_db/table_data.rb', line 12

def initialize(table)
  @table = table
end

Instance Attribute Details

#current_pageObject

Returns the value of attribute current_page.



7
8
9
# File 'lib/rails_db/table_data.rb', line 7

def current_page
  @current_page
end

#offsetObject

Returns the value of attribute offset.



7
8
9
# File 'lib/rails_db/table_data.rb', line 7

def offset
  @offset
end

#per_pageObject

Returns the value of attribute per_page.



7
8
9
# File 'lib/rails_db/table_data.rb', line 7

def per_page
  @per_page
end

#sort_columnObject

Returns the value of attribute sort_column.



7
8
9
# File 'lib/rails_db/table_data.rb', line 7

def sort_column
  @sort_column
end

#sort_orderObject

Returns the value of attribute sort_order.



7
8
9
# File 'lib/rails_db/table_data.rb', line 7

def sort_order
  @sort_order
end

#tableObject (readonly)

Returns the value of attribute table.



6
7
8
# File 'lib/rails_db/table_data.rb', line 6

def table
  @table
end

#timeObject (readonly)

Returns the value of attribute time.



6
7
8
# File 'lib/rails_db/table_data.rb', line 6

def time
  @time
end

Instance Method Details

#countObject



30
31
32
# File 'lib/rails_db/table_data.rb', line 30

def count
  Database.adapter.exec_query("SELECT COUNT(*) FROM #{table.name}")[0].rows.flatten.last.to_i
end

#dataObject



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rails_db/table_data.rb', line 16

def data
  @data ||= begin
    commands = ["SELECT * FROM #{table.name}"]
    if sort_column
      commands.push("ORDER BY #{sort_column} #{sort_order}")
    end
    if per_page && offset
      commands.push("LIMIT #{per_page} OFFSET #{offset}")
    end
    results, @time = Database.adapter.exec_query(commands.join(' '))
    results
  end
end