sinatra-mapping 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,5 +1,11 @@
1
1
  = Changes
2
2
 
3
+ [1.0.5 - July 2009]
4
+ * Fixes in the HTML attributes into helper method for link attributes.
5
+ * Fixes in the map path method which check script name in environment.
6
+ * Improvements helper methods.
7
+ * Updates in documentation.
8
+
3
9
  [1.0.4 - July 2009]
4
10
  * Error in root path without application/script name has been fixed.
5
11
  * Helper for mapping has been registered automatically.
data/VERSION CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  :release:
3
- :date: 2009-07-27
3
+ :date: 2009-07-30
4
4
  :cycle: Release
5
5
  :major: 1
6
6
  :minor: 0
7
- :patch: 4
7
+ :patch: 5
@@ -52,10 +52,11 @@ module Sinatra
52
52
  # mapping:
53
53
  # root: tasks
54
54
  # status: changes
55
- # # In Web application.
56
- # class WebApp << Sinatra::Base
57
- # mapping YAML.load_file("settings.yml")[:mapping]
58
- # end
55
+ #
56
+ # # In Sinatra application.
57
+ # mapping YAML.load_file("settings.yml")[:mapping]
58
+ # # root_path # /tasks
59
+ # # status_path # /tasks/changes
59
60
  def mapping(hash)
60
61
  hash.each do |name, path|
61
62
  map name, path
@@ -63,11 +64,13 @@ module Sinatra
63
64
  end
64
65
 
65
66
  # Returns URL path with query instructions.
67
+ # This method has been extracted from
68
+ # http://wiki.github.com/sinatra/sinatra/howto-generate-links.
66
69
  def build_path_to(script_name = nil, *args)
67
70
  args.compact!
68
- query = args.pop if args.last.kind_of?(Hash)
69
- path = map_path_to(script_name, *args)
70
- path << "?" << Rack::Utils::build_query(query) if query
71
+ query = args.pop if args.last.kind_of?(Hash)
72
+ path = map_path_to(script_name, *args)
73
+ path << "?" << Rack::Utils::build_query(query) if query
71
74
  path
72
75
  end
73
76
 
@@ -76,17 +79,14 @@ module Sinatra
76
79
  # Check arguments. If argument is a symbol and exist map path before
77
80
  # setted, then return path mapped by symbol name.
78
81
  def map_path_to(*args)
79
- script_name = args.shift if args.first.to_s =~ /\/\w.*/
82
+ script_name = args.shift if args.first.to_s =~ /^\/\w.*/
80
83
  path_mapped(script_name, *locations_get_from(*args))
81
84
  end
82
85
 
83
86
  # Returns all paths mapped by root path in prefix.
84
87
  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
88
+ return cleanup_paths("/#{script_name}/#{@locations[:root]}") if args.empty?
89
+ cleanup_paths("/#{script_name}/#{@locations[:root]}/#{args.join('/')}")
90
90
  end
91
91
 
92
92
  # Get paths from location maps.
@@ -10,6 +10,23 @@ module Sinatra
10
10
 
11
11
  # Creates a title using a path mapped. Otherwise, returns just arguments
12
12
  # joined by spaces and capitalised.
13
+ #
14
+ # # In Sinatra application
15
+ #
16
+ # map :posts, "articles"
17
+ # map :tags, "labels"
18
+ # map :archive, "archive/articles"
19
+ #
20
+ # # In views
21
+ #
22
+ # <%=title_path :posts%>
23
+ # # => "Articles"
24
+ #
25
+ # <%=title_path :tags%>
26
+ # # => "Labels"
27
+ #
28
+ # <%=title_path :archive%>
29
+ # # => "Archive articles"
13
30
  def title_path(path, *args)
14
31
  title = (options.locations[path] || path).to_s.gsub('/',' ').strip
15
32
  title.gsub!(/\W/,' ') # Cleanup
@@ -19,35 +36,44 @@ module Sinatra
19
36
  # Creates anchor links for name and extract path and HTML options from
20
37
  # arguments. Example:
21
38
  #
22
- # # In Web application, add a map.
39
+ # # In Sinatra application, add a map.
40
+ #
41
+ # map :tasks, "tasks"
23
42
  # map :status, "tasks/status"
24
43
  #
44
+ # get tasks_path do
45
+ # erb :tasks, :locals => { :name => params.values.join(', ') }
46
+ # end
47
+ #
25
48
  # get status_path do
26
- # status = "finished"
27
- # erb :status, :locals => { :status => status }
49
+ # erb :status, :locals => { :status => "finished" }
28
50
  # end
29
51
  #
30
52
  # # In status view, add a link to status map.
53
+ #
31
54
  # <%= link_to "All finished", :status, status %>
32
55
  # # => <a href="/tasks/status/finished">All finished</a>
