-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from yaroslav0rudenok/master
Fixed tests, added 'in-route mapping'
- Loading branch information
Showing
12 changed files
with
206 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ | |
coverage | ||
rdoc | ||
pkg | ||
*.lock | ||
.rvmrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
source 'https://rubygems.org' | ||
|
||
group :test do | ||
gem 'sinatra' | ||
|
||
gem 'rspec' | ||
gem 'rack-test' | ||
|
||
gem 'jeweler' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.3.2 | ||
0.4.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
require 'sinatra/base' | ||
|
||
module Sinatra | ||
module Snap | ||
module Helpers | ||
def path_to path_name | ||
pattern = self.class.named_paths[path_name] | ||
raise ArgumentError.new("Unknown path ':#{path_name.to_s}'") if pattern == nil | ||
pattern.extend UrlBuilder | ||
end | ||
end | ||
|
||
def self.registered(app) | ||
app.helpers Snap::Helpers | ||
|
||
app.set :named_paths, {} | ||
end | ||
|
||
def paths(paths) | ||
paths.keys.each { | key | verify_type_of(key) } | ||
named_paths.merge!(paths) | ||
end | ||
|
||
alias_method :path, :paths | ||
|
||
def route(verb, path, options={}, &block) | ||
if path.kind_of? Hash | ||
raise ArgumentError.new("Can't register multiple paths") if path.length > 1 | ||
paths path | ||
path = path.values.first | ||
elsif path.kind_of? Symbol | ||
path = named_paths[path] | ||
end | ||
super verb, path, options, &block | ||
end | ||
|
||
def verify_type_of(name) | ||
raise ArgumentError.new('Path name must be a symbol') unless name.kind_of?(Symbol) | ||
end | ||
|
||
private :verify_type_of | ||
|
||
module UrlBuilder | ||
SPLAT = %r{(\*)} | ||
NAMED_PARAMETER = %r{/?(:\S+?)(?:/|$)} | ||
REGEXP_GROUP = %r{\(.+?\)} | ||
|
||
def with(*values) | ||
if self.instance_of? Regexp | ||
replace_regex_with(values) | ||
else | ||
replace_string_with(values) | ||
end | ||
end | ||
|
||
def replace_regex_with(values) | ||
perform_substitution_using(self.source, REGEXP_GROUP, values) | ||
end | ||
|
||
def perform_substitution_using(url, replacement_pattern, values) | ||
string = String.new(url) | ||
string.scan(replacement_pattern).each_with_index do | placeholder, index | | ||
string.sub!(Regexp.new(Regexp.escape(placeholder.first)), values[index].to_s) | ||
end | ||
string | ||
end | ||
|
||
def replace_string_with(values) | ||
replacement_pattern = route_defined_using_splat? ? SPLAT : NAMED_PARAMETER | ||
perform_substitution_using(self, replacement_pattern, values) | ||
end | ||
|
||
def route_defined_using_splat? | ||
SPLAT.match self | ||
end | ||
|
||
private :replace_regex_with, :replace_string_with, :route_defined_using_splat?, :perform_substitution_using | ||
end | ||
end | ||
|
||
register Snap | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,45 @@ | ||
# Generated by jeweler | ||
# DO NOT EDIT THIS FILE DIRECTLY | ||
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec' | ||
# -*- encoding: utf-8 -*- | ||
|
||
Gem::Specification.new do |s| | ||
s.name = %q{sinatra-snap} | ||
s.version = "0.3.2" | ||
s.name = "sinatra-snap" | ||
s.version = "0.4.0" | ||
|
||
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= | ||
s.authors = ["bcarlso"] | ||
s.date = %q{2010-03-13} | ||
s.email = %q{bcarlso@gmail.com} | ||
s.authors = ["bcarlso", "yaroslav0rudenok"] | ||
s.date = "2013-03-12" | ||
s.email = "bcarlso@gmail.com" | ||
s.extra_rdoc_files = [ | ||
"LICENSE", | ||
"README.rdoc" | ||
"README.rdoc" | ||
] | ||
s.files = [ | ||
".document", | ||
".gitignore", | ||
"LICENSE", | ||
"README.rdoc", | ||
"Rakefile", | ||
"VERSION", | ||
"examples/named_route_example.rb", | ||
"lib/sinatra-snap.rb", | ||
"lib/sinatra/named_path_support.rb", | ||
"sinatra-snap.gemspec", | ||
"spec/snap_spec.rb", | ||
"spec/spec_helper.rb" | ||
] | ||
s.homepage = %q{http://github.com/bcarlso/snap} | ||
s.rdoc_options = ["--charset=UTF-8"] | ||
s.require_paths = ["lib"] | ||
s.rubygems_version = %q{1.3.6} | ||
s.summary = %q{Sinatra NAmed Path support} | ||
s.test_files = [ | ||
"Gemfile", | ||
"LICENSE", | ||
"README.rdoc", | ||
"Rakefile", | ||
"VERSION", | ||
"examples/snap_example.rb", | ||
"lib/sinatra/snap.rb", | ||
"sinatra-snap.gemspec", | ||
"spec/snap_spec.rb", | ||
"spec/spec_helper.rb", | ||
"examples/named_route_example.rb" | ||
"spec/spec_helper.rb" | ||
] | ||
s.homepage = "http://github.com/bcarlso/snap" | ||
s.require_paths = ["lib"] | ||
s.rubygems_version = "1.8.25" | ||
s.summary = "Sinatra NAmed Path support" | ||
|
||
if s.respond_to? :specification_version then | ||
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION | ||
s.specification_version = 3 | ||
|
||
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then | ||
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then | ||
else | ||
end | ||
else | ||
end | ||
end | ||
|
Oops, something went wrong.