Class: RailsDb::TableData

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

Constant Summary

Constants included from TablePagination

RailsDb::TablePagination::DEFAULT_PAGINATION_PER_PAGE

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

#column_names, #column_properties, #connection, #to_param

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

#select_columnsObject

Returns the value of attribute select_columns.



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

def select_columns
  @select_columns
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

#ascObject



45
46
47
48
# File 'lib/rails_db/table_data.rb', line 45

def asc
  self.sort_order = 'asc'
  self
end

#columnsObject



65
66
67
68
69
70
71
# File 'lib/rails_db/table_data.rb', line 65

def columns
  if select_columns && select_columns.any?
    select_columns
  else
    table.column_names
  end
end

#countObject



73
74
75
# File 'lib/rails_db/table_data.rb', line 73

def count
  Database.count(table.name)
end

#dataObject



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

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

#descObject



40
41
42
43
# File 'lib/rails_db/table_data.rb', line 40

def desc
  self.sort_order = 'desc'
  self
end

#limit(limit) ⇒ Object



35
36
37
38
# File 'lib/rails_db/table_data.rb', line 35

def limit(limit)
  self.per_page = limit
  self
end

#order(sort_order) ⇒ Object



55
56
57
58
# File 'lib/rails_db/table_data.rb', line 55

def order(sort_order)
  self.send(sort_order) if [:asc, :desc].include?(sort_order.to_sym)
  self
end

#order_by(column) ⇒ Object



50
51
52
53
# File 'lib/rails_db/table_data.rb', line 50

def order_by(column)
  self.sort_column = column
  self
end

#select(*select_columns) ⇒ Object



60
61
62
63
# File 'lib/rails_db/table_data.rb', line 60

def select(*select_columns)
  self.select_columns = Array.wrap(select_columns).flatten
  self
end