module ActionPolicy::PrettyPrint

Takes the object and a method name, and returns the “annotated” source code for the method: code is split into parts by logical operators and each part is evaluated separately.

Example:

class MyClass
  def access?
    admin? && access_feed?
  end
end

puts PrettyPrint.format_method(MyClass.new, :access?)

#=> MyClass#access?
#=> ↳ admin? #=> false
#=> AND
#=> access_feed? #=> true

Constants

FALSE
TRUE

Attributes

ignore_expressions[RW]

Public Class Methods

available?() click to toggle source
# File lib/action_policy/utils/pretty_print.rb, line 132
      def available?() = true

      def print_method(object, method_name)
        ast = object.method(method_name).source.then(&Unparser.:parse)
        # outer node is a method definition itself
        body = ast.children[2]

        Visitor.new(object).collect(body)
      end
    else
      def available?() = false

      def print_method(_, _) = ""
    end

    def colorize(val)
      return val unless $stdout.isatty
      return TRUE if val.eql?(true) # rubocop:disable Lint/DeprecatedConstants
      return FALSE if val.eql?(false) # rubocop:disable Lint/DeprecatedConstants
      val
    end
  end

  self.ignore_expressions = [
    /^\s*binding\.(pry|irb)\s*$/s
  ]
end
colorize(val) click to toggle source
# File lib/action_policy/utils/pretty_print.rb, line 147
def colorize(val)
  return val unless $stdout.isatty
  return TRUE if val.eql?(true) # rubocop:disable Lint/DeprecatedConstants
  return FALSE if val.eql?(false) # rubocop:disable Lint/DeprecatedConstants
  val
end
print_method(object, method_name) click to toggle source