Class: RailsDb::TableData
- Inherits:
-
Object
- Object
- RailsDb::TableData
- 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
-
#current_page ⇒ Object
Returns the value of attribute current_page.
-
#offset ⇒ Object
Returns the value of attribute offset.
-
#per_page ⇒ Object
Returns the value of attribute per_page.
-
#select_columns ⇒ Object
Returns the value of attribute select_columns.
-
#sort_column ⇒ Object
Returns the value of attribute sort_column.
-
#sort_order ⇒ Object
Returns the value of attribute sort_order.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
Instance Method Summary collapse
- #asc ⇒ Object
- #columns ⇒ Object
- #count ⇒ Object
- #data ⇒ Object
- #desc ⇒ Object
-
#initialize(table) ⇒ TableData
constructor
A new instance of TableData.
- #limit(limit) ⇒ Object
- #order(sort_order) ⇒ Object
- #order_by(column) ⇒ Object
- #select(*select_columns) ⇒ Object
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_page ⇒ Object
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 |
#offset ⇒ Object
Returns the value of attribute offset.
7 8 9 |
# File 'lib/rails_db/table_data.rb', line 7 def offset @offset end |
#per_page ⇒ Object
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_columns ⇒ Object
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_column ⇒ Object
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_order ⇒ Object
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 |
#table ⇒ Object (readonly)
Returns the value of attribute table.
6 7 8 |
# File 'lib/rails_db/table_data.rb', line 6 def table @table end |
#time ⇒ Object (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
#asc ⇒ Object
45 46 47 48 |
# File 'lib/rails_db/table_data.rb', line 45 def asc self.sort_order = 'asc' self end |
#columns ⇒ Object
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 |
#count ⇒ Object
73 74 75 |
# File 'lib/rails_db/table_data.rb', line 73 def count Database.adapter.exec_query("SELECT COUNT(*) FROM #{table.name}")[0].rows.flatten.last.to_i end |
#data ⇒ Object
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.adapter.exec_query(commands.join(' ')) results end end |
#desc ⇒ Object
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 |