Class: HttpRouter::Route

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, path) ⇒ Route

Returns a new instance of Route.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/http_router/route.rb', line 6

def initialize(base, path)
  @router = base
  @path = path
  @original_path = path.dup
  @partially_match = extract_partial_match(path)
  @trailing_slash_ignore = extract_trailing_slash(path)
  @variable_store = {}
  @matches_with = {}
  @additional_matchers = {}
  @conditions =  {}
  @default_values = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/http_router/route.rb', line 28

def method_missing(method, *args, &block)
  if RequestNode::RequestMethods.include?(method)
    condition(method => args)
  else
    super
  end
end

Instance Attribute Details

#default_valuesObject

Returns the value of attribute default_values.



4
5
6
# File 'lib/http_router/route.rb', line 4

def default_values
  @default_values
end

#destObject (readonly)

Returns the value of attribute dest.



3
4
5
# File 'lib/http_router/route.rb', line 3

def dest
  @dest
end

#partially_matchObject

Returns the value of attribute partially_match.



4
5
6
# File 'lib/http_router/route.rb', line 4

def partially_match
  @partially_match
end

#pathsObject (readonly)

Returns the value of attribute paths.



3
4
5
# File 'lib/http_router/route.rb', line 3

def paths
  @paths
end

#trailing_slash_ignoreObject

Returns the value of attribute trailing_slash_ignore.



4
5
6
# File 'lib/http_router/route.rb', line 4

def trailing_slash_ignore
  @trailing_slash_ignore
end

Instance Method Details

#compileObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/http_router/route.rb', line 130

def compile
  unless @paths
    @paths = compile_paths
    @paths.each_with_index do |p1, i|
      @paths[i+1, @paths.size].each do |p2|
        raise AmbiguousRouteException.new if p1 === p2
      end
    end
    @paths.each do |path|
      path.route = self
      current_node = router.root.add_path(path)
      working_set = current_node.add_request_methods(@conditions)
      working_set.each do |current_node|
        current_node.value = path
      end
    end
  end
  self
end

#compiled?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/http_router/route.rb', line 126

def compiled?
  !@paths.nil?
end

#condition(conditions) ⇒ Object Also known as: conditions



83
84
85
86
87
88
89
90
91
# File 'lib/http_router/route.rb', line 83

def condition(conditions)
  guard_compiled
  conditions.each do |k,v|
    @conditions.key?(k) ?
      @conditions[k] << v :
      @conditions[k] = Array(v)
  end
  self
end

#default(v) ⇒ Object



54
55
56
57
# File 'lib/http_router/route.rb', line 54

def default(v)
  @default_values.merge!(v)
  self
end

#deleteObject



75
76
77
# File 'lib/http_router/route.rb', line 75

def delete
  request_method('DELETE')
end

#getObject



59
60
61
# File 'lib/http_router/route.rb', line 59

def get
  request_method('GET', 'HEAD')
end

#headObject



67
68
69
# File 'lib/http_router/route.rb', line 67

def head
  request_method('HEAD')
end

#matching(match) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/http_router/route.rb', line 94

def matching(match)
  guard_compiled
  match.each do |var_name, matchers|
    matchers = Array(matchers)
    matchers.each do |m|
      if m.respond_to?(:call)
        (@additional_matchers[var_name] ||= []) << m
      else
        @matches_with.key?(var_name) ?
          raise :
          @matches_with[var_name] = m
      end    
    end
  end
  self
end

#name(name) ⇒ Object



49
50
51
52
# File 'lib/http_router/route.rb', line 49

def name(name)
  @name = name
  router.named_routes[name] = self
end

#namedObject



111
112
113
# File 'lib/http_router/route.rb', line 111

def named
  @name
end

#only_getObject



79
80
81
# File 'lib/http_router/route.rb', line 79

def only_get
  request_method('DELETE')
end

#partial(match = true) ⇒ Object



121
122
123
124
# File 'lib/http_router/route.rb', line 121

def partial(match = true)
  @partially_match = match
  self
end

#partially_match?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/http_router/route.rb', line 177

def partially_match?
  @partially_match
end

#postObject



63
64
65
# File 'lib/http_router/route.rb', line 63

def post
  request_method('POST')
end

#putObject



71
72
73
# File 'lib/http_router/route.rb', line 71

def put
  request_method('PUT')
end

#redirect(path, status = 302) ⇒ Object

Raises:

  • (ArgumentError)


150
151
152
153
154
155
156
157
158
159
160
# File 'lib/http_router/route.rb', line 150

def redirect(path, status = 302)
  guard_compiled
  raise(ArgumentError, "Status has to be an integer between 300 and 399") unless (300..399).include?(status)
  to { |env|
    params = env['router.params']
    response = ::Rack::Response.new
    response.redirect(eval(%|"#{path}"|), status)
    response.finish
  }
  self
end

#significant_variable_namesObject



19
20
21
22
23
24
25
26
# File 'lib/http_router/route.rb', line 19

def significant_variable_names
  unless @significant_variable_names
    @significant_variable_names = @paths.map { |p| p.variable_names }
    @significant_variable_names.flatten!
    @significant_variable_names.uniq!
  end
  @significant_variable_names
end

#static(root) ⇒ Object



162
163
164
165
166
167
168
169
170
171
# File 'lib/http_router/route.rb', line 162

def static(root)
  guard_compiled
  raise AlreadyCompiledException.new if compiled?
  if File.directory?(root)
    partial.to ::Rack::File.new(root)
  else
    to proc{|env| env['PATH_INFO'] = File.basename(root); ::Rack::File.new(File.dirname(root)).call(env)}
  end
  self
end

#to(dest = nil, &block) ⇒ Object



115
116
117
118
119
# File 'lib/http_router/route.rb', line 115

def to(dest = nil, &block)
  compile
  @dest = dest || block
  self
end

#trailing_slash_ignore?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/http_router/route.rb', line 173

def trailing_slash_ignore?
  @trailing_slash_ignore
end

#url(*args) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/http_router/route.rb', line 181

def url(*args)
  options = args.last.is_a?(Hash) ? args.pop : nil
  options ||= {} if default_values
  options = default_values.merge(options) if default_values && options
  path = if args.empty?
    matching_path(options)
  else
    matching_path(args, options)
  end
  raise UngeneratableRouteException.new unless path
  path.url(args, options)
end

#with_options(options) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/http_router/route.rb', line 36

def with_options(options)
  if options && options[:matching]
    default(options[:matching])
  end
  if options && options[:conditions]
    condition(options[:conditions])
  end
  if options && options[:default_values]
    default(options[:default_values])
  end
  self
end