Class: Feet::RouteObject

Inherits:
Object show all
Defined in:
lib/feet/routing.rb

Instance Method Summary collapse

Constructor Details

#initializeRouteObject

Returns a new instance of RouteObject.



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/feet/routing.rb', line 3

def initialize
  @rules = []
  @default_rules = [
    {
      regexp: /^\/([a-zA-Z0-9]+)$/,
      vars: ['controller'],
      dest: nil,
      via: false,
      options: { default: { 'action' => 'index' } }
    }
  ]
end

Instance Method Details

#check_url(url, method) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/feet/routing.rb', line 82

def check_url(url, method)
  (@rules + @default_rules).each do |rule|
    next if rule[:via] && rule[:via] != method.downcase

    # Check if rule against regexp
    matched_data = rule[:regexp].match(url)

    if matched_data
      # Build params hash
      options = rule[:options]
      params = options[:default].dup

      # Match variable names with the regexp captured parts
      rule[:vars].each_with_index do |var, i|
        params[var] = matched_data.captures[i]
      end

      if rule[:dest]
        # There's either a destination like 'controller#action' or a Proc
        return get_dest(rule[:dest], params)
      else
        # The params are specified in the options[:default]
        # Use controller#action to get the Rack application
        controller = params['controller']
        action = params['action']
        return get_dest("#{controller}##{action}", params)
      end
    end
  end
  nil
end

#get_dest(dest, routing_params = {}) ⇒ Object

Returns a Rack application or raises an error if no/invalid ‘dest’



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/feet/routing.rb', line 115

def get_dest(dest, routing_params = {})
  return dest if dest.respond_to?(:call)

  # Ex 'controller#action'
  if dest =~ /^([^#]+)#([^#]+)$/
    name = $1.capitalize
    controller = Object.const_get("#{name}Controller")
    return controller.action($2, routing_params)
  end

  raise "No destination: #{dest.inspect}!"
end

#match(url, *args) ⇒ Object

Example arguments url “:controller/:id” args [:default=>{“action”=>“show”}]



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
# File 'lib/feet/routing.rb', line 34

def match(url, *args)
  # Capture the options hash
  options = {}
  options = args.pop if args[-1].is_a? Hash
  # Check for default option
  options[:default] ||= {}

  # Get destination and limit the # of arguments
  dest = nil
  dest = args.pop if args.size > 0
  raise 'Too many arguments!' if args.size > 0

  # Parse URL parts. Split on appropriate punctuation
  # (slash, parens, question mark, dot)
  parts = url.split /(\/|\(|\)|\?|\.)/
  parts.reject! { |p| p.empty? }

  vars = []
  regexp_parts = parts.map do |part|
    case part[0]
    when ':'
      # Map Variable
      vars << part[1..-1]
      '([a-zA-Z0-9]+)?'
    when '*'
      # Map Wildcard
      vars << part[1..-1]
      '(.*)'
    when '.'
      '\\.' # Literal dot
    else
      part
    end
  end

  # Join the main regexp
  regexp = regexp_parts.join('')

  # Store match object
  @rules.push({
                regexp: Regexp.new("^/#{regexp}$"),
                vars: vars,
                dest: dest,
                via: options[:via] ? options[:via].downcase : false,
                options: options
              })
end

#resource(name) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/feet/routing.rb', line 20

def resource(name)
  match "/#{name}", default: { 'action' => 'index' }, via: 'GET'
  match "/#{name}/new", default: { 'action' => 'new' }, via: 'GET'
  match "/#{name}", default: { 'action' => 'create' }, via: 'POST'
  match "/#{name}/:id", default: { 'action' => 'show' }, via: 'GET'
  match "/#{name}/:id/edit", default: { 'action' => 'edit' }, via: 'GET'
  match "/#{name}/:id", default: { 'action' => 'update' }, via: 'PUT'
  match "/#{name}/:id", default: { 'action' => 'update' }, via: 'PATCH'
  match "/#{name}/:id", default: { 'action' => 'destroy' }, via: 'DELETE'
end

#root(*args) ⇒ Object



16
17
18
# File 'lib/feet/routing.rb', line 16

def root(*args)
  match('', *args)
end