Module: ActionPolicy::PrettyPrint

Defined in:
lib/action_policy/utils/pretty_print.rb

Overview

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

Defined Under Namespace

Classes: Visitor

Constant Summary collapse

TRUE =
"\e[32mtrue\e[0m"
FALSE =
"\e[31mfalse\e[0m"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.ignore_expressionsObject

Returns the value of attribute ignore_expressions.



127
128
129
# File 'lib/action_policy/utils/pretty_print.rb', line 127

def ignore_expressions
  @ignore_expressions
end

Class Method Details

.colorize(val) ⇒ Object



143
144
145
146
147
148
# File 'lib/action_policy/utils/pretty_print.rb', line 143

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

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


130
# File 'lib/action_policy/utils/pretty_print.rb', line 130

def available?() = true


132
133
134
135
136
# File 'lib/action_policy/utils/pretty_print.rb', line 132

def print_method(object, method_name)
  ast = Prism.parse(object.method(method_name).source)

  Visitor.new(object).collect(ast)
end