sinatra-mapping 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,5 +1,9 @@
1
1
  = Changes
2
2
 
3
+ [1.0.4 - July 2009]
4
+ * Error in root path without application/script name has been fixed.
5
+ * Helper for mapping has been registered automatically.
6
+
3
7
  [1.0.3 - July 2009]
4
8
  * Error in root path without script name in environment have been fixed.
5
9
 
data/VERSION CHANGED
@@ -4,4 +4,4 @@
4
4
  :cycle: Release
5
5
  :major: 1
6
6
  :minor: 0
7
- :patch: 3
7
+ :patch: 4
@@ -23,10 +23,9 @@ module Sinatra
23
23
  # map :root, "tasks" # => /tasks/
24
24
  # map :changes, "last-changes # => /tasks/last-changes
25
25
  def map(name, path = nil)
26
- @env ||= {}
27
26
  @locations ||= {}
28
27
  if name.to_sym == :root
29
- @locations[:root] = cleanup_paths("/#{@env['SCRIPT_NAME']}/#{path}/")
28
+ @locations[:root] = cleanup_paths("/#{path}/")
30
29
  metadef "#{name}_path" do |*paths|
31
30
  @locations[:root]
32
31
  end
@@ -64,10 +63,10 @@ module Sinatra
64
63
  end
65
64
 
66
65
  # Returns URL path with query instructions.
67
- def query_path_to(*args)
66
+ def build_path_to(script_name = nil, *args)
68
67
  args.compact!
69
68
  query = args.pop if args.last.kind_of?(Hash)
70
- path = map_path_to(*args)
69
+ path = map_path_to(script_name, *args)
71
70
  path << "?" << Rack::Utils::build_query(query) if query
72
71
  path
73
72
  end
@@ -77,16 +76,22 @@ module Sinatra
77
76
  # Check arguments. If argument is a symbol and exist map path before
78
77
  # setted, then return path mapped by symbol name.
79
78
  def map_path_to(*args)
80
- path_mapped(*locations_get_from(*args))
79
+ script_name = args.shift if args.first.to_s =~ /\/\w.*/
80
+ path_mapped(script_name, *locations_get_from(*args))
81
81
  end
82
82
 
83
83
  # Returns all paths mapped by root path in prefix.
84
- def path_mapped(*args)
85
- !args.empty? ? cleanup_paths("/#{@locations[:root]}/#{args.join('/')}") : @locations[:root]
84
+ def path_mapped(script_name, *args)
85
+ if !args.empty?
86
+ cleanup_paths("/#{script_name}/#{@locations[:root]}/#{args.join('/')}")
87
+ else
88
+ cleanup_paths("/#{script_name}/#{@locations[:root]}")
89
+ end
86
90
  end
87
91
 
88
92
  # Get paths from location maps.
89
93
  def locations_get_from(*args)
94
+ args.delete(:root)
90
95
  args.collect do |path|
91
96
  @locations.has_key?(path) ? @locations[path] : path
92
97
  end
@@ -94,6 +99,7 @@ module Sinatra
94
99
 
95
100
  # Clean all duplicated slashes.
96
101
  def cleanup_paths(*paths)
102
+ #.gsub(%r{#{@locations[:root]}/#{@locations[:root]}}, @locations[:root])
97
103
  paths.join('/').gsub(/[\/]{2,}/,'/')
98
104
  end
99
105
 
@@ -32,8 +32,12 @@ module Sinatra
32
32
  # # => <a href="/tasks/status/finished">All finished</a>
33
33
  def link_to(name = nil, *args)
34
34
  options = args.last.kind_of?(Hash) ? args.pop : {}
35
- url = args.shift if args.first.to_s =~ /^\w.*:\/\//
36
- "<a href=\"#{url || path_to(*args)}\" #{extract_tags_attributes options}>#{name}</a>"
35
+ url = args.shift if args.first.to_s =~ /^\w.*?:/
36
+ "<a href=\"#{url || path_to(*args)}\" #{extract_tags_attributes(options)}>#{name || url}</a>"
37
+ end
38
+
39
+ def link_to_query(name, action, query = {}, options = {})
40
+ "<a href=\"#{path_to(action,query)}\" #{extract_tags_attributes(options)}>#{name}</a>"
37
41
  end
38
42
 
39
43
  # Returns all paths with query parameters. Example:
@@ -45,7 +49,7 @@ module Sinatra
45
49
  # path_to :tags, "ruby", :posts
46
50
  # # returns "/labels/ruby/articles"
47
51
  def path_to(*args)
48
- options.query_path_to(*args)
52
+ options.build_path_to(env['SCRIPT_NAME'], *args)
49
53
  end
50
54
 
51
55
  private
@@ -67,7 +71,9 @@ module Sinatra
67
71
  end
68
72
  end
69
73
 
70
- end # module MapHelpers
74
+ end # module MappingHelpers
75
+
76
+ helpers MappingHelpers
71
77
 
72
78
  end # module Sinatra
73
79
 
data/test/test_mapping.rb CHANGED
@@ -9,7 +9,7 @@ require 'ruby-debug'
9
9
 
10
10
  class Sinatra::Base
11
11
  def env
12
- {'SCRIPT_NAME' => 'blog'}
12
+ @env ||= { 'SCRIPT_NAME' => '/blog' }
13
13
  end
14
14
  end
15
15
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-mapping
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hallison Batista