56
+ #
57
+ # <%= link_to "All finished", :status, :name => status %>
58
+ # # => <a href="/tasks/status?name=finished">All finished</a>
33
59
  def link_to(name = nil, *args)
34
60
  options = args.last.kind_of?(Hash) ? args.pop : {}
35
61
  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>"
62
+ args << extract_query_attributes(options)
63
+ "<a href=\"#{url || path_to(*args)}\"#{extract_link_attributes(options)}>#{name || url}</a>"
41
64
  end
42
65
 
43
66
  # Returns all paths with query parameters. Example:
44
67
  #
45
- # # in Sinatra application:
68
+ # # In Sinatra application:
69
+ #
46
70
  # map :post, "articles"
47
71
  # map :tags, "labels"
48
- # # use the following instructions:
72
+ #
73
+ # # Use the following instructions:
74
+ #
49
75
  # path_to :tags, "ruby", :posts
50
- # # returns "/labels/ruby/articles"
76
+ # # => "/labels/ruby/articles"
51
77
  def path_to(*args)
52
78
  options.build_path_to(env['SCRIPT_NAME'], *args)
53
79
  end
@@ -55,22 +81,31 @@ module Sinatra
55
81
  private
56
82
 
57
83
  # Extract all tag attributes from a hash keys and values.
58
- def extract_tags_attributes(hash)
59
- hash.select do |key, value|
60
- [ :accesskey, :charset, :coords,
61
- :hreflang, :id, :lang,
62
- :name, :onblur, :onclick,
63
- :ondblclick, :onfocus, :onkeydown,
64
- :onkeypress, :onkeyup, :onmousedown,
65
- :onmousemove, :onmouseout, :onmouseover,
66
- :onmouseup, :rel, :rev,
67
- :shape, :style, :tabindex,
68
- :target, :title, :type ].include? key
69
- end.map do |attr, value|
70
- "#{attr}=\"#{value}\""
84
+ def extract_link_attributes(hash)
85
+ select_link_attributes(hash).map do |attribute, value|
86
+ " #{attribute}=\"#{value}\""
71
87
  end
72
88
  end
73
89
 
90
+ # Returns only attributes for link tag.
91
+ def select_link_attributes(hash)
92
+ hash.select{ |key, value| link_attributes.include?key }
93
+ end
94
+
95
+ # Select all keys and values that not included in link attributes.
96
+ def extract_query_attributes(hash)
97
+ query = hash.select{ |key, value| !link_attributes.include?key }.flatten
98
+ Hash[*query] unless query.empty?
99
+ end
100
+
101
+ # Attribute list for link tag.
102
+ def link_attributes
103
+ [:accesskey, :charset, :coords, :hreflang, :id, :lang, :name, :onblur,
104
+ :onclick, :ondblclick, :onfocus, :onkeydown, :onkeypress, :onkeyup,
105
+ :onmousedown, :onmousemove, :onmouseout, :onmouseover, :onmouseup,
106
+ :rel, :rev, :shape, :style, :tabindex, :target, :title, :type]
107
+ end
108
+
74
109
  end # module MappingHelpers
75
110
 
76
111
  helpers MappingHelpers
data/test/test_mapping.rb CHANGED
@@ -74,7 +74,11 @@ class AppForTest < Sinatra::Base
74
74
  end
75
75
 
76
76
  get search_path do
77
- "#{title_path :search}:#{path_to :search, :keywords => 'ruby'}"
77
+ <<-end_content.gsub(/^ /,'')
78
+ #{title_path :search}:#{path_to :search, :keywords => 'ruby'}
79
+ #{link_to "Search", :search, :title => 'Search'}
80
+ #{link_to "Search", :search, :title => 'Search', :keywords => 'ruby'}
81
+ end_content
78
82
  end
79
83
 
80
84
  end
@@ -188,7 +192,9 @@ class TestMapping < Test::Unit::TestCase
188
192
  get "#{app.search_path}?keywords=ruby" do |response|
189
193
  assert response.ok?
190
194
  assert_equal "http://example.org#{@locations[:search_path]}?keywords=ruby", last_request.url
191
- assert_equal "Find articles:#{@locations[:search_path]}?keywords=ruby", response.body
195
+ assert_equal "Find articles:#{@locations[:search_path]}?keywords=ruby", response.body.split("\n")[0]
196
+ assert_equal "<a href=\"#{@locations[:search_path]}\" title=\"Search\">Search</a>", response.body.split("\n")[1]
197
+ assert_equal "<a href=\"#{@locations[:search_path]}?keywords=ruby\" title=\"Search\">Search</a>", response.body.split("\n")[2]
192
198
  end
193
199
  end
194
200
 
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.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hallison Batista
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-27 00:00:00 -04:00
12
+ date: 2009-07-30 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency