sinatra-mapping 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,7 +1,9 @@
1
1
  = Changes
2
2
 
3
- [1.1.1 - not released]
3
+ [1.2.0 - 2009-09-22]
4
4
  * The mapping methods works in classic application.
5
+ * Updates in documentation.
6
+ * All tests have been fixed.
5
7
 
6
8
  [1.1.0 - 2009-08-25]
7
9
  * Added suffix "/?" in all paths when no arguments.
data/INFO CHANGED
@@ -9,6 +9,5 @@
9
9
  :email: email@hallisonbatista.com
10
10
  :homepage: http://sinatra-mapping.rubyforge.org/
11
11
  :dependencies:
12
- rack: >= 0.9.1
13
12
  sinatra: >= 0.9.1.1
14
13
 
data/LICENSE CHANGED
@@ -17,3 +17,4 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
17
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
18
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
19
  THE SOFTWARE.
20
+
@@ -2,7 +2,7 @@
2
2
 
3
3
  Map easily URLs in your Web applications.
4
4
 
5
- * {Homepage}[:link:/]
5
+ * {Homepage}[http://sinatra-mapping.rubyforge.org/]
6
6
  * {Repository}[http://github.com/hallison/sinatra-mapping]
7
7
  * {Project}[http://rubyforge.org/projects/sinatra-mapping]
8
8
  * {Issues}[http://github.com/hallison/sinatra-mapping/issues]
@@ -43,113 +43,109 @@ Sinatra implements REST routes:
43
43
 
44
44
  For improve this routes use extension by registered method in the main
45
45
  source of application. To better understand, copy and paste the following
46
- example in {Ruby}[http://www.ruby-lang.org] source file +webfoo.rb+:
46
+ example in {Ruby}[http://www.ruby-lang.org] source file +weblog.rb+:
47
47
 
48
48
  #!/usr/bin/env ruby
49
49
  require 'rubygems'
50
50
  require 'sinatra'
51
51
  require 'sinatra/mapping' # only this line for use mapping!
52
52
 
53
- class Sinatra::Application
54
-
55
- map :root, "blog" # /blog/
56
- map :entries, "posts" # /blog/posts
57
- map :tags, "labels" # /blog/labels
58
-
59
- mapping :entry => "posts/:entry_id", # /blog/posts/id-for-post
60
- :entry_comments => "posts/:entry_id/comments", # /blog/posts/id-for-post/comments
61
- :tagged_entries => "labels/:tag_id/entries" # /blog/labels/id-for-tag/entries
62
-
63
- # /blog/
64
- get root_path do
65
- <<-end_content
66
- <h1>Welcome to Foo Web Application</h1>
67
- <ul>
68
- <li>#{link_to title_path(:entries), :entries, :title => title_path(:entries)}</li>
69
- <li>#{link_to title_path(:tags), :tags, :title => title_path(:tags)}</li>
70
- </ul>
71
- end_content
72
- end
73
-
74
- # /blog/entries
75
- get entries_path do
76
- <<-end_content
77
- <h1>Welcome to Foo Web Application</h1>
78
- <h2>#{title_path(:entries)}</h2>
79
- <ul>
80
- <li>#{link_to "Testing new entry ...", :entries, "testing-new-entry"}</li>
81
- <li>#{link_to "Testing old entry ...", :entries, "testing-old-entry"}</li>
82
- </ul>
83
- <p>
84
- #{link_to "Back", :root}
85
- </p>
86
- end_content
87
- end
88
-
89
- # /blog/labels/tag-id-for-show-content
90
- get tags_path do
91
- <<-end_content
92
- <h1>Welcome to Foo Web Application</h1>
93
- <h2>#{title_path(:tags)}</h2>
94
- <ul>
95
- <li>#{link_to "Ruby", :tags, "ruby", "entries"}</li>
96
- <li>#{link_to "Sinatra", :tags, "sinatra", "entries"}</li>
97
- <li>#{link_to "Mapping", :tags, "mapping", "entries"}</li>
98
- </ul>
99
- <p>
100
- #{link_to "Back", :root}
101
- </p>
102
- end_content
103
- end
104
-
105
- # /blog/entries/entry-id-for-show-content
106
- get entry_path do |entry_id|
107
- title = entry_id.gsub('-',' ').capitalize
108
- <<-end_content
109
- <h1>Welcome to Foo Web Application</h1>
110
- <h2>#{title}</h2>
111
- <p>
112
- It works!
113
- </p>
114
- <p>
115
- #{link_to "Back", :root} | #{link_to "Comments", :entries, entry_id, 'comments'}
116
- </p>
117
- end_content
118
- end
119
-
120
- # /blog/entries/entry-id-for-show-content/comments
121
- get entry_comments_path do |entry_id|
122
- title = entry_id.gsub('-',' ').capitalize
123
- <<-end_content
124
- <h1>Welcome to Foo Web Application</h1>
125
- <h2>#{title}</h2>
126
- <p>
127
- It works and show comments for "#{title}".
128
- </p>
129
- <p>
130
- #{link_to "Back", :root}
131
- </p>
132
- end_content
133
- end
134
-
135
- get tagged_entries_path do |tag_id|
136
- <<-end_content
137
- <h1>Welcome to Foo Web Application</h1>
138
- <h2>#{tag_id.capitalize}</h2>
139
- <p>
140
- It works and show all entries tagged with "#{tag_id}".
141
- </p>
142
- <p>
143
- #{link_to "Back", :root}
144
- </p>
145
- end_content
146
- end
53
+ map :root, "blog" # /blog/
54
+ map :entries, "posts" # /blog/posts
55
+ map :tags, "labels" # /blog/labels
56
+
57
+ mapping :entry => "posts/:entry_id", # /blog/posts/id-for-post
58
+ :entry_comments => "posts/:entry_id/comments", # /blog/posts/id-for-post/comments
59
+ :tagged_entries => "labels/:tag_id/entries" # /blog/labels/id-for-tag/entries
60
+
61
+ # /blog/
62
+ get root_path do
63
+ <<-end_content
64
+ <h1>Welcome to Foo Web Application</h1>
65
+ <ul>
66
+ <li>#{link_to title_path(:entries), :entries, :title => title_path(:entries)}</li>
67
+ <li>#{link_to title_path(:tags), :tags, :title => title_path(:tags)}</li>
68
+ </ul>
69
+ end_content
70
+ end
71
+
72
+ # /blog/entries
73
+ get entries_path do
74
+ <<-end_content
75
+ <h1>Welcome to Foo Web Application</h1>
76
+ <h2>#{title_path(:entries)}</h2>
77
+ <ul>
78
+ <li>#{link_to "Testing new entry ...", :entries, "testing-new-entry"}</li>
79
+ <li>#{link_to "Testing old entry ...", :entries, "testing-old-entry"}</li>
80
+ </ul>
81
+ <p>
82
+ #{link_to "Back", :root}
83
+ </p>
84
+ end_content
85
+ end
86
+
87
+ # /blog/labels/tag-id-for-show-content
88
+ get tags_path do
89
+ <<-end_content
90
+ <h1>Welcome to Foo Web Application</h1>
91
+ <h2>#{title_path(:tags)}</h2>
92
+ <ul>
93
+ <li>#{link_to "Ruby", :tags, "ruby", "entries"}</li>
94
+ <li>#{link_to "Sinatra", :tags, "sinatra", "entries"}</li>
95
+ <li>#{link_to "Mapping", :tags, "mapping", "entries"}</li>
96
+ </ul>
97
+ <p>
98
+ #{link_to "Back", :root}
99
+ </p>
100
+ end_content
101
+ end
102
+
103
+ # /blog/entries/entry-id-for-show-content
104
+ get entry_path do |entry_id|
105
+ title = entry_id.gsub('-',' ').capitalize
106
+ <<-end_content
107
+ <h1>Welcome to Foo Web Application</h1>
108
+ <h2>#{title}</h2>
109
+ <p>
110
+ It works!
111
+ </p>
112
+ <p>
113
+ #{link_to "Back", :root} | #{link_to "Comments", :entries, entry_id, 'comments'}
114
+ </p>
115
+ end_content
116
+ end
117
+
118
+ # /blog/entries/entry-id-for-show-content/comments
119
+ get entry_comments_path do |entry_id|
120
+ title = entry_id.gsub('-',' ').capitalize
121
+ <<-end_content
122
+ <h1>Welcome to Foo Web Application</h1>
123
+ <h2>#{title}</h2>
124
+ <p>
125
+ It works and show comments for "#{title}".
126
+ </p>
127
+ <p>
128
+ #{link_to "Back", :root}
129
+ </p>
130
+ end_content
131
+ end
147
132
 
133
+ get tagged_entries_path do |tag_id|
134
+ <<-end_content
135
+ <h1>Welcome to Foo Web Application</h1>
136
+ <h2>#{tag_id.capitalize}</h2>
137
+ <p>
138
+ It works and show all entries tagged with "#{tag_id}".
139
+ </p>
140
+ <p>
141
+ #{link_to "Back", :root}
142
+ </p>
143
+ end_content
148
144
  end
149
145
 
150
146
  Run:
151
147
 
152
- $ ruby webfoo.rb -p 3000
148
+ $ ruby weblog.rb -p 3000
153
149
 
154
150
  Open Web browser http://localhost:3000/blog/ and look ... it works!
155
151
 
data/VERSION CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  :release:
3
- :date: 2009-08-25
4
3
  :cycle: Stable release
5
4
  :major: 1
6
- :minor: 1
5
+ :minor: 2
6
+ :date: 2009-09-22
7
7
  :patch: 0
@@ -22,8 +22,8 @@ module Mapping
22
22
  #
23
23
  # If name is equal :root, then returns path ended by slash "/".
24
24
  #
25
- # map :root, "tasks" # => /tasks/
26
- # map :changes, "last-changes # => /tasks/last-changes
25
+ # map :root, "tasks" #=> /tasks/
26
+ # map :changes, "last-changes #=> /tasks/last-changes
27
27
  def map(name, path = nil)
28
28
  @locations ||= {}
29
29
  if name.to_sym == :root
@@ -37,12 +37,14 @@ module Mapping
37
37
  map_path_to(@locations[name.to_sym], *paths << "/?")
38
38
  end
39
39
  end
40
+ Delegator.delegate "#{name}_path"
40
41
  end
41
42
 
42
43
  # Auto mapping from a hash. This method is very useful.
43
44
  # Example:
44
45
  #
45
- # # In Web application.
46
+ # In Web application:
47
+ #
46
48
  # class WebApp << Sinatra::Base
47
49
  # mapping :root => "tasks", # /tasks
48
50
  # :status => "changes" # /tasks/changes
@@ -50,21 +52,28 @@ module Mapping
50
52
  #
51
53
  # Or, it's possible use from configuration file.
52
54
  #
53
- # # YAML file "settings.yml".
54
- # mapping:
55
- # root: tasks
56
- # status: changes
55
+ # # YAML file "mapping.yml".
56
+ # root: tasks
57
+ # status: changes
57
58
  #
58
59
  # # In Sinatra application.
59
- # mapping YAML.load_file("settings.yml")[:mapping]
60
- # # root_path # /tasks
61
- # # status_path # /tasks/changes
60
+ #
61
+ # mapping YAML.load_file("mapping.yml")
62
+ # #=> root_path # /tasks
63
+ # #=> status_path # /tasks/changes
62
64
  def mapping(hash)
63
65
  hash.each do |name, path|
64
66
  map name, path
65
67
  end
66
68
  end
67
69
 
70
+ # Register automatically all helpers in base application.
71
+ def self.registered(app)
72
+ app.helpers Mapping::Helpers
73
+ end
74
+
75
+ private
76
+
68
77
  # Returns URL path with query instructions.
69
78
  # This method has been extracted from
70
79
  # http://wiki.github.com/sinatra/sinatra/howto-generate-links.
@@ -76,13 +85,6 @@ module Mapping
76
85
  path
77
86
  end
78
87
 
79
- # Register automatically all helpers in base application.
80
- def self.registered(app)
81
- app.helpers Mapping::Helpers
82
- end
83
-
84
- private
85
-
86
88
  # Check arguments. If argument is a symbol and exist map path before
87
89
  # setted, then return path mapped by symbol name.
88
90
  def map_path_to(*args)
@@ -111,29 +113,31 @@ private
111
113
  paths.join('/').gsub(/[\/]{2,}/,'/')
112
114
  end
113
115
 
116
+ # Copyright (c) 2009 Hallison Batista
117
+ #
114
118
  # This module contains several helper methods for paths written using
115
- # {map}[:link:./Sinatra/Mapping.html#map] method.
119
+ # +map+ method.
116
120
  module Helpers
117
121
 
118
122
  # Creates a title using a path mapped. Otherwise, returns just arguments
119
123
  # joined by spaces and capitalised.
120
124
  #
121
- # # In Sinatra application
125
+ # In Sinatra application:
122
126
  #
123
127
  # map :posts, "articles"
124
128
  # map :tags, "labels"
125
129
  # map :archive, "archive/articles"
126
130
  #
127
- # # In views
131
+ # In views:
128
132
  #
129
133
  # <%=title_path :posts%>
130
- # # => "Articles"
134
+ # #=> "Articles"
131
135
  #
132
136
  # <%=title_path :tags%>
133
- # # => "Labels"
137
+ # #=> "Labels"
134
138
  #
135
139
  # <%=title_path :archive%>
136
- # # => "Archive articles"
140
+ # #=> "Archive articles"
137
141
  def title_path(path, *args)
138
142
  title = (options.locations[path] || path).to_s.gsub('/',' ').strip
139
143
  title.gsub!(/\W/,' ') # Cleanup
@@ -143,7 +147,7 @@ private
143
147
  # Creates anchor links for name and extract path and HTML options from
144
148
  # arguments. Example:
145
149
  #
146
- # # In Sinatra application, add a map.
150
+ # In Sinatra application, add a map.
147
151
  #
148
152
  # map :tasks, "tasks"
149
153
  # map :status, "tasks/status"
@@ -156,13 +160,13 @@ private
156
160
  # erb :status, :locals => { :status => "finished" }
157
161
  # end
158
162
  #
159
- # # In status view, add a link to status map.
163
+ # In status view, add a link to status map.
160
164
  #
161
165
  # <%= link_to "All finished", :status, status %>
162
- # # => <a href="/tasks/status/finished">All finished</a>
166
+ # #=> <a href="/tasks/status/finished">All finished</a>
163
167
  #
164
168
  # <%= link_to "All finished", :status, :name => status %>
165
- # # => <a href="/tasks/status?name=finished">All finished</a>
169
+ # #=> <a href="/tasks/status?name=finished">All finished</a>
166
170
  def link_to(name = nil, *args)
167
171
  options = args.last.kind_of?(Hash) ? args.pop : {}
168
172
  url = args.shift if args.first.to_s =~ /^\w.*?:/
@@ -172,17 +176,17 @@ private
172
176
 
173
177
  # Returns all paths with query parameters. Example:
174
178
  #
175
- # # In Sinatra application:
179
+ # In Sinatra application:
176
180
  #
177
181
  # map :post, "articles"
178
182
  # map :tags, "labels"
179
183
  #
180
- # # Use the following instructions:
184
+ # Use the following instructions:
181
185
  #
182
186
  # path_to :tags, "ruby", :posts
183
- # # => "/labels/ruby/articles"
187
+ # #=> "/labels/ruby/articles"
184
188
  def path_to(*args)
185
- options.build_path_to(env['SCRIPT_NAME'], *args)
189
+ self.class.send(:build_path_to, env['SCRIPT_NAME'], *args)
186
190
  end
187
191
 
188
192
  private
@@ -3,11 +3,10 @@ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/..")
3
3
  require 'rubygems'
4
4
  require 'test/unit'
5
5
  require 'rack/test'
6
- require 'ruby-debug'
7
6
 
8
7
  require 'test/fixtures/classic_application'
9
8
 
10
- class ModularTestMapping < Test::Unit::TestCase
9
+ class ClassicMappingTest < Test::Unit::TestCase
11
10
 
12
11
  include Rack::Test::Methods
13
12
 
@@ -32,7 +31,7 @@ class ModularTestMapping < Test::Unit::TestCase
32
31
  end
33
32
 
34
33
  def app
35
- @app = ::ModularApplication
34
+ @app = Sinatra::Application
36
35
  @app.set :environment, :test
37
36
  @app
38
37
  end
@@ -3,13 +3,12 @@ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../../lib/")
3
3
 
4
4
  require 'sinatra'
5
5
  require 'sinatra/mapping'
6
- require 'ruby-debug'
7
6
 
8
- #class Sinatra::Application
9
- # def env
10
- # @env.update('SCRIPT_NAME' => '/test')
11
- # end
12
- #end
7
+ class Sinatra::Application
8
+ def env
9
+ @env.update('SCRIPT_NAME' => '/test')
10
+ end
11
+ end
13
12
 
14
13
  map :root, "blog" # root_path => /blog/
15
14
  map :about # about_path => /blog/about
@@ -48,7 +47,7 @@ get about_path do
48
47
  end
49
48
 
50
49
  get search_path do
51
- <<-end_content.gsub(/^ /,'')
50
+ <<-end_content.gsub(/^ /,'')
52
51
  #{title_path :search}:#{path_to :search, :keywords => 'ruby'}
53
52
  #{link_to "Search", :search, :title => 'Search'}
54
53
  #{link_to "Search", :search, :title => 'Search', :keywords => 'ruby'}
@@ -56,7 +55,7 @@ get search_path do
56
55
  end
57
56
 
58
57
  get drafts_path do
59
- <<-end_content.gsub(/^ /,'')
58
+ <<-end_content.gsub(/^ /,'')
60
59
  #{title_path :drafts}:#{path_to [:drafts, :posts]}
61
60
  #{link_to "Unpublished", :drafts, :posts, :title => 'Unpublished'}
62
61
  end_content
@@ -3,11 +3,10 @@ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/..")
3
3
  require 'rubygems'
4
4
  require 'test/unit'
5
5
  require 'rack/test'
6
- require 'ruby-debug'
7
6
 
8
7
  require 'test/fixtures/modular_application'
9
8
 
10
- class ModularTestMapping < Test::Unit::TestCase
9
+ class ModularMappingTest < Test::Unit::TestCase
11
10
 
12
11
  include Rack::Test::Methods
13
12
 
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.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hallison Batista
@@ -9,19 +9,9 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-25 00:00:00 -04:00
12
+ date: 2009-09-22 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: rack
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: 0.9.1
24
- version:
25
15
  - !ruby/object:Gem::Dependency
26
16
  name: sinatra
27
17
  type: :runtime