sinatra-mapping 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +5 -0
- data/README +16 -10
- data/Rakefile +15 -17
- data/VERSION +2 -2
- data/lib/sinatra/mapping.rb +13 -20
- data/lib/sinatra/mapping_helpers.rb +19 -3
- data/test/test_mapping.rb +14 -13
- metadata +2 -2
data/CHANGES
CHANGED
data/README
CHANGED
@@ -1,4 +1,10 @@
|
|
1
|
-
= Sinatra
|
1
|
+
= Sinatra::Mapping - Map easily URLs paths
|
2
|
+
|
3
|
+
|
4
|
+
* {Repository}[http://github.com/hallison/sinatra-mapping]
|
5
|
+
* {Project}[http://rubyforge.org/projects/sinatra-mapping]
|
6
|
+
* {Documentation}[:link:Sinatra/Mapping.html]
|
7
|
+
* {Issues}[http://github.com/hallison/sinatra-mapping/issues]
|
2
8
|
|
3
9
|
{Sinatra}[http://www.sinatrarb.com]
|
4
10
|
{mapping extension}[http://sinatra-mapping.rubyforge.org] is a minimal
|
@@ -10,7 +16,7 @@ Install stable version gem:
|
|
10
16
|
|
11
17
|
Or, install development version gem:
|
12
18
|
|
13
|
-
gem install sinatra-mapping --source http://gems.github.com
|
19
|
+
gem install hallison-sinatra-mapping --source http://gems.github.com
|
14
20
|
|
15
21
|
== Getting start
|
16
22
|
|
@@ -31,31 +37,31 @@ Example:
|
|
31
37
|
|
32
38
|
get root_path do
|
33
39
|
# /blog/
|
34
|
-
# do
|
40
|
+
# do something for root path.
|
35
41
|
end
|
36
42
|
|
37
43
|
get posts_path do
|
38
44
|
# /blog/articles
|
39
|
-
# do
|
45
|
+
# do something for posts path.
|
40
46
|
end
|
41
47
|
|
42
48
|
get posts_path(":year/:month/:day/:permalink") do |year, month, day, permalink|
|
43
49
|
# /blog/articles/2009/10/08/permalink-for-your-article
|
44
|
-
# do
|
50
|
+
# do something for posts path using parameters for find a post.
|
45
51
|
end
|
46
52
|
|
47
53
|
get tags_path do
|
48
54
|
# /blog/labels
|
49
|
-
# do
|
55
|
+
# do something for tags path.
|
50
56
|
end
|
51
57
|
|
52
58
|
get archive_path do
|
53
|
-
# do
|
59
|
+
# do something for archive path.
|
54
60
|
end
|
55
61
|
|
56
62
|
get archive_path(":year/:month/:day/:permalink") do |year, month, day, permalink|
|
57
63
|
# /blog/archived-articles/1978/10/08/permalink-for-your-article
|
58
|
-
# do
|
64
|
+
# do something for archive path using parameters for find a post.
|
59
65
|
end
|
60
66
|
|
61
67
|
end
|
@@ -64,8 +70,8 @@ Easy!!!
|
|
64
70
|
|
65
71
|
== More informations
|
66
72
|
|
67
|
-
* See {changes}[:link:CHANGES.html] for understand the goals of
|
68
|
-
release
|
73
|
+
* See the {changes}[:link:CHANGES.html] for understand the goals of
|
74
|
+
that release version.
|
69
75
|
* {License}[:link:LICENSE.html]
|
70
76
|
|
71
77
|
== Copyright
|
data/Rakefile
CHANGED
@@ -7,25 +7,23 @@ load 'sinatra-mapping.gemspec'
|
|
7
7
|
desc "Generate version for release and tagging."
|
8
8
|
task :version, [:major, :minor, :patch, :release, :date, :cycle] do |taskspec, version|
|
9
9
|
require 'parsedate'
|
10
|
-
current = YAML.load_file(taskspec.name.upcase)
|
10
|
+
current = YAML.load_file(taskspec.name.upcase) || {}
|
11
11
|
version_date = Date.new(*ParseDate.parsedate(version[:date] || current[:date].to_s).compact) unless version or current
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
}
|
12
|
+
newer = {
|
13
|
+
:major => version[:major].to_i || current[:major].to_i,
|
14
|
+
:minor => version[:minor].to_i || current[:minor].to_i,
|
15
|
+
:patch => version[:patch].to_i || current[:patch].to_i,
|
16
|
+
:release => version[:release].to_s.empty? ? nil : version[:release].to_i,
|
17
|
+
:date => version_date || Date.today,
|
18
|
+
:cycle => version[:cycle] || current[:cycle]
|
19
|
+
}
|
21
20
|
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
newer.merge(current) do |key, new_value, current_value|
|
22
|
+
new_value || current_value
|
23
|
+
end
|
25
24
|
|
26
|
-
|
27
|
-
|
28
|
-
end
|
25
|
+
File.open(taskspec.name.upcase, "w") do |version_file|
|
26
|
+
version_file << newer.to_yaml
|
29
27
|
end
|
30
28
|
end
|
31
29
|
|
@@ -36,7 +34,7 @@ Rake::GemPackageTask.new(@spec) do |pkg|
|
|
36
34
|
end
|
37
35
|
|
38
36
|
Rake::RDocTask.new("doc") do |rdoc|
|
39
|
-
rdoc.title = "Sinatra
|
37
|
+
rdoc.title = "Sinatra::Mapping"
|
40
38
|
rdoc.main = "README"
|
41
39
|
rdoc.options = [ '-SHN', '-f', 'darkfish' ]
|
42
40
|
rdoc.rdoc_dir = 'doc'
|
data/VERSION
CHANGED
data/lib/sinatra/mapping.rb
CHANGED
@@ -20,26 +20,34 @@ module Sinatra
|
|
20
20
|
# map :root, "tasks" # => /tasks/
|
21
21
|
# map :changes, "last-changes # => /tasks/last-changes
|
22
22
|
def map(name, path = nil)
|
23
|
-
@root_path ||= ""
|
24
23
|
@locations ||= {}
|
25
24
|
if name.to_sym == :root
|
26
|
-
@
|
25
|
+
@locations[:root] = cleanup_paths("/#{path}/")
|
27
26
|
metadef "#{name}_path" do |*paths|
|
28
|
-
@
|
27
|
+
@locations[:root]
|
29
28
|
end
|
30
29
|
else
|
31
30
|
@locations[name.to_sym] = path || name.to_s
|
32
31
|
metadef "#{name}_path" do |*paths|
|
33
|
-
|
32
|
+
map_path_to(@locations[name.to_sym], *paths)
|
34
33
|
end
|
35
34
|
end
|
36
35
|
end
|
37
36
|
|
37
|
+
# Returns URL path with query instructions.
|
38
|
+
def query_path_to(*args)
|
39
|
+
args.compact!
|
40
|
+
query = args.pop if args.last.kind_of?(Hash)
|
41
|
+
path = map_path_to(*args)
|
42
|
+
path << "?" << Rack::Utils::build_query(query) if query
|
43
|
+
path
|
44
|
+
end
|
45
|
+
|
38
46
|
private
|
39
47
|
|
40
48
|
# Check arguments. If argument is a symbol and exist map path before
|
41
49
|
# setted, then return path mapped by symbol name.
|
42
|
-
def
|
50
|
+
def map_path_to(*args)
|
43
51
|
path_mapped(*locations_get_from(*args))
|
44
52
|
end
|
45
53
|
|
@@ -50,7 +58,6 @@ module Sinatra
|
|
50
58
|
|
51
59
|
# Get paths from location maps.
|
52
60
|
def locations_get_from(*args)
|
53
|
-
args.delete(:root)
|
54
61
|
args.collect do |path|
|
55
62
|
@locations.has_key?(path) ? @locations[path] : path
|
56
63
|
end
|
@@ -67,17 +74,3 @@ module Sinatra
|
|
67
74
|
|
68
75
|
end # module Sinatra
|
69
76
|
|
70
|
-
# private
|
71
|
-
#
|
72
|
-
# attr_reader :root
|
73
|
-
# attr_accessor :locations
|
74
|
-
#
|
75
|
-
# def root_set_to(path)
|
76
|
-
# @root = path_clean("/#{path}/")
|
77
|
-
# end
|
78
|
-
# alias :root= :root_set_to
|
79
|
-
#
|
80
|
-
#
|
81
|
-
# private
|
82
|
-
#
|
83
|
-
#
|
@@ -1,17 +1,33 @@
|
|
1
1
|
module Sinatra
|
2
2
|
|
3
|
-
# This module contains several helper methods for
|
3
|
+
# This module contains several helper methods for paths written using
|
4
4
|
# {map}[:link:./Sinatra/Mapping.html#map] method.
|
5
|
+
#
|
6
|
+
# For use this helper, add the following syntax in application source:
|
7
|
+
#
|
8
|
+
# register Sinatra::MappingHelpers
|
5
9
|
module MappingHelpers
|
6
10
|
|
7
|
-
#
|
8
|
-
# joined by spaces and
|
11
|
+
# Creates a title using a path mapped. Otherwise, returns just arguments
|
12
|
+
# joined by spaces and capitalised.
|
9
13
|
def title_path(path, *args)
|
10
14
|
title = (options.send("#{path}_path") || path).to_s.gsub('/',' ').strip
|
11
15
|
title.gsub!(/\W/,' ') # Cleanup
|
12
16
|
(args.empty? ? title : "#{title} #{args.join(' ')}").capitalize
|
13
17
|
end
|
14
18
|
|
19
|
+
# Returns all paths with query parameters. Example:
|
20
|
+
#
|
21
|
+
# # in Sinatra application:
|
22
|
+
# map :post, "articles"
|
23
|
+
# map :tags, "labels"
|
24
|
+
# # use the following instructions:
|
25
|
+
# path_to :tags, "ruby", :posts
|
26
|
+
# # returns "/labels/ruby/articles"
|
27
|
+
def path_to(*args)
|
28
|
+
options.query_path_to(*args)
|
29
|
+
end
|
30
|
+
|
15
31
|
end # module MapHelpers
|
16
32
|
|
17
33
|
end # module Sinatra
|
data/test/test_mapping.rb
CHANGED
@@ -10,60 +10,61 @@ require 'ruby-debug'
|
|
10
10
|
class AppForTest < Sinatra::Base
|
11
11
|
|
12
12
|
register Sinatra::Mapping
|
13
|
+
helpers Sinatra::MappingHelpers
|
13
14
|
|
14
15
|
map :root # root_path => /
|
15
16
|
map :posts, "articles" # posts_path => /articles
|
16
|
-
map :archive, "archive/articles" # archive_path => /articles
|
17
|
+
map :archive, "archive/articles" # archive_path => /archive/articles
|
17
18
|
map :about # about_path => /about
|
18
19
|
|
19
|
-
|
20
|
+
#mapping YAML.load_file("#{File.dirname(__FILE__)}/fixtures.yml")
|
20
21
|
|
21
22
|
before do
|
22
23
|
@date = Date.today
|
23
24
|
end
|
24
25
|
|
25
26
|
get root_path do
|
26
|
-
"#{title_path :root}:#{
|
27
|
+
"#{title_path :root}:#{path_to :root}"
|
27
28
|
end
|
28
29
|
|
29
30
|
get posts_path do
|
30
|
-
"#{title_path :posts}:#{
|
31
|
+
"#{title_path :posts}:#{path_to :posts}"
|
31
32
|
end
|
32
33
|
|
33
34
|
get posts_path "/" do
|
34
|
-
redirect
|
35
|
+
redirect path_to(:posts), 301
|
35
36
|
end
|
36
37
|
|
37
38
|
get posts_path "/:year/:month/:day/:permalink" do |year, month, day, permalink|
|
38
|
-
"#{title_path :posts}:" +
|
39
|
+
"#{title_path :posts}:" + path_to(:posts, "#{@date.to_s.gsub('-','/')}/#{permalink}")
|
39
40
|
end
|
40
41
|
|
41
42
|
get posts_path "/:year/:month/:day/:permalink/" do |year, month, day, permalink|
|
42
|
-
redirect
|
43
|
+
redirect path_to(:posts, year, month, day, permalink), 301
|
43
44
|
end
|
44
45
|
|
45
46
|
get archive_path do
|
46
|
-
"#{title_path :archive}:#{
|
47
|
+
"#{title_path :archive}:#{path_to :archive}"
|
47
48
|
end
|
48
49
|
|
49
50
|
get archive_path "/" do
|
50
|
-
redirect
|
51
|
+
redirect path_to(:archive), 301
|
51
52
|
end
|
52
53
|
|
53
54
|
get archive_path "/:year/:month/:day/:permalink" do |year, month, day, permalink|
|
54
|
-
"#{title_path :archive}:" +
|
55
|
+
"#{title_path :archive}:" + path_to(:archive, "#{@date.to_s.gsub('-','/')}/#{permalink}")
|
55
56
|
end
|
56
57
|
|
57
58
|
get archive_path "/:year/:month/:day/:permalink/" do |year, month, day, permalink|
|
58
|
-
redirect
|
59
|
+
redirect path_to(:archive, year, month, day, permalink), 301
|
59
60
|
end
|
60
61
|
|
61
62
|
get about_path do
|
62
|
-
"#{title_path :about}:#{
|
63
|
+
"#{title_path :about}:#{path_to :about}"
|
63
64
|
end
|
64
65
|
|
65
66
|
get about_path "/" do
|
66
|
-
redirect
|
67
|
+
redirect path_to(:about), 301
|
67
68
|
end
|
68
69
|
|
69
70
|
end
|
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: 0.1.
|
4
|
+
version: 0.1.1
|
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-
|
12
|
+
date: 2009-07-17 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|