Class: DBA::Sample

Inherits:
Command show all
Defined in:
lib/dba/sample.rb

Instance Attribute Summary

Attributes inherited from Command

#database, #table_name

Instance Method Summary collapse

Methods inherited from Command

arity_check, #initialize

Constructor Details

This class inherits a constructor from DBA::Command

Instance Method Details

#call(table, column = nil) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/dba/sample.rb', line 2

def call(table, column = nil)
  self.table_name = table

  column_name = column.to_sym if column

  if column_name
    dataset = database[table_name].exclude(column_name => nil).distinct.select(column_name)
    dataset.from_self.order(random_function).limit(20).each do |row|
      puts row[column_name]
    end
  else
    database[table_name].order(random_function).first(3).each do |row|
      printer.print(row)
      printer.print_line
    end
  end
end