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 CHANGED
@@ -1,5 +1,10 @@
1
1
  = Changes
2
2
 
3
+ [0.1.1 - July 2009]
4
+ * New helper methods have been added.
5
+
6
+ * Improvements in map method.
7
+
3
8
  [0.1.0 - July 2009]
4
9
  * This version has been extracted from
5
10
  {Postview}[http://postview.rubyforge.org] project.
data/README CHANGED
@@ -1,4 +1,10 @@
1
- = Sinatra Mapping - Map easily URLs paths
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 anything for root path.
40
+ # do something for root path.
35
41
  end
36
42
 
37
43
  get posts_path do
38
44
  # /blog/articles
39
- # do anything for posts path.
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 anything for posts path using parameters for find a post.
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 anything for tags path.
55
+ # do something for tags path.
50
56
  end
51
57
 
52
58
  get archive_path do
53
- # do anything for archive path.
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 anything for archive path using parameters for find a post.
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 which
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
- if current and version_date
13
- newer = {
14
- :major => version[:major].to_i || current[:major].to_i,
15
- :minor => version[:minor].to_i || current[:minor].to_i,
16
- :patch => version[:patch].to_i || current[:patch].to_i,
17
- :release => version[:release].to_s.empty? ? nil : version[:release].to_i,
18
- :date => version_date || Date.today,
19
- :cycle => version[:cycle] || current[:cycle]
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
- newer.merge(current) do |key, new_value, current_value|
23
- new_value || current_value
24
- end
21
+ newer.merge(current) do |key, new_value, current_value|
22
+ new_value || current_value
23
+ end
25
24
 
26
- File.open(taskspec.name.upcase, "w") do |version_file|
27
- version_file << newer.to_yaml
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 - Mapping"
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
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  :release:
3
- :date: 2009-07-15
3
+ :date: 2009-07-17
4
4
  :cycle: Development version, pre-alpha
5
5
  :major: 0
6
6
  :minor: 1
7
- :patch: 0
7
+ :patch: 1
@@ -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
- @root_path = cleanup_paths("/#{path}/")
25
+ @locations[:root] = cleanup_paths("/#{path}/")
27
26
  metadef "#{name}_path" do |*paths|
28
- @root_path
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
- path_to(@locations[name.to_sym], *paths)
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 path_to(*args)
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 maps written using
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
- # Create title using a path mapped. Other else, return just arguments
8
- # joined by spaces and capitalise.
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/archive
17
+ map :archive, "archive/articles" # archive_path => /archive/articles
17
18
  map :about # about_path => /about
18
19
 
19
- helpers Sinatra::MappingHelpers
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}:#{options.root_path}"
27
+ "#{title_path :root}:#{path_to :root}"
27
28
  end
28
29
 
29
30
  get posts_path do
30
- "#{title_path :posts}:#{options.posts_path}"
31
+ "#{title_path :posts}:#{path_to :posts}"
31
32
  end
32
33
 
33
34
  get posts_path "/" do
34
- redirect options.posts_path, 301
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}:" + options.posts_path("#{@date.to_s.gsub('-','/')}/#{permalink}")
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 options.posts_path(year, month, day, permalink), 301
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}:#{options.archive_path}"
47
+ "#{title_path :archive}:#{path_to :archive}"
47
48
  end
48
49
 
49
50
  get archive_path "/" do
50
- redirect options.archive_path, 301
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}:" + options.archive_path("#{@date.to_s.gsub('-','/')}/#{permalink}")
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 options.archive_path(year, month, day, permalink), 301
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}:#{options.about_path}"
63
+ "#{title_path :about}:#{path_to :about}"
63
64
  end
64
65
 
65
66
  get about_path "/" do
66
- redirect options.about_path, 301
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.0
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-15 00:00:00 -04:00
12
+ date: 2009-07-17 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency