class ActionPolicy::PrettyPrint::Visitor
Attributes
indent[RW]
lines[R]
object[R]
Public Class Methods
new(object)
click to toggle source
# File lib/action_policy/utils/pretty_print.rb, line 48 def initialize(object) @object = object end
Public Instance Methods
collect(ast)
click to toggle source
# File lib/action_policy/utils/pretty_print.rb, line 52 def collect(ast) @lines = [] @indent = 0 visit_node(ast) lines.join("\n") end
eval_exp(exp)
click to toggle source
# File lib/action_policy/utils/pretty_print.rb, line 74 def eval_exp(exp) return "<skipped>" if ignore_exp?(exp) object.instance_eval(exp) rescue => e "Failed: #{e.message}" end
expression_with_result(sexp)
click to toggle source
# File lib/action_policy/utils/pretty_print.rb, line 69 def expression_with_result(sexp) expression = Unparser.unparse(sexp) "#{expression} #=> #{PrettyPrint.colorize(eval_exp(expression))}" end
ignore_exp?(exp)
click to toggle source
Some lines should not be evaled
# File lib/action_policy/utils/pretty_print.rb, line 123 def ignore_exp?(exp) PrettyPrint.ignore_expressions.any? { exp.match?(_1) } end
indented(str)
click to toggle source
# File lib/action_policy/utils/pretty_print.rb, line 115 def indented(str) "#{indent.zero? ? "↳ " : ""}#{" " * indent}#{str}".tap do # increase indent after the first expression self.indent += 2 if indent.zero? end end
visit_and(ast)
click to toggle source
# File lib/action_policy/utils/pretty_print.rb, line 81 def visit_and(ast) visit_node(ast.children[0]) lines << indented("AND") visit_node(ast.children[1]) end
visit_begin(ast)
click to toggle source
# File lib/action_policy/utils/pretty_print.rb, line 93 def visit_begin(ast) # Parens if ast.children.size == 1 lines << indented("(") self.indent += 2 visit_node(ast.children[0]) self.indent -= 2 lines << indented(")") else # Multiple expressions ast.children.each do |node| visit_node(node) # restore indent after each expression self.indent -= 2 end end end
visit_missing(ast)
click to toggle source
# File lib/action_policy/utils/pretty_print.rb, line 111 def visit_missing(ast) lines << indented(expression_with_result(ast)) end
visit_node(ast)
click to toggle source
# File lib/action_policy/utils/pretty_print.rb, line 61 def visit_node(ast) if respond_to?("visit_#{ast.type}") send("visit_#{ast.type}", ast) else visit_missing ast end end
visit_or(ast)
click to toggle source
# File lib/action_policy/utils/pretty_print.rb, line 87 def visit_or(ast) visit_node(ast.children[0]) lines << indented("OR") visit_node(ast.children[1]) end