Class: DBA::Printer

Inherits:
Object
  • Object
show all
Defined in:
lib/dba/printer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io = STDOUT) ⇒ Printer

Returns a new instance of Printer.



7
8
9
# File 'lib/dba/printer.rb', line 7

def initialize(io = STDOUT)
  @io = io
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



11
12
13
# File 'lib/dba/printer.rb', line 11

def io
  @io
end

Instance Method Details



42
43
44
45
46
47
48
# File 'lib/dba/printer.rb', line 42

def print_diff(before_lines, after_lines)
  removed = before_lines - after_lines
  removed.each { |line| io.puts pastel.red("- #{line}") }

  added = after_lines - before_lines
  added.each { |line| io.puts pastel.bright_black("+ #{line}") }
end


17
18
19
# File 'lib/dba/printer.rb', line 17

def print_error(message)
  io.puts(pastel.red('ERROR: ' + message))
end


69
70
71
72
73
74
75
76
77
78
# File 'lib/dba/printer.rb', line 69

def print_indexes(indexes)
  indexes.each do |index_name, info_hash|
    fields = []
    fields << index_name
    fields << muted('(' + info_hash.fetch(:columns).map(&:to_s).join(', ') + ')')
    fields << muted('{unique}') if info_hash[:unique]

    io.puts fields.join(' ')
  end
end


13
14
15
# File 'lib/dba/printer.rb', line 13

def print_line
  io.puts
end


21
22
23
24
25
# File 'lib/dba/printer.rb', line 21

def print_row(hash)
  hash.each do |name, value|
    io.puts muted("#{name}: ") + format(value)
  end
end


56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dba/printer.rb', line 56

def print_schema(table_name, schema_hash)
  schema_hash.each do |column_name, info_hash|
    fields = []
    fields << "#{table_name}.#{column_name}"
    fields << muted(format_column_type(info_hash))
    fields << muted('{primary}') if info_hash[:primary_key]

    io.puts fields.join(' ')
  end

  io.puts
end


50
51
52
53
54
# File 'lib/dba/printer.rb', line 50

def print_table(name, row_count)
  rows = muted("#{row_count} rows")

  io.puts "#{name} #{rows}"
end


29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dba/printer.rb', line 29

def print_usage(program_name, command_parameters)
  io.puts "Usage: #{program_name} COMMAND"
  io.puts

  command_parameters.each do |command_name, parameters|
    parameters = parameters.map { |type, name| format_parameter(type, name) }.compact.join(' ').upcase

    io.puts "  #{program_name} #{command_name} #{parameters}"
  end

  io.puts
end