Class: ActionPolicy::PrettyPrint::Visitor
- Inherits:
-
Object
- Object
- ActionPolicy::PrettyPrint::Visitor
- Defined in:
- lib/action_policy/utils/pretty_print.rb
Instance Attribute Summary collapse
-
#indent ⇒ Object
Returns the value of attribute indent.
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #collect(ast) ⇒ Object
- #eval_exp(exp) ⇒ Object
- #expression_with_result(sexp) ⇒ Object
-
#ignore_exp?(exp) ⇒ Boolean
Some lines should not be evaled.
- #indented(str) ⇒ Object
-
#initialize(object) ⇒ Visitor
constructor
A new instance of Visitor.
- #visit_and_node(ast) ⇒ Object
- #visit_missing(ast) ⇒ Object
- #visit_node(ast) ⇒ Object
- #visit_or_node(ast) ⇒ Object
- #visit_parentheses_node(ast) ⇒ Object
- #visit_statements_node(ast) ⇒ Object
Constructor Details
#initialize(object) ⇒ Visitor
Returns a new instance of Visitor.
47 48 49 |
# File 'lib/action_policy/utils/pretty_print.rb', line 47 def initialize(object) @object = object end |
Instance Attribute Details
#indent ⇒ Object
Returns the value of attribute indent.
45 46 47 |
# File 'lib/action_policy/utils/pretty_print.rb', line 45 def indent @indent end |
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
44 45 46 |
# File 'lib/action_policy/utils/pretty_print.rb', line 44 def lines @lines end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
44 45 46 |
# File 'lib/action_policy/utils/pretty_print.rb', line 44 def object @object end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
44 45 46 |
# File 'lib/action_policy/utils/pretty_print.rb', line 44 def source @source end |
Instance Method Details
#collect(ast) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/action_policy/utils/pretty_print.rb', line 51 def collect(ast) @lines = [] @indent = 0 @source = ast.source.source ast = ast.value.child_nodes[0].child_nodes[0].body visit_node(ast) lines.join("\n") end |
#eval_exp(exp) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/action_policy/utils/pretty_print.rb', line 75 def eval_exp(exp) return "<skipped>" if ignore_exp?(exp) object.instance_eval(exp) rescue => e "Failed: #{e.}" end |
#expression_with_result(sexp) ⇒ Object
70 71 72 73 |
# File 'lib/action_policy/utils/pretty_print.rb', line 70 def expression_with_result(sexp) expression = source[sexp.location.start_offset...sexp.location.end_offset] "#{expression} #=> #{PrettyPrint.colorize(eval_exp(expression))}" end |
#ignore_exp?(exp) ⇒ Boolean
Some lines should not be evaled
121 122 123 |
# File 'lib/action_policy/utils/pretty_print.rb', line 121 def ignore_exp?(exp) PrettyPrint.ignore_expressions.any? { exp.match?(_1) } end |
#indented(str) ⇒ Object
113 114 115 116 117 118 |
# File 'lib/action_policy/utils/pretty_print.rb', line 113 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_node(ast) ⇒ Object
82 83 84 85 86 |
# File 'lib/action_policy/utils/pretty_print.rb', line 82 def visit_and_node(ast) visit_node(ast.left) lines << indented("AND") visit_node(ast.right) end |
#visit_missing(ast) ⇒ Object
109 110 111 |
# File 'lib/action_policy/utils/pretty_print.rb', line 109 def visit_missing(ast) lines << indented(expression_with_result(ast)) end |
#visit_node(ast) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/action_policy/utils/pretty_print.rb', line 62 def visit_node(ast) if respond_to?("visit_#{ast.type}") send("visit_#{ast.type}", ast) else visit_missing ast end end |
#visit_or_node(ast) ⇒ Object
88 89 90 91 92 |
# File 'lib/action_policy/utils/pretty_print.rb', line 88 def visit_or_node(ast) visit_node(ast.left) lines << indented("OR") visit_node(ast.right) end |
#visit_parentheses_node(ast) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/action_policy/utils/pretty_print.rb', line 102 def visit_parentheses_node(ast) lines << indented("(") self.indent += 2 visit_node(ast.child_nodes[0]) lines << indented(")") end |
#visit_statements_node(ast) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/action_policy/utils/pretty_print.rb', line 94 def visit_statements_node(ast) ast.child_nodes.each do |node| visit_node(node) # restore indent after each expression self.indent -= 2 end end |