strikeroff-routing-filter 0.0.2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,31 +1,31 @@
1
- module RoutingFilter
2
- class Base
3
- class_inheritable_accessor :active
4
- self.active = true
5
-
6
- attr_accessor :chain, :options
7
-
8
- def initialize(options = {})
9
- @options = options
10
- options.each { |name, value| instance_variable_set :"@#{name}", value }
11
- end
12
-
13
- def run(method, *args, &block)
14
- _next = successor ? lambda { successor.run(method, *args, &block) } : block
15
- active ? send(method, *args, &_next) : _next.call(*args)
16
- end
17
-
18
- def run_reverse(method, *args, &block)
19
- _prev = predecessor ? lambda { predecessor.run(method, *args, &block) } : block
20
- active ? send(method, *args, &_prev) : _prev.call(*args)
21
- end
22
-
23
- def predecessor
24
- @chain.predecessor(self)
25
- end
26
-
27
- def successor
28
- @chain.successor(self)
29
- end
30
- end
1
+ module RoutingFilter
2
+ class Base
3
+ class_inheritable_accessor :active
4
+ self.active = true
5
+
6
+ attr_accessor :chain, :options
7
+
8
+ def initialize(options = {})
9
+ @options = options
10
+ options.each { |name, value| instance_variable_set :"@#{name}", value }
11
+ end
12
+
13
+ def run(method, *args, &block)
14
+ _next = successor ? lambda { successor.run(method, *args, &block) } : block
15
+ active ? send(method, *args, &_next) : _next.call(*args)
16
+ end
17
+
18
+ def run_reverse(method, *args, &block)
19
+ _prev = predecessor ? lambda { predecessor.run(method, *args, &block) } : block
20
+ active ? send(method, *args, &_prev) : _prev.call(*args)
21
+ end
22
+
23
+ def predecessor
24
+ @chain.predecessor(self)
25
+ end
26
+
27
+ def successor
28
+ @chain.successor(self)
29
+ end
30
+ end
31
31
  end
