Class: PryMoves::Formatter
- Inherits:
-
Object
- Object
- PryMoves::Formatter
- Defined in:
- lib/pry-moves/formatter.rb
Constant Summary collapse
- MAX_PARAMS =
5
Instance Attribute Summary collapse
-
#colorize ⇒ Object
Returns the value of attribute colorize.
Instance Method Summary collapse
- #cut_string(str) ⇒ Object
- #first_line(str) ⇒ Object
- #format_arg(binding, arg_name) ⇒ Object
- #format_obj(obj) ⇒ Object
- #format_obj2(obj) ⇒ Object
-
#initialize(colorize = true) ⇒ Formatter
constructor
A new instance of Formatter.
- #method_signature(binding) ⇒ Object
- #path_trash ⇒ Object
- #shorten_path(path) ⇒ Object
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
#colorize ⇒ Object
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
70 71 72 73 74 75 76 |
# File 'lib/pry-moves/formatter.rb', line 70 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
78 79 80 81 82 83 84 85 |
# File 'lib/pry-moves/formatter.rb', line 78 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 |
#path_trash ⇒ Object
60 61 62 |
# File 'lib/pry-moves/formatter.rb', line 60 def path_trash @path_trash ||= defined?(Rails) ? Rails.root.to_s : Dir.pwd end |