1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Wrap entities into the LsEntity facility

And thus simplify "#process".
This commit is contained in:
Kyrylo Silin 2013-12-01 18:47:50 +02:00
parent 362e8c7e6d
commit 7bf6c15893
2 changed files with 91 additions and 29 deletions

View file

@ -1,13 +1,4 @@
require 'pry/commands/ls/grep'
require 'pry/commands/ls/formatter'
require 'pry/commands/ls/globals'
require 'pry/commands/ls/constants'
require 'pry/commands/ls/methods'
require 'pry/commands/ls/self_methods'
require 'pry/commands/ls/instance_vars'
require 'pry/commands/ls/local_names'
require 'pry/commands/ls/local_vars'
require 'pry/commands/ls/ls_entity'
class Pry
class Command::Ls < Pry::ClassCommand
match 'ls'
@ -56,32 +47,30 @@ class Pry
attr_reader :interrogatee
# Exclude -q, -v and --grep because they,
# don't specify what the user wants to see.
def no_user_opts?
!(opts[:methods] || opts['instance-methods'] || opts[:ppp] ||
opts[:globals] || opts[:locals] || opts[:constants] || opts[:ivars])
end
def process
@interrogatee = args.empty? ? target_self : target.eval(args.join(' '))
# Exclude -q, -v and --grep because they,
# don't specify what the user wants to see.
no_user_opts = !(
opts[:methods] || opts['instance-methods'] || opts[:ppp] ||
opts[:globals] || opts[:locals] || opts[:constants] || opts[:ivars]
)
raise_errors_if_arguments_are_weird
grep = Grep.new(Regexp.new(opts[:G] || '.'))
greppable = proc { |o| o.grep = grep; o }
no_user_opts = no_user_opts?
entities = [
greppable[Globals.new(target, opts)],
greppable[Constants.new(interrogatee, target, no_user_opts, opts)],
greppable[Methods.new(interrogatee, no_user_opts, opts)],
greppable[SelfMethods.new(interrogatee, no_user_opts, opts)],
InstanceVars.new(interrogatee, no_user_opts, opts),
greppable[LocalNames.new(target, no_user_opts, _pry_.sticky_locals, args)],
LocalVars.new(target, _pry_.sticky_locals, opts)
]
ls_entity = LsEntity.new({
interrogatee: interrogatee,
target: target,
no_user_opts: no_user_opts,
opts: opts,
sticky_locals: _pry_.sticky_locals,
args: args
})
stagger_output(entities.map(&:write_out).reject { |o| !o }.join(''))
stagger_output(ls_entity.entities_table)
end
private

View file

@ -0,0 +1,73 @@
require 'pry/commands/ls/grep'
require 'pry/commands/ls/formatter'
require 'pry/commands/ls/globals'
require 'pry/commands/ls/constants'
require 'pry/commands/ls/methods'
require 'pry/commands/ls/self_methods'
require 'pry/commands/ls/instance_vars'
require 'pry/commands/ls/local_names'
require 'pry/commands/ls/local_vars'
class Pry
class Command::Ls < Pry::ClassCommand
class LsEntity
def initialize(opts)
@interrogatee = opts[:interrogatee]
@target = opts[:target]
@no_user_opts = opts[:no_user_opts]
@opts = opts[:opts]
@sticky_locals = opts[:sticky_locals]
@args = opts[:args]
@grep = Grep.new(Regexp.new(opts[:opts][:G] || '.'))
end
def entities_table
entities.map(&:write_out).reject { |o| !o }.join('')
end
private
def greppable
proc do |entity|
entity.tap { |o| o.grep = @grep }
end
end
def globals
greppable[Globals.new(@target, @opts)]
end
def constants
greppable[Constants.new(@interrogatee, @target, @no_user_opts, @opts)]
end
def methods
greppable[Methods.new(@interrogatee, @no_user_opts, @opts)]
end
def self_methods
greppable[SelfMethods.new(@interrogatee, @no_user_opts, @opts)]
end
def instance_vars
InstanceVars.new(@interrogatee, @no_user_opts, @opts)
end
def local_names
greppable[LocalNames.new(@target, @no_user_opts, @sticky_locals, @args)]
end
def local_vars
LocalVars.new(@target, @sticky_locals, @opts)
end
def entities
[globals, constants, methods, self_methods, instance_vars, local_names,
local_vars]
end
end
end
end