Skip to content

Commit

Permalink
Merge pull request #2 from yaroslav0rudenok/master
Browse files Browse the repository at this point in the history
Fixed tests, added 'in-route mapping'
  • Loading branch information
bcarlso committed Mar 15, 2013
2 parents 0cfaa8f + 95bcb29 commit 6551676
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 157 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
coverage
rdoc
pkg
*.lock
.rvmrc
10 changes: 10 additions & 0 deletions Gemfile
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
6 changes: 5 additions & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ Then require it in your application and use away!

require 'rubygems'
require 'sinatra'
require 'sinatra-snap'
require 'sinatra/snap'

paths :add => '/add/:addend/:augend',
:sum => '/sum/:addend/:augend',
:subtract => '/subtract/*/*',
:difference => %r{/difference/(\d+)/(\d+)}

get :multiply => '/multiply/:one/:two' do |one, two|
"#{one.to_i * two.to_i}"
end

get :add do
redirect path_to(:sum).with(params[:addend], params[:augend])
end
Expand Down
18 changes: 4 additions & 14 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,20 @@ begin
gem.summary = "Sinatra NAmed Path support"
gem.email = "bcarlso@gmail.com"
gem.homepage = "http://github.com/bcarlso/snap"
gem.authors = ["bcarlso"]
gem.authors = ["bcarlso", "yaroslav0rudenok"]
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end

rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
end

require 'spec/rake/spectask'
Spec::Rake::SpecTask.new(:spec) do |spec|
spec.libs << 'lib' << 'spec'
spec.spec_files = FileList['spec/**/*_spec.rb']
end

Spec::Rake::SpecTask.new(:rcov) do |spec|
spec.libs << 'lib' << 'spec'
spec.pattern = 'spec/**/*_spec.rb'
spec.rcov = true
end

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)

task :default => :spec

require 'rake/rdoctask'
require 'rdoc/task'
Rake::RDocTask.new do |rdoc|
if File.exist?('VERSION.yml')
config = YAML.load(File.read('VERSION.yml'))
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.2
0.4.0
7 changes: 5 additions & 2 deletions examples/named_route_example.rb → examples/snap_example.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require 'rubygems'
require 'sinatra'
require 'sinatra-snap'
require 'sinatra/snap'

path :home => '/index'
paths :add => '/add/:augend/:addend',
Expand All @@ -12,6 +11,10 @@
"Hello World!"
end

get :multiply => '/multiply/:one/:two' do |one, two|
"#{one.to_i * two.to_i}"
end

get :add do
redirect(path_to(:sum).with(params[:augend], params[:addend]))
end
Expand Down
1 change: 0 additions & 1 deletion lib/sinatra-snap.rb

This file was deleted.

81 changes: 0 additions & 81 deletions lib/sinatra/named_path_support.rb

This file was deleted.

82 changes: 82 additions & 0 deletions lib/sinatra/snap.rb
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
52 changes: 24 additions & 28 deletions sinatra-snap.gemspec
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

Loading

0 comments on commit 6551676

Please sign in to comment.