class Rubannotate::Annotator

Attributes

config[R]

Public Class Methods

new() click to toggle source
# File lib/rubannotate/annotator.rb, line 7
def initialize
  @config = Config.new
  @logger = ::Logger.new(STDOUT, formatter: proc { |_, _, _, msg| "#{msg}\n" })
end

Public Instance Methods

annotate(class_names = nil) click to toggle source
# File lib/rubannotate/annotator.rb, line 12
def annotate(class_names = nil)
  load_environments
  load_config

  class_names = Array(class_names)
  classes = class_names.empty? ? load_default_classes : class_names.map(&:constantize)

  paths = models_path
  connection = ::ActiveRecord::Base.connection

  info '== Starting annotate '.ljust(79, '=')
  classes.each do |c|
    table_name = c.table_name
    next unless table_name
    next unless connection.table_exists?(table_name)

    paths.each do |path|
      filepath = path.join(c.name.underscore + '.rb')
      next unless filepath.exist?

      info "   -> Processing #{filepath.sub(app_path.to_s + File::SEPARATOR, '')}"

      io = StringIO.new
      SchemaDumper.new(table_name, connection).dump(io)

      str = io.tap(&:rewind).read
      Writer.new(filepath).write(str)
    end
  end
end
cleanup() click to toggle source
# File lib/rubannotate/annotator.rb, line 43
def cleanup
  load_environments
  load_config

  models_path.each do |path|
    path.glob('**/*.rb').each do |filepath|
      Writer.new(filepath).cleanup
    end
  end
end

Private Instance Methods

app_path() click to toggle source
# File lib/rubannotate/annotator.rb, line 75
def app_path
  ::Pathname.pwd
end
info(message) click to toggle source
# File lib/rubannotate/annotator.rb, line 85
def info(message)
  @logger.info message
end
load_config() click to toggle source
# File lib/rubannotate/annotator.rb, line 61
def load_config
  @logger.level = @config.logging_level
end
load_default_classes() click to toggle source
# File lib/rubannotate/annotator.rb, line 65
def load_default_classes
  models_path.each do |path|
    Dir.glob('**/*rb', base: path).each do |f|
      require_dependency(path.join(f))
    end
  end

  ::ActiveRecord::Base.descendants
end
load_environments() click to toggle source
# File lib/rubannotate/annotator.rb, line 56
def load_environments
  require 'rails'
  require app_path.join('config', 'environment.rb')
end
models_path() click to toggle source
# File lib/rubannotate/annotator.rb, line 79
def models_path
  config.models_path.map do |path|
    app_path.join(path)
  end
end