Last Updated: February 25, 2016
·
666
· erebusbat

Patch to allow Awesome Print gem to work nicely with OpenStructs

I use the OpenStruct class alot as it makes for prettier code. This is annoying with a big object when debugging in pry or irb as awesome_print just outputs a single line. The following patches will trick awesome_print into thinking that an OpenStruct is just a struct (and add the method to OpenStruct to support that):

class OpenStruct
  # Allow awesome_print to work (with patch to AwesomePrint::Inspector defined below)
  if defined?(AwesomePrint)
    def each_pair &block
      @table.each_pair(&block)
    end
  end
end

## Patch inspector so it recognizes OpenStruct
if defined?(AwesomePrint::Inspector)
  module AwesomePrint
    class Inspector
      private
        alias_method :printable_original, :printable
        def printable(object)
          case object
          when OpenStruct  then :struct
          else printable_original(object)
          end
        end

    end
  end
end