Class: PryMoves::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/pry-moves/formatter.rb

Constant Summary collapse

MAX_PARAMS =
5
PATH_TRASH =
defined?(Rails) ? Rails.root.to_s : Dir.pwd

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(colorize = true) ⇒ Formatter

Returns a new instance of Formatter.



5
6
7
# File 'lib/pry-moves/formatter.rb', line 5

def initialize colorize = true
  @colorize = colorize
end

Instance Attribute Details

#colorizeObject

Returns the value of attribute colorize.



3
4
5
# File 'lib/pry-moves/formatter.rb', line 3

def colorize
  @colorize
end

Instance Method Details

#cut_string(str) ⇒ Object



55
56
57
58
# File 'lib/pry-moves/formatter.rb', line 55

def cut_string str
  return str unless str
  str.length > 50 ? "#{str.first 50}..." : str
end

#first_line(str) ⇒ Object



51
52
53
# File 'lib/pry-moves/formatter.rb', line 51

def first_line str
  str.split("\n").first
end

#format_arg(binding, arg_name) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/pry-moves/formatter.rb', line 42

def format_arg binding, arg_name
  arg = begin
    binding.eval(arg_name.to_s)
  rescue Exception
    "?"
  end
  format_obj arg
end

#format_obj(obj) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/pry-moves/formatter.rb', line 68

def format_obj(obj)
  if obj.is_a? String
    format_obj2 cut_string first_line obj
  else
    first_line format_obj2 obj
  end
end

#format_obj2(obj) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/pry-moves/formatter.rb', line 76

def format_obj2(obj)
  if @colorize
    PryMoves::Painter.colorize obj
  else
    i = obj.inspect
    i.start_with?('#<') ? obj.class.to_s : i
  end
end

#method_signature(binding) ⇒ Object



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
40
# File 'lib/pry-moves/formatter.rb', line 10

def method_signature(binding)
  meth = binding.eval('__method__')
  meth_obj = meth ? Pry::Method.from_binding(binding) : nil
  if !meth_obj
    ""
  elsif meth_obj.undefined?
    "#{meth_obj.name}(UNKNOWN) (undefined method)"
  else
    args = meth_obj.parameters.map.with_index do |(type, name), i|
      if name
        value = format_arg binding, name.to_s
        show_value = true
      else
        name = (type == :block ? 'block' : "arg#{i + 1}")
      end
      name = case type
        when :req   then "#{name} ="
        when :key   then "#{name}:"
        when :opt   then "#{name}=?"
        when :rest  then "*#{name}"
        when :block then "&#{name}"
        else '?'
      end
      show_value ? "#{name} #{value}" : name
    end
    if args.count > MAX_PARAMS
      args = args.first(MAX_PARAMS) + ["(#{args.count - MAX_PARAMS} more params)…"]
    end
    "#{meth_obj.name}(#{args.join(', ')})"
  end
end

#shorten_path(path) ⇒ Object



62
63
64
65
66
# File 'lib/pry-moves/formatter.rb', line 62

def shorten_path(path)
  path = path.gsub( /^#{PATH_TRASH}\//, '')
  PryMoves::Backtrace.trim_path ?
    File.basename(path, ".*") : path
end