class WTF::Dumper
Constants
- FORMAT_OPTIONS
- MODIFY_OPTIONS
- OPTIONS
- OUTPUT_OPTIONS
- PREFIX_OPTIONS
Attributes
args[R]
options[R]
Public Class Methods
new(*args)
click to toggle source
# File lib/wtf/dumper.rb, line 12 def initialize(*args) @args = args @options = {} while is_option?(args.last) @options[args.pop] = true end end
Public Instance Methods
call()
click to toggle source
# File lib/wtf/dumper.rb, line 20 def call where, names = parse_source(caller[1]) data = prefix(where) << format(names) output(data) end
Private Instance Methods
cleanup(str, bare = false)
click to toggle source
# File lib/wtf/dumper.rb, line 94 def cleanup(str, bare = false) # remove array parentheses str.gsub!(/^\[|\]$/,'') # ActiveRecord no attributes str.gsub!(/#<[A-Z]\w+ id: \d+\K.*?>/, '>') if bare str end
format(names)
click to toggle source
# File lib/wtf/dumper.rb, line 65 def format(names) return format_with_names(names) if names case when options[:pp] cleanup(args.pretty_inspect, options[:bare]) when options[:yaml] YAML.dump(args) when options[:json] JSON::pretty_generate(args) when options[:text] args.map(&:to_s).join("\n") when options[:line] args.map(&:inspect).join("\n ") when options[:csv] args.first.map(&:to_csv).join else cleanup(args.inspect, options[:bare]) end end
format_with_names(names)
click to toggle source
# File lib/wtf/dumper.rb, line 86 def format_with_names(names) len = names.map(&:length).max args.each_with_object('').with_index do |(arg, data), i| name = names[i] || '?' data << "\n" << " %-#{len}s => %s" % [name, arg.inspect] end end
is_option?(sym)
click to toggle source
# File lib/wtf/dumper.rb, line 28 def is_option?(sym) OPTIONS.include?(sym) or WTF.output_options.key?(sym) end
output(data)
click to toggle source
# File lib/wtf/dumper.rb, line 102 def output(data) selected = (OUTPUT_OPTIONS + WTF.output_options.keys).select { |how| options[how] } selected << :default if selected.empty? proxy = Output.new(data) selected.each do |how| proxy.call(how) end end
parse_names(source)
click to toggle source
# File lib/wtf/dumper.rb, line 56 def parse_names(source) return nil unless source md = source.match(/(\.wtf\(|WTF\?\(?)(.*?)\s*(?:if |unless |$)/) return nil unless md names = md[2].split(',').map(&:strip) # naive names.unshift('self') if md[1] == '.wtf(' names end
parse_source(item)
click to toggle source
# File lib/wtf/dumper.rb, line 45 def parse_source(item) md = item.match(%r{^(.*?([^/]+?)(?:\.rb)?):(\d+):in `(.*)'$}) where = '%s/%s:%s' % md.values_at(2, 4, 3) names = parse_names(read_source(md[1], md[3].to_i)) if options[:name] [where, names] end
prefix(where)
click to toggle source
# File lib/wtf/dumper.rb, line 32 def prefix(where) data = '' data << "\n" if options[:nl] data << "[%s] " % Time.now if options[:time] return data if options[:np] data << if args.first.is_a?(Symbol) args.shift.to_s.upcase else "WTF (#{where})" end data << ': ' end
read_source(file, line)
click to toggle source
# File lib/wtf/dumper.rb, line 52 def read_source(file, line) File.open(file).each_line.lazy.take(line).to_a[line-1] end