rack-mount 0.5.2 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -28,7 +28,8 @@ module Rack::Mount
28
28
 
29
29
  def generate(methods, params = {}, recall = {}, options = {})
30
30
  if methods.is_a?(Array)
31
- result = methods.map { |m| generate_method(m, params, recall, options) || (return nil) }
31
+ result = methods.map { |m| generate_method(m, params, recall, options) }
32
+ return nil if result.all? { |e| e.nil? }
32
33
  else
33
34
  result = generate_method(methods, params, recall, options)
34
35
  end
@@ -20,28 +20,45 @@ module Rack::Mount
20
20
  route
21
21
  end
22
22
 
23
- # Generates path from identifiers or significant keys.
23
+ # Generates a url from Rack env and identifiers or significant keys.
24
24
  #
25
25
  # To generate a url by named route, pass the name in as a +Symbol+.
26
- # url(:dashboard) # => "/dashboard"
26
+ # url(env, :dashboard) # => "/dashboard"
27
27
  #
28
28
  # Additional parameters can be passed in as a hash
29
- # url(:people, :id => "1") # => "/people/1"
29
+ # url(env, :people, :id => "1") # => "/people/1"
30
30
  #
31
31
  # If no name route is given, it will fall back to a slower
32
32
  # generation search.
33
- # url(:controller => "people", :action => "show", :id => "1")
33
+ # url(env, :controller => "people", :action => "show", :id => "1")
34
34
  # # => "/people/1"
35
- def url(*args)
36
- named_route, params, recall, options = extract_params!(*args)
35
+ def url(env, *args)
36
+ named_route, params = nil, {}
37
+
38
+ case args.length
39
+ when 2
40
+ named_route, params = args[0], args[1].dup
41
+ when 1
42
+ if args[0].is_a?(Hash)
43
+ params = args[0].dup
44
+ else
45
+ named_route = args[0]
46
+ end
47
+ else
48
+ raise ArgumentError
49
+ end
37
50
 
38
- options[:parameterize] ||= lambda { |name, param| Utils.escape_uri(param) }
51
+ only_path = params.delete(:only_path)
52
+ recall = env[@parameters_key] || {}
39
53
 
40
- unless result = generate(:path_info, named_route, params, recall, options)
54
+ unless result = generate([:host, :path_info], named_route, params, recall,
55
+ :parameterize => lambda { |name, param| Utils.escape_uri(param) })
41
56
  return
42
57
  end
43
58
 
44
- uri, params = result
59
+ parts, params = result
60
+ return unless parts
61
+
45
62
  params.each do |k, v|
46
63
  if v
47
64
  params[k] = v
@@ -50,7 +67,10 @@ module Rack::Mount
50
67
  end
51
68
  end
52
69
 
53
- uri << "?#{Utils.build_nested_query(params)}" if uri && params.any?
70
+ req = @request_class.new(env)
71
+ uri = only_path ? '' : "#{req.scheme}://#{parts[0] || req.host}"
72
+ uri = "#{uri}#{req.script_name}#{parts[1]}"
73
+ uri << "?#{Utils.build_nested_query(params)}" if params.any?
54
74
  uri
55
75
  end
56
76
 
data/lib/rack/mount.rb CHANGED
@@ -1,7 +1,16 @@
1
1
  require 'rack'
2
2
 
3
3
  module Rack #:nodoc:
4
- module Mount #:nodoc:
4
+ # A stackable dynamic tree based Rack router.
5
+ #
6
+ # Rack::Mount supports Rack's Cascade style of trying several routes until
7
+ # it finds one that is not a 404. This allows multiple routes to be nested
8
+ # or stacked on top of each other. Since the application endpoint can
9
+ # trigger the router to continue matching, middleware can be used to add
10
+ # arbitrary conditions to any route. This allows you to route based on
11
+ # other request attributes, session information, or even data dynamically
12
+ # pulled from a database.
13
+ module Mount
5
14
  autoload :GeneratableRegexp, 'rack/mount/generatable_regexp'
6
15
  autoload :Mixover, 'rack/mount/mixover'
7
16
  autoload :Multimap, 'rack/mount/multimap'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-mount
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Peek
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-18 00:00:00 -06:00
12
+ date: 2010-02-19 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency