sinatra-trails 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -25,7 +25,8 @@ module Sinatra
25
25
  @to_route = "/#{@components.join('/')}"
26
26
  namespace = ancestors.reverse.find { |ancestor| ancestor.class == Scope && ancestor.name }
27
27
 
28
- @to_regexp, @keys = Sinatra::Base.send(:compile, to_route)
28
+ @to_regexp, @keys = Sinatra::Base.send :compile, @to_route
29
+
29
30
  add_param 'resource', scope.name if [Resource, Resources].include?(scope.class)
30
31
  add_param 'namespace', namespace.name if namespace
31
32
  add_param 'action', name
@@ -55,26 +56,38 @@ module Sinatra
55
56
 
56
57
  private
57
58
  def add_param key, capture
58
- unless keys.include? key
59
- @keys << key
60
- @captures << capture.to_s
61
- end
59
+ @keys << key
60
+ @captures << capture.to_s
62
61
  end
63
62
  end
64
63
 
64
+ # This is hackish
65
65
  class ScopeMatcher
66
66
  def initialize scope, matchers
67
67
  @scope = scope
68
- @names, @matchers = matchers.partition { |el| Symbol === el }
68
+ @names, @routes = matchers.partition { |el| Symbol === el }
69
69
  end
70
70
 
71
71
  def match str
72
- if @matchers.empty? && @names.empty?
73
- Regexp.union(@scope.routes).match str
74
- else
75
- Regexp.union(*@matchers, *@names.map{ |name| @scope[name] }).match str
76
- end
72
+ Regexp.union(matchers).match str
73
+ end
74
+
75
+ def matchers
76
+ @matchers ||=
77
+ if @routes.empty? && @names.empty?
78
+ @scope.routes
79
+ else
80
+ @routes + @names.map { |name| @scope[name] }
81
+ end
77
82
  end
83
+
84
+ def actual_keys
85
+ @keys ||= matchers.map{ |m| m.keys }.flatten
86
+ end
87
+
88
+ def keys() self end
89
+ def any?() actual_keys.any? end
90
+ def zip(arr) actual_keys.zip(arr) end
78
91
  end
79
92
 
80
93
  class Scope
@@ -1,5 +1,5 @@
1
1
  module Sinatra
2
2
  module Trails
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -490,6 +490,28 @@ describe 'trails' do
490
490
  end
491
491
  end
492
492
 
493
+ describe 'before filter costructing correct params' do
494
+ before :all do
495
+ app.instance_eval do
496
+ resources :users do
497
+ before(user) do
498
+ # puts params
499
+ @user = params[:id]
500
+ end
501
+
502
+ get user do
503
+ @user
504
+ end
505
+ end
506
+ end
507
+ end
508
+
509
+ it 'should set before filter for passed routes' do
510
+ get '/users/1'
511
+ last_response.body.should == '1'
512
+ end
513
+ end
514
+
493
515
  describe 'having access to resource name' do
494
516
  before :all do
495
517
  app.resources(:users => :posts) do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-trails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-01 00:00:00.000000000Z
12
+ date: 2011-10-04 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sinatra
16
- requirement: &2162183940 !ruby/object:Gem::Requirement
16
+ requirement: &2158241920 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2162183940
24
+ version_requirements: *2158241920
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: i18n
27
- requirement: &2162207320 !ruby/object:Gem::Requirement
27
+ requirement: &2158241500 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2162207320
35
+ version_requirements: *2158241500
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activesupport
38
- requirement: &2162206820 !ruby/object:Gem::Requirement
38
+ requirement: &2158241000 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '3.0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *2162206820
46
+ version_requirements: *2158241000
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
- requirement: &2162206400 !ruby/object:Gem::Requirement
49
+ requirement: &2158240580 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2162206400
57
+ version_requirements: *2158240580
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rspec
60
- requirement: &2162205940 !ruby/object:Gem::Requirement
60
+ requirement: &2158263920 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2162205940
68
+ version_requirements: *2158263920
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rack-test
71
- requirement: &2162205520 !ruby/object:Gem::Requirement
71
+ requirement: &2158263500 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *2162205520
79
+ version_requirements: *2158263500
80
80
  description: A named routes Sinatra DSL inspired by Rails routing
81
81
  email:
82
82
  - macarui@gmail.com