File: route.rb

package info (click to toggle)
ruby-journey 1.0.4-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 288 kB
  • sloc: ruby: 2,830; javascript: 113; yacc: 42; makefile: 2
file content (95 lines) | stat: -rw-r--r-- 2,470 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
module Journey
  class Route
    attr_reader :app, :path, :verb, :defaults, :ip, :name

    attr_reader :constraints
    alias :conditions :constraints

    attr_accessor :precedence

    ##
    # +path+ is a path constraint.
    # +constraints+ is a hash of constraints to be applied to this route.
    def initialize name, app, path, constraints, defaults = {}
      constraints  = constraints.dup
      @name        = name
      @app         = app
      @path        = path
      @verb        = constraints[:request_method] || //
      @ip          = constraints.delete(:ip) || //

      @constraints = constraints
      @constraints.keep_if { |_,v| Regexp === v || String === v }
      @defaults    = defaults
      @required_defaults = nil
      @required_parts    = nil
      @parts             = nil
      @decorated_ast     = nil
      @precedence        = 0
    end

    def ast
      return @decorated_ast if @decorated_ast

      @decorated_ast = path.ast
      @decorated_ast.grep(Nodes::Terminal).each { |n| n.memo = self }
      @decorated_ast
    end

    def requirements # :nodoc:
      # needed for rails `rake routes`
      path.requirements.merge(@defaults).delete_if { |_,v|
        /.+?/ == v
      }
    end

    def segments
      @path.names
    end

    def required_keys
      path.required_names.map { |x| x.to_sym } + required_defaults.keys
    end

    def score constraints
      required_keys = path.required_names
      supplied_keys = constraints.map { |k,v| v && k.to_s }.compact

      return -1 unless (required_keys - supplied_keys).empty?

      score = (supplied_keys & path.names).length
      score + (required_defaults.length * 2)
    end

    def parts
      @parts ||= segments.map { |n| n.to_sym }
    end
    alias :segment_keys :parts

    def format path_options
      (defaults.keys - required_parts).each do |key|
        path_options.delete key if defaults[key].to_s == path_options[key].to_s
      end

      formatter      = Visitors::Formatter.new(path_options)

      formatted_path = formatter.accept(path.spec)
      formatted_path.gsub(/\/\x00/, '')
    end

    def optional_parts
      path.optional_names.map { |n| n.to_sym }
    end

    def required_parts
      @required_parts ||= path.required_names.map { |n| n.to_sym }
    end

    def required_defaults
      @required_defaults ||= begin
        matches = parts
        @defaults.dup.delete_if { |k,_| matches.include? k }
      end
    end
  end
end