summaryrefslogtreecommitdiff
path: root/lib/pry/commands/ls/instance_vars.rb
blob: 2f33677f3cc916021ac78d1d90532e4d9c54e239 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
require 'pry/commands/ls/interrogatable'

class Pry
  class Command::Ls < Pry::ClassCommand
    class InstanceVars < Pry::Command::Ls::Formatter
      include Pry::Command::Ls::Interrogatable

      def initialize(interrogatee, no_user_opts, opts, _pry_)
        super(_pry_)
        @interrogatee = interrogatee
        @no_user_opts = no_user_opts
        @default_switch = opts[:ivars]
      end

      def correct_opts?
        super || @no_user_opts
      end

      def output_self
        ivars = if Object === @interrogatee
                  Pry::Method.safe_send(@interrogatee, :instance_variables)
                else
                  [] #TODO: BasicObject support
                end
        kvars = Pry::Method.safe_send(interrogatee_mod, :class_variables)
        ivars_out = output_section('instance variables', format(:instance_var, ivars))
        kvars_out = output_section('class variables', format(:class_var, kvars))
        ivars_out + kvars_out
      end

      private

      def format(type, vars)
        vars.sort_by { |var| var.to_s.downcase }.map { |var| color(type, var) }
      end

    end
  end
end