From 7bf6c15893beb190530c87d262699645b3223e8e Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Sun, 1 Dec 2013 18:47:50 +0200 Subject: [PATCH] Wrap entities into the LsEntity facility And thus simplify "#process". --- lib/pry/commands/ls.rb | 47 ++++++++------------ lib/pry/commands/ls/ls_entity.rb | 73 ++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 29 deletions(-) create mode 100644 lib/pry/commands/ls/ls_entity.rb diff --git a/lib/pry/commands/ls.rb b/lib/pry/commands/ls.rb index 2edc3bd2..32b6c054 100644 --- a/lib/pry/commands/ls.rb +++ b/lib/pry/commands/ls.rb @@ -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 diff --git a/lib/pry/commands/ls/ls_entity.rb b/lib/pry/commands/ls/ls_entity.rb new file mode 100644 index 00000000..b7d5f931 --- /dev/null +++ b/lib/pry/commands/ls/ls_entity.rb @@ -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