mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
Refactor the way how ls
displays constants
I also had to refactor global variables a bit, because the more entities I have to display, the harder the code is.
This commit is contained in:
parent
acdab50b25
commit
63f12424d1
3 changed files with 60 additions and 5 deletions
51
lib/pry/commands/ls/constants.rb
Normal file
51
lib/pry/commands/ls/constants.rb
Normal file
|
@ -0,0 +1,51 @@
|
|||
class Pry
|
||||
class Command::Ls < Pry::ClassCommand
|
||||
class Constants < Pry::Command::Ls::Formatter
|
||||
|
||||
def initialize(interrogatee, target, has_any_opts, opts)
|
||||
super(target)
|
||||
@interrogatee = interrogatee
|
||||
@has_any_opts = has_any_opts
|
||||
@default_switch = opts[:constants]
|
||||
@verbose_switch = opts[:verbose]
|
||||
end
|
||||
|
||||
def correct_opts?
|
||||
super || (!@has_any_opts && Module === @interrogatee)
|
||||
end
|
||||
|
||||
def interrogatee_mod
|
||||
if Module === @interrogatee
|
||||
@interrogatee
|
||||
else
|
||||
class << @interrogatee
|
||||
ancestors.grep(::Class).reject { |c| c == self }.first
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def output_self
|
||||
mod = interrogatee_mod
|
||||
constants = WrappedModule.new(mod).constants(@verbose_switch)
|
||||
output_section('constants', grep.regexp[format(mod, constants)])
|
||||
end
|
||||
|
||||
def format(mod, constants)
|
||||
constants.sort_by(&:downcase).map do |name|
|
||||
if const = (!mod.autoload?(name) && (mod.const_get(name) || true) rescue nil)
|
||||
if (const < Exception rescue false)
|
||||
color(:exception_constant, name)
|
||||
elsif (Module === mod.const_get(name) rescue false)
|
||||
color(:class_constant, name)
|
||||
else
|
||||
color(:constant, name)
|
||||
end
|
||||
else
|
||||
color(:unloaded_constant, name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
|
@ -2,6 +2,8 @@ class Pry
|
|||
class Command::Ls < Pry::ClassCommand
|
||||
class Formatter
|
||||
|
||||
attr_accessor :grep
|
||||
|
||||
def initialize(target)
|
||||
@target = target
|
||||
end
|
||||
|
@ -23,10 +25,14 @@ class Pry
|
|||
end
|
||||
|
||||
def write_out
|
||||
return unless @switch
|
||||
return false unless correct_opts?
|
||||
output_self
|
||||
end
|
||||
|
||||
def correct_opts?
|
||||
@default_switch
|
||||
end
|
||||
|
||||
def output_self
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
|
|
@ -19,11 +19,9 @@ class Pry
|
|||
$CHILD_STATUS $SAFE $ERROR_INFO $ERROR_POSITION $LAST_MATCH_INFO
|
||||
$LAST_PAREN_MATCH $LAST_READ_LINE $MATCH $POSTMATCH $PREMATCH)
|
||||
|
||||
attr_accessor :grep
|
||||
|
||||
def initialize(target, globals_switch = false)
|
||||
def initialize(target, opts)
|
||||
super(target)
|
||||
@switch = globals_switch
|
||||
@default_switch = opts[:globals]
|
||||
end
|
||||
|
||||
def output_self
|
||||
|
|
Loading…
Add table
Reference in a new issue