class DBA::Printer
Attributes
io[R]
Public Class Methods
new(io = STDOUT)
click to toggle source
# File lib/dba/printer.rb, line 7 def initialize(io = STDOUT) @io = io end
Public Instance Methods
print_diff(before_lines, after_lines)
click to toggle source
# 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
print_error(message)
click to toggle source
# File lib/dba/printer.rb, line 17 def print_error(message) io.puts(pastel.red('ERROR: ' + message)) end
print_indexes(indexes)
click to toggle source
# 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
print_line()
click to toggle source
# File lib/dba/printer.rb, line 13 def print_line io.puts end
print_row(hash)
click to toggle source
# File lib/dba/printer.rb, line 21 def print_row(hash) hash.each do |name, value| io.puts muted("#{name}: ") + format(value) end end
Also aliased as: print
print_schema(table_name, schema_hash)
click to toggle source
# 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
print_table(name, row_count)
click to toggle source
# File lib/dba/printer.rb, line 50 def print_table(name, row_count) rows = muted("#{row_count} rows") io.puts "#{name} #{rows}" end
print_usage(program_name, command_parameters)
click to toggle source
# 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
Private Instance Methods
format(value)
click to toggle source
# File lib/dba/printer.rb, line 82 def format(value) case value when NilClass null when BigDecimal value.to_s('F') when Time value.strftime('%F %T') else value.to_s end end
format_column_type(info_hash)
click to toggle source
# File lib/dba/printer.rb, line 102 def format_column_type(info_hash) return info_hash[:db_type] unless info_hash[:type] return info_hash[:type] unless info_hash[:db_type]&.end_with?('[]') info_hash[:type].to_s + '[]' end
format_parameter(type, name)
click to toggle source
# File lib/dba/printer.rb, line 95 def format_parameter(type, name) case type when :req then name when :opt then "[#{name}]" end end
muted(text)
click to toggle source
# File lib/dba/printer.rb, line 112 def muted(text) return text unless io.isatty pastel.bright_black(text.to_s) end
null()
click to toggle source
# File lib/dba/printer.rb, line 108 def null @null ||= muted('NULL') end
pastel()
click to toggle source
# File lib/dba/printer.rb, line 118 def pastel @pastel ||= Pastel.new end