Class: Rails::RFC6570::Visitor
- Inherits:
-
ActionDispatch::Journey::Visitors::Visitor
- Object
- ActionDispatch::Journey::Visitors::Visitor
- Rails::RFC6570::Visitor
- Defined in:
- lib/rails/rfc6570/visitor.rb
Constant Summary collapse
- DISPATCH_CACHE =
rubocop:disable Style/MutableConstant
{}
Instance Method Summary collapse
- #accept(node) ⇒ Object
-
#initialize(factory: nil) ⇒ Visitor
constructor
A new instance of Visitor.
- #terminal(node) ⇒ Object
- #visit(node) ⇒ Object
-
#visit_CAT(node) ⇒ Object
rubocop:disable Naming/MethodName.
- #visit_DOT(node) ⇒ Object
- #visit_DOT_SYMBOL(dot, node) ⇒ Object
- #visit_GROUP(node) ⇒ Object
- #visit_LITERAL(node) ⇒ Object
- #visit_OR(_node) ⇒ Object
- #visit_SLASH(node) ⇒ Object
- #visit_SLASH_CAT(slash, cat) ⇒ Object
- #visit_SLASH_STAR(_slash, star) ⇒ Object
- #visit_SLASH_SYMBOL(slash, node) ⇒ Object
- #visit_STAR_CAT(star, cat) ⇒ Object
- #visit_SYMBOL(node) ⇒ Object
Constructor Details
#initialize(factory: nil) ⇒ Visitor
Returns a new instance of Visitor.
8 9 10 11 12 13 |
# File 'lib/rails/rfc6570/visitor.rb', line 8 def initialize(factory: nil) super() @stack = [] @factory = factory || method(:symbolize) end |
Instance Method Details
#accept(node) ⇒ Object
15 16 17 |
# File 'lib/rails/rfc6570/visitor.rb', line 15 def accept(node) Array(visit(node)).flatten end |
#terminal(node) ⇒ Object
26 27 28 |
# File 'lib/rails/rfc6570/visitor.rb', line 26 def terminal(node) node.left end |
#visit(node) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/rails/rfc6570/visitor.rb', line 19 def visit(node) @stack.unshift node.type send DISPATCH_CACHE.fetch(node.type), node ensure @stack.shift end |
#visit_CAT(node) ⇒ Object
rubocop:disable Naming/MethodName
31 32 33 34 35 36 37 |
# File 'lib/rails/rfc6570/visitor.rb', line 31 def visit_CAT(node) if (mth = DISPATCH_CACHE[:"#{node.left.type}_#{node.right.type}"]) send mth, node.left, node.right else [visit(node.left), visit(node.right)] end end |
#visit_DOT(node) ⇒ Object
47 48 49 |
# File 'lib/rails/rfc6570/visitor.rb', line 47 def visit_DOT(node) terminal(node) end |
#visit_DOT_SYMBOL(dot, node) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/rails/rfc6570/visitor.rb', line 67 def visit_DOT_SYMBOL(dot, node) if @stack[0..1] == %i[CAT GROUP] symbol(node, prefix: '.') else [visit(dot), visit(node)] end end |
#visit_GROUP(node) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/rails/rfc6570/visitor.rb', line 59 def visit_GROUP(node) # if @stack.include?(:GROUP) && @stack[1..-1].include?(:GROUP) # raise 'Cannot transform nested groups.' # end visit node.left end |
#visit_LITERAL(node) ⇒ Object
39 40 41 |
# File 'lib/rails/rfc6570/visitor.rb', line 39 def visit_LITERAL(node) terminal(node) end |
#visit_OR(_node) ⇒ Object
55 56 57 |
# File 'lib/rails/rfc6570/visitor.rb', line 55 def visit_OR(_node) raise 'OR nodes cannot be serialized to URI templates' end |
#visit_SLASH(node) ⇒ Object
43 44 45 |
# File 'lib/rails/rfc6570/visitor.rb', line 43 def visit_SLASH(node) terminal(node) end |
#visit_SLASH_CAT(slash, cat) ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/rails/rfc6570/visitor.rb', line 83 def visit_SLASH_CAT(slash, cat) if cat.left.type == :STAR [symbol(cat.left.left, prefix: '/', suffix: '*'), visit(cat.right)] elsif cat.left.type == :SYMBOL && @stack[0..1] == %i[CAT GROUP] [symbol(cat.left, prefix: '/'), visit(cat.right)] else [visit(slash), visit(cat)] end end |
#visit_SLASH_STAR(_slash, star) ⇒ Object
93 94 95 |
# File 'lib/rails/rfc6570/visitor.rb', line 93 def visit_SLASH_STAR(_slash, star) symbol(star.left, prefix: '/', suffix: '*') end |
#visit_SLASH_SYMBOL(slash, node) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/rails/rfc6570/visitor.rb', line 75 def visit_SLASH_SYMBOL(slash, node) if @stack[0..1] == %i[CAT GROUP] symbol(node, prefix: '/') else [visit(slash), visit(node)] end end |
#visit_STAR_CAT(star, cat) ⇒ Object
97 98 99 |
# File 'lib/rails/rfc6570/visitor.rb', line 97 def visit_STAR_CAT(star, cat) [symbol(star.left, prefix: '/', suffix: '*'), visit(cat)] end |
#visit_SYMBOL(node) ⇒ Object
51 52 53 |
# File 'lib/rails/rfc6570/visitor.rb', line 51 def visit_SYMBOL(node) symbol(node) end |