Class: HttpRouter::Generator::PathGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/http_router/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(route, path, validation_regex = nil) ⇒ PathGenerator

Returns a new instance of PathGenerator.



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
# File 'lib/http_router/generator.rb', line 8

def initialize(route, path, validation_regex = nil)
  @route = route
  @path = path.dup
  @param_names = []
  if path.is_a?(String)
    path[0, 0] = '/' unless path[0] == ?/
    regex_parts = path.split(/([:\*][a-zA-Z0-9_]+)/)
    regex, code = '', ''
    dynamic = false
    regex_parts.each_with_index do |part, index|
      case part[0]
      when ?:, ?*
        if index != 0 && regex_parts[index - 1][-1] == ?\\
          regex << Regexp.quote(part) unless validation_regex
          code << part
          dynamic = true
        else
          regex << (@route.matches_with(part[1, part.size].to_sym) || '.*?').to_s unless validation_regex
          code << "\#{args.shift || (options && options.delete(:#{part[1, part.size]})) || return}"
          dynamic = true
        end
      else
        regex << Regexp.quote(part) unless validation_regex
        code << part
      end
    end
    validation_regex ||= Regexp.new("^#{regex}$") if dynamic
    if validation_regex
      instance_eval "      def generate(args, options)\n        generated_path = \\\"\#{code}\\\"\n        \#{validation_regex.inspect}.match(generated_path) ? URI.escape(generated_path) : nil\n      end\n      EOT\n    else\n      instance_eval <<-EOT, __FILE__, __LINE__ + 1\n      def generate(args, options)\n        URI.escape(\\\"\#{code}\\\")\n      end\n      EOT\n    end\n  end\nend\n", __FILE__, __LINE__ + 1

Instance Attribute Details

#param_namesObject

Returns the value of attribute param_names.



7
8
9
# File 'lib/http_router/generator.rb', line 7

def param_names
  @param_names
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/http_router/generator.rb', line 6

def path
  @path
end