@@ -1,57 +1,57 @@
1
- require 'routing_filter/base'
2
-
3
- module RoutingFilter
4
- class ForceExtension < Base
5
- attr_reader :extension, :exclude
6
-
7
- def initialize(*args)
8
- super
9
- @extension ||= 'html'
10
- @exclude = %r(^(http.?://[^/]+)?\/?$) if @exclude.nil?
11
- end
12
-
13
- def around_recognize(path, env, &block)
14
- extract_extension!(path) unless excluded?(path)
15
- yield(path, env)
16
- end
17
-
18
- def around_generate(*args, &block)
19
- returning yield do |result|
20
- url = result.is_a?(Array) ? result.first : result
21
- append_extension!(url) if append_extension?(url)
22
- end
23
- end
24
-
25
- protected
26
-
27
- def extract_extension!(path)
28
- path.sub! /\.#{extension}$/, ''
29
- $1
30
- end
31
-
32
- def append_extension?(url)
33
- !(url.blank? || excluded?(url) || mime_extension?(url))
34
- end
35
-
36
- def excluded?(url)
37
- case exclude
38
- when Regexp
39
- url =~ exclude
40
- when Proc
41
- exclude.call(url)
42
- end
43
- end
44
-
45
- def mime_extension?(url)
46
- url =~ /\.#{Mime::EXTENSION_LOOKUP.keys.join('|')}(\?|$)/
47
- end
48
-
49
- def append_extension!(url)
50
- url.replace url.sub(/(\?|$)/, ".#{extension}\\1")
51
- end
52
-
53
- def append_page!(url, page)
54
- url.replace "#{url}/pages/#{page}"
55
- end
56
- end
1
+ require 'routing_filter/base'
2
+
3
+ module RoutingFilter
4
+ class ForceExtension < Base
5
+ attr_reader :extension, :exclude
6
+
7
+ def initialize(*args)
8
+ super
9
+ @extension ||= 'html'
10
+ @exclude = %r(^(http.?://[^/]+)?\/?$) if @exclude.nil?
11
+ end
12
+
13
+ def around_recognize(path, env, &block)
14
+ extract_extension!(path) unless excluded?(path)
15
+ yield(path, env)
16
+ end
17
+
18
+ def around_generate(*args, &block)
19
+ returning yield do |result|
20
+ url = result.is_a?(Array) ? result.first : result
21
+ append_extension!(url) if append_extension?(url)
22
+ end
23
+ end
24
+
25
+ protected
26
+
27
+ def extract_extension!(path)
28
+ path.sub! /\.#{extension}$/, ''
29
+ $1
30
+ end
31
+
32
+ def append_extension?(url)
33
+ !(url.blank? || excluded?(url) || mime_extension?(url))
34
+ end
35
+
36
+ def excluded?(url)
37
+ case exclude
38
+ when Regexp
39
+ url =~ exclude
40
+ when Proc
41
+ exclude.call(url)
42
+ end
43
+ end
44
+
45
+ def mime_extension?(url)
46
+ url =~ /\.#{Mime::EXTENSION_LOOKUP.keys.join('|')}(\?|$)/
47
+ end
48
+
49
+ def append_extension!(url)
50
+ url.replace url.sub(/(\?|$)/, ".#{extension}\\1")
51
+ end
52
+
53
+ def append_page!(url, page)
54
+ url.replace "#{url}/pages/#{page}"
55
+ end
56
+ end
57
57
  end
@@ -1,82 +1,82 @@
1
- require 'i18n'
2
- require 'routing_filter/base'
3
-
4
- module RoutingFilter
5
- class Locale < Base
6
- @@include_default_locale = true
7
- cattr_writer :include_default_locale
8
-
9
- class << self
10
- def include_default_locale?
11
- @@include_default_locale
12
- end
13
-
14
- def locales
15
- @@locales ||= I18n.available_locales.map(&:to_sym)
16
- end
17
-
18
- def locales=(locales)
19
- @@locales = locales.map(&:to_sym)
20
- end
21
-
22
- def locales_pattern
23
- @@locales_pattern ||= %r(^/(#{self.locales.map { |l| Regexp.escape(l.to_s) }.join('|')})(?=/|$))
24
- end
25
-
26
- def extract_locale!(path)
27
- path.sub! self.locales_pattern, ''
28
- $1
29
- end
30
-
31
- def prepend_locale!(url, locale)
32
- url.sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{locale}#{$2}" }
33
- end
34
- end
35
-
36
- def around_recognize(path, env, &block)
37
- locale = self.class.extract_locale!(path) # remove the locale from the beginning of the path
38
- returning yield do |params| # invoke the given block (calls more filters and finally routing)
39
- params[:locale] = locale if locale # set recognized locale to the resulting params hash
40
- end
41
- end
42
-
43
- def around_generate(*args, &block)
44
- options = args.extract_options!
45
-
46
- locale = options.delete(:locale)
47
- unless skip_prepend_locale? options
48
- locale = I18n.locale if locale.nil? # default to I18n.locale when locale is nil (could also be false)
49
- locale = nil unless valid_locale?(locale) # reset to no locale when locale is not valid
50
- returning yield do |result|
51
- if locale && prepend_locale?(locale)
52
- url = result.is_a?(Array) ? result.first : result
53
- self.class.prepend_locale!(url, locale)
54
- end
55
- end
56
- else
57
- yield
58
- end
59
- end
60
-
61
- protected
62
-
63
-
64
- def prepend_locale?(locale)
65
- self.class.include_default_locale? || !default_locale?(locale)
66
- end
67
-
68
- def skip_prepend_locale?(options)
69
- !!options[:scip_locale_filter]
70
- end
71
-
72
- def valid_locale?(locale)
73
- locale && self.class.locales.include?(locale.to_sym)
74
- end
75
-
76
- def default_locale?(locale)
77
- locale && locale.to_sym == I18n.default_locale.to_sym
78
- end
79
-
80
-
81
- end
82
- end
1
+ require 'i18n'
2
+ require 'routing_filter/base'
3
+
4
+ module RoutingFilter
5
+ class Locale < Base
6
+ @@include_default_locale = true
7
+ cattr_writer :include_default_locale
8
+
9
+ class << self
10
+ def include_default_locale?
11
+ @@include_default_locale
12
+ end
13
+
14
+ def locales
15
+ @@locales ||= I18n.available_locales.map(&:to_sym)
16
+ end
17
+
18
+ def locales=(locales)
19
+ @@locales = locales.map(&:to_sym)
20
+ end
21
+
22
+ def locales_pattern
23
+ @@locales_pattern ||= %r(^/(#{self.locales.map { |l| Regexp.escape(l.to_s) }.join('|')})(?=/|$))
24
+ end
25
+
26
+ def extract_locale!(path)
27
+ path.sub! self.locales_pattern, ''
28
+ $1
29
+ end
30
+
31
+ def prepend_locale!(url, locale)
32
+ url.sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{locale}#{$2}" }
33
+ end
34
+ end
35
+
36
+ def around_recognize(path, env, &block)
37
+ locale = self.class.extract_locale!(path) # remove the locale from the beginning of the path
38
+ returning yield do |params| # invoke the given block (calls more filters and finally routing)
39
+ params[:locale] = locale if locale # set recognized locale to the resulting params hash
40
+ end
41
+ end
42
+
43
+ def around_generate(*args, &block)
44
+ options = args.extract_options!
45
+
46
+ locale = options.delete(:locale)
47
+ unless skip_prepend_locale? options
48
+ locale = I18n.locale if locale.nil? # default to I18n.locale when locale is nil (could also be false)
49
+ locale = nil unless valid_locale?(locale) # reset to no locale when locale is not valid
50
+ returning yield do |result|
51
+ if locale && prepend_locale?(locale)
52
+ url = result.is_a?(Array) ? result.first : result
53
+ self.class.prepend_locale!(url, locale)
54
+ end
55
+ end
56
+ else
57
+ yield
58
+ end
59
+ end
60
+
61
+ protected
62
+
63
+
64
+ def prepend_locale?(locale)
65
+ self.class.include_default_locale? || !default_locale?(locale)
66
+ end
67
+
68
+ def skip_prepend_locale?(options)
69
+ !!options[:scip_locale_filter]
70
+ end
71
+
72
+ def valid_locale?(locale)
73
+ locale && self.class.locales.include?(locale.to_sym)
74
+ end
75
+
76
+ def default_locale?(locale)
77
+ locale && locale.to_sym == I18n.default_locale.to_sym
78
+ end
79
+
80
+
81
+ end
82
+ end
@@ -1,33 +1,33 @@
1
- require 'routing_filter/base'
2
-
3
- module RoutingFilter
4
- class Pagination < Base
5
- def around_recognize(path, env, &block)
6
- page = extract_page!(path)
7
- returning yield(path, env) do |params|
8
- params[:page] = page.to_i if page
9
- end
10
- end
11
-
12
- def around_generate(*args, &block)
13
- page = args.extract_options!.delete(:page)
14
- returning yield do |result|
15
- if page && page != 1
16
- url = result.is_a?(Array) ? result.first : result
17
- append_page!(url, page)
18
- end
19
- end
20
- end
21
-
22
- protected
23
-
24
- def extract_page!(path)
25
- path.sub! %r(/pages/([\d]+)/?$), ''
26
- $1
27
- end
28
-
29
- def append_page!(url, page)
30
- url.sub!(/($|\?)/) { "/pages/#{page}#{$1}" }
31
- end
32
- end
1
+ require 'routing_filter/base'
2
+
3
+ module RoutingFilter
4
+ class Pagination < Base
5
+ def around_recognize(path, env, &block)
6
+ page = extract_page!(path)
7
+ returning yield(path, env) do |params|
8
+ params[:page] = page.to_i if page
9
+ end
10
+ end
11
+
12
+ def around_generate(*args, &block)
13
+ page = args.extract_options!.delete(:page)
14
+ returning yield do |result|
15
+ if page && page != 1
16
+ url = result.is_a?(Array) ? result.first : result
17
+ append_page!(url, page)
18
+ end
19
+ end
20
+ end
21
+
22
+ protected
23
+
24
+ def extract_page!(path)
25
+ path.sub! %r(/pages/([\d]+)/?$), ''
26
+ $1
27
+ end
28
+
29
+ def append_page!(url, page)
30
+ url.sub!(/($|\?)/) { "/pages/#{page}#{$1}" }
31
+ end
32
+ end
33
33
  end
@@ -0,0 +1,33 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{routing_filter}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Vesov Ilya"]
9
+ s.date = %q{2009-10-18}
10
+ s.description = %q{routing_filter}
11
+ s.email = %q{strikeroff@gmail.com}
12
+ s.extra_rdoc_files = ["lib/routing_filter/base.rb", "lib/routing_filter/force_extension.rb", "lib/routing_filter/locale.rb", "lib/routing_filter/pagination.rb", "lib/routing_filter.rb"]
13
+ s.files = ["init.rb", "lib/routing_filter/base.rb", "lib/routing_filter/force_extension.rb", "lib/routing_filter/locale.rb", "lib/routing_filter/pagination.rb", "lib/routing_filter.rb", "Manifest", "MIT-LICENSE", "Rakefile", "README.markdown", "routing-filter.gemspec", "routing_filter.gemspec", "spec/force_extension_spec.rb", "spec/generation_spec.rb", "spec/pagination_extension_spec.rb", "spec/recognition_spec.rb", "spec/routing_filter_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "VERSION"]
14
+ s.homepage = %q{http://7studio.ru}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Routing_filter", "--main", "README.markdown", "-c utf-8"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{routing_filter}
18
+ s.rubygems_version = %q{1.3.5}
19
+ s.summary = %q{routing_filter}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
26
+ s.add_development_dependency(%q<echoe>, [">= 0"])
27
+ else
28
+ s.add_dependency(%q<echoe>, [">= 0"])
29
+ end
30
+ else
31
+ s.add_dependency(%q<echoe>, [">= 0"])
32
+ end
33
+ end
@@ -1,65 +1,65 @@
1
- require File.dirname(__FILE__) + '/spec_helper.rb'
2
-
3
- describe 'RoutingFilter::ForceExtension' do
4
- include RoutingFilterHelpers
5
-
6
- before :each do
7
- setup_environment(:force_extension)
8
- end
9
-
10
- describe 'url recognition' do
11
- it 'recognizes the path /sections/1.html' do
12
- should_recognize_path '/sections/1.html', @section_params
13
- end
14
-
15
- it 'recognizes the path /sections/1/articles/1.html' do
16
- should_recognize_path '/sections/1/articles/1.html', @article_params
17
- end
18
-
19
- it 'does not recognize the path /sections/1/articles/1.foobar' do
20
- lambda { @set.recognize_path('/sections/1/articles/1.foobar', {}) }.should raise_error(ActionController::RoutingError)
21
- end
22
- end
23
-
24
- describe 'url generation' do
25
- it 'appends the .html extension to generated paths (section_path)' do
26
- section_path(:id => 1).should == '/sections/1.html'
27
- end
28
-
29
- it 'appends the .html extension to generated paths (section_article_path)' do
30
- section_article_path(:section_id => 1, :id => 1).should == '/sections/1/articles/1.html'
31
- end
32
-
33
- it 'appends the .html extension to generated paths (admin_articles_path)' do
34
- admin_articles_path.should == '/admin/articles.html'
35
- end
36
-
37
- it 'does not replace or add on an existing extension' do
38
- section_path(:id => 1, :format => 'xml').should == '/sections/1.xml'
39
- end
40
-
41
- it 'works with url query params' do
42
- section_path(:id => 1, :foo => 'bar').should == '/sections/1.html?foo=bar'
43
- end
44
-
45
- it 'excludes / by default' do
46
- home_path.should == '/'
47
- end
48
-
49
- it 'excludes http://test.host/ by default' do
50
- home_url.should == 'http://test.host/'
51
- end
52
-
53
- it 'excludes with custom regexp' do
54
- setup_environment { |map| map.filter :force_extension, :exclude => %r(^/(admin|$)) }
55
- home_path.should == '/'
56
- admin_articles_path.should == '/admin/articles'
57
- section_path(:id => 1).should == '/sections/1.html'
58
- end
59
-
60
- it 'does not exclude / when :exclude => false was passed' do
61
- setup_environment { |map| map.filter :force_extension, :exclude => false }
62
- home_path.should == '/.html'
63
- end
64
- end
65
- end
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe 'RoutingFilter::ForceExtension' do
4
+ include RoutingFilterHelpers
5
+
6
+ before :each do
7
+ setup_environment(:force_extension)
8
+ end
9
+
10
+ describe 'url recognition' do
11
+ it 'recognizes the path /sections/1.html' do
12
+ should_recognize_path '/sections/1.html', @section_params
13
+ end
14
+
15
+ it 'recognizes the path /sections/1/articles/1.html' do
16
+ should_recognize_path '/sections/1/articles/1.html', @article_params
17
+ end
18
+
19
+ it 'does not recognize the path /sections/1/articles/1.foobar' do
20
+ lambda { @set.recognize_path('/sections/1/articles/1.foobar', {}) }.should raise_error(ActionController::RoutingError)
21
+ end
22
+ end
23
+
24
+ describe 'url generation' do
25
+ it 'appends the .html extension to generated paths (section_path)' do
26
+ section_path(:id => 1).should == '/sections/1.html'
27
+ end
28
+
29
+ it 'appends the .html extension to generated paths (section_article_path)' do
30
+ section_article_path(:section_id => 1, :id => 1).should == '/sections/1/articles/1.html'
31
+ end
32
+
33
+ it 'appends the .html extension to generated paths (admin_articles_path)' do
34
+ admin_articles_path.should == '/admin/articles.html'
35
+ end
36
+
37
+ it 'does not replace or add on an existing extension' do
38
+ section_path(:id => 1, :format => 'xml').should == '/sections/1.xml'
39
+ end
40
+
41
+ it 'works with url query params' do
42
+ section_path(:id => 1, :foo => 'bar').should == '/sections/1.html?foo=bar'
43
+ end
44
+
45
+ it 'excludes / by default' do
46
+ home_path.should == '/'
47
+ end
48
+
49
+ it 'excludes http://test.host/ by default' do
50
+ home_url.should == 'http://test.host/'
51
+ end
52
+
53
+ it 'excludes with custom regexp' do
54
+ setup_environment { |map| map.filter :force_extension, :exclude => %r(^/(admin|$)) }
55
+ home_path.should == '/'
56
+ admin_articles_path.should == '/admin/articles'
57
+ section_path(:id => 1).should == '/sections/1.html'
58
+ end
59
+
60
+ it 'does not exclude / when :exclude => false was passed' do
61
+ setup_environment { |map| map.filter :force_extension, :exclude => false }
62
+ home_path.should == '/.html'
63
+ end
64
+ end
65
+ end