Created
February 8, 2018 08:05
-
-
Save bootleq/22f6c45767920c67f7dfcb17ff42ab06 to your computer and use it in GitHub Desktop.
Change certain classes' `ap` (awesome_print) display
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
module AwesomePrint | |
# Make awesome_print aware of certain classes and format differently | |
module YourName | |
def self.included(base) | |
base.send :alias_method, :cast_without_your_name, :cast | |
base.send :alias_method, :cast, :cast_with_your_name | |
end | |
def cast_with_your_name(object, type) | |
cast = cast_without_your_name(object, type) | |
case object | |
when ::PG::Result | |
cast = :pg_result_instance | |
when ::Reform::Form | |
cast = :reform_form_instance | |
when ::Trailblazer::Operation | |
cast = :trailblazer_operation_instance | |
end | |
cast | |
end | |
private | |
def awesome_pg_result_instance(object) | |
return object.error_message if object.error_message.length > 0 | |
if @options[:table] && defined?(TablePrint) # NOTE: use custom non-standard option `table` | |
printer = TablePrint::Printer.new(object.map(&:to_hash)) | |
printer.table_print | |
else | |
awesome_array(object.map(&:to_hash)) | |
end | |
end | |
def awesome_reform_form_instance(object) | |
return awesome_object(object) if @options[:raw] | |
name = "#{awesome_simple(object.class.to_s, :class)}" | |
base = "< #{awesome_simple(object.class.superclass.to_s, :class)}" | |
[name, base, awesome_hash(object.to_nested_hash)].join(' ') | |
end | |
def awesome_trailblazer_operation_instance(object) | |
return awesome_object(object) if @options[:raw] | |
name = "#{awesome_simple(object.class.to_s, :class)}" | |
base = "< #{awesome_simple(object.class.superclass.to_s, :class)}" | |
[name, base].join(' ') | |
end | |
end | |
end | |
AwesomePrint::Formatter.send(:include, AwesomePrint::YourName) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment