Created
April 13, 2015 00:33
-
-
Save estum/19c257f9e3c8a432ffef to your computer and use it in GitHub Desktop.
AwesomePrint::Proc: inspecting Proc instances with source code (using Sourcify)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# = Awesome Print Proc Formatter | |
# | |
# Prints source when inspecting Proc object. | |
# Requires <tt>sourcify (>= 0.6.0.rc4)</tt> gem. | |
begin | |
require "sourcify" | |
rescue LoadError | |
end | |
module AwesomePrint | |
module Proc | |
def self.included(base) | |
base.send :alias_method, :cast_without_proc, :cast | |
base.send :alias_method, :cast, :cast_with_proc | |
end | |
# Add Proc class to the dispatcher pipeline. | |
def cast_with_proc(object, type) | |
if (type == :proc || object.is_a?(::Proc)) && object.respond_to?(:to_source) | |
:proc | |
else | |
cast_without_proc(object, type) | |
end | |
end | |
private | |
# Format Proc object. | |
def awesome_proc(object) | |
if !@options[:raw] && (/\A(?<kw>proc)\s*(?<block_source>.+?)\z/ =~ object.to_source) | |
kw = "->" if object.lambda? | |
sprintf("%s %s", colorize(kw, :keyword), colorize(block_source, :string)) | |
else | |
awesome_object(object) | |
end | |
end | |
end | |
end | |
if defined?(Sourcify) && Sourcify::VERSION >= "0.6.0.rc4" | |
AwesomePrint::Formatter.include(AwesomePrint::Proc) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment