lib/dr/base/utils.rb in drain-0.3.0 vs lib/dr/base/utils.rb in drain-0.4
- old
+ new
@@ -1,25 +1,34 @@
module DR
module Utils
extend self
- def pretty_print(string, format: nil, pretty: false)
+ def pretty_print(string, format: nil, pretty: nil)
case format.to_s
when "json"
require 'json'
return pretty_print(string.to_json, pretty: pretty)
when "yaml"
require "yaml"
return pretty_print(string.to_yaml, pretty: pretty)
end
- if pretty.to_s=="color"
+ pretty = "color" if pretty == nil or pretty == true #default
+ case pretty.to_s
+ when "ap"
begin
require 'ap'
ap string
rescue LoadError,NameError
- pretty_print(string,pretty:true)
+ pretty_print(string,pretty: :pp)
end
- elsif pretty
+ when "color", "pp_color"
+ begin
+ require 'pry'
+ Pry::ColorPrinter.pp string
+ rescue LoadError,NameError
+ pretty_print(string,pretty: :pp)
+ end
+ when "pp"
require 'pp'
pp string
else
puts string
end
@@ -45,8 +54,26 @@
components+=[nil]*[(num-components.length), 0].max
a=components[0..(components.length-num)]
b=components[(components.length-num+1)..(components.length-1)]
return [a.join(sep), *b]
end
+ end
+ end
+
+ # Include this to get less output from pp
+ module PPHelper
+ # less verbose for pretty_print
+ def pretty_print(pp)
+ info = respond_to?(:to_pp) ? to_pp : to_s
+ pp.object_address_group(self) { pp.text " "+info }
+ end
+
+ #since we hide the pp value of self, allow to inspect it
+ def export
+ r={}
+ instance_variables.sort.each do |var|
+ r[var]=instance_variable_get(var)
+ end
+ r
end
end
end