Sha256: 40f8b9b0634f453b9d837a7ae04f89641528a14c55c1512d20bedcf38d563d26

Contents?: true

Size: 1.69 KB

Versions: 7

Compression:

Stored size: 1.69 KB

Contents

# encoding: UTF-8

class PryRails::ShowModels < Pry::ClassCommand
  match "show-models"
  group "Rails"
  description "Show all models."

  def options(opt)
    opt.banner unindent <<-USAGE
      Usage: show-models

      show-models displays the current Rails app's models.
    USAGE

    opt.on :G, "grep", "Filter output by regular expression", :argument => true
  end

  def process
    Rails.application.eager_load!

    @formatter = PryRails::ModelFormatter.new

    display_activerecord_models
    display_mongoid_models
  end

  def display_activerecord_models
    return unless defined?(ActiveRecord::Base)

    models = ActiveRecord::Base.descendants

    models.sort_by(&:to_s).each do |model|
      print_unless_filtered @formatter.format_active_record(model)
    end
  end

  def display_mongoid_models
    return unless defined?(Mongoid::Document)

    models = []

    ObjectSpace.each_object do |o|
      is_model = false

      begin
        is_model = o.class == Class && o.ancestors.include?(Mongoid::Document)
      rescue
        # If it's a weird object, it's not what we want anyway.
      end

      models << o if is_model
    end

    models.sort_by(&:to_s).each do |model|
      print_unless_filtered @formatter.format_mongoid(model)
    end
  end

  def print_unless_filtered(str)
    if opts.present?(:G)
      return unless str =~ grep_regex
      str = colorize_matches(str) # :(
    end
    output.puts str
  end

  def colorize_matches(string)
    if Pry.color
      string.to_s.gsub(grep_regex) { |s| "\e[7m#{s}\e[27m" }
    else
      string
    end
  end

  def grep_regex
    @grep_regex ||= Regexp.new(opts[:G], Regexp::IGNORECASE)
  end
end

PryRails::Commands.add_command PryRails::ShowModels

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
pry-rails-0.3.8 lib/pry-rails/commands/show_models.rb
pry-rails-0.3.7 lib/pry-rails/commands/show_models.rb
pry-rails-0.3.6 lib/pry-rails/commands/show_models.rb
pry-rails-0.3.5 lib/pry-rails/commands/show_models.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/pry-rails-0.3.4/lib/pry-rails/commands/show_models.rb
pry-rails-0.3.4 lib/pry-rails/commands/show_models.rb
pry-rails-0.3.3 lib/pry-rails/commands/show_models.rb