dzl 1.0.0.beta2 → 1.0.0.beta3

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.
data/config.ru CHANGED
@@ -35,7 +35,12 @@ end
35
35
  # run Dzl::Examples::FunWithHandlers
36
36
  # end
37
37
 
38
- require 'dzl/examples/fun_with_hooks'
38
+ # require 'dzl/examples/fun_with_hooks'
39
+ # map '/' do
40
+ # run Dzl::Examples::FunWithHooks
41
+ # end
42
+
43
+ require 'dzl/examples/fun_with_scopes'
39
44
  map '/' do
40
- run Dzl::Examples::FunWithHooks
45
+ run Dzl::Examples::FunWithScopes
41
46
  end
@@ -13,6 +13,13 @@ class Dzl::DSLProxies::Router < Dzl::DSLProxy
13
13
  end
14
14
  alias_method :parameters, :pblock
15
15
 
16
+ def scope(path)
17
+ raise ArgumentError unless block_given?
18
+ raise ArgumentError.new("scope must start with a '/'") unless path.starts_with?('/')
19
+
20
+ @subject.call_with_scope(Proc.new, path)
21
+ end
22
+
16
23
  def endpoint(route, *request_methods)
17
24
  request_methods = [:get] if request_methods.empty?
18
25
  request_methods.uniq!
@@ -22,7 +29,12 @@ class Dzl::DSLProxies::Router < Dzl::DSLProxy
22
29
  request_methods: request_methods
23
30
  }
24
31
 
25
- ept = Dzl::DSLSubjects::Endpoint.new(route, opts, @subject)
32
+ ept = Dzl::DSLSubjects::Endpoint.new(
33
+ [@subject.scope, route].join,
34
+ opts,
35
+ @subject
36
+ )
37
+
26
38
  @subject.add_endpoint(ept)
27
39
  @subject.call_with_subject(Proc.new, ept) if block_given?
28
40
  end
@@ -74,6 +74,6 @@ class Dzl::DSLSubjects::Endpoint < Dzl::DSLSubject
74
74
  route_part.starts_with?(':') ? "/.*?" : "/#{route_part}"
75
75
  end.push('$').join('')
76
76
 
77
- @route_regex = Regexp.new(route_regex_string)
77
+ @route_regex = Regexp.new('^' + route_regex_string)
78
78
  end
79
79
  end
@@ -9,6 +9,7 @@ class Dzl::DSLSubjects::Router < Dzl::DSLSubject
9
9
  @pblocks = {}
10
10
  @endpoints_by_route = {}
11
11
  @stack = []
12
+ @scope = []
12
13
  @defaults = {}
13
14
  @defaults_dslsub = Dzl::DSLSubjects::Defaults.new(self)
14
15
  @dsl_proxy = Dzl::DSLProxies::Router.new(self)
@@ -25,6 +26,16 @@ class Dzl::DSLSubjects::Router < Dzl::DSLSubject
25
26
  @stack.last
26
27
  end
27
28
 
29
+ def call_with_scope(proc, scope)
30
+ @scope.push(scope)
31
+ proc.call
32
+ @scope.pop
33
+ end
34
+
35
+ def scope
36
+ @scope.join
37
+ end
38
+
28
39
  def routes
29
40
  @endpoints_by_route.keys
30
41
  end
@@ -0,0 +1,7 @@
1
+ require 'dzl/dsl_proxies/scope'
2
+
3
+ class Dzl::DSLSubjects::Scope < Dzl::DSLSubject
4
+ def initialize(path)
5
+ @path = path
6
+ end
7
+ end
@@ -0,0 +1,27 @@
1
+ require 'dzl/examples/base'
2
+
3
+ class Dzl::Examples::FunWithScopes < Dzl::Examples::Base
4
+ scope '/foo' do
5
+ get '/bar'
6
+ get '/baz'
7
+ end
8
+
9
+ scope '/bar' do
10
+ get '/foo'
11
+ get '/baz'
12
+ end
13
+
14
+ scope '/nest' do
15
+ scope '/this' do
16
+ get '/first'
17
+ end
18
+
19
+ get '/and'
20
+
21
+ scope '/that' do
22
+ get '/second'
23
+ end
24
+ end
25
+
26
+ get '/zoom'
27
+ end
data/lib/dzl/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dzl
2
- VERSION = "1.0.0.beta2"
2
+ VERSION = "1.0.0.beta3"
3
3
  end
@@ -115,6 +115,12 @@ describe Dzl::Examples::FunWithParams do
115
115
  end
116
116
  end
117
117
 
118
+ describe '/bar' do
119
+ it 'should 404 on /bar/anything' do
120
+ get('/bar/foo') {|r| r.status.should == 404}
121
+ end
122
+ end
123
+
118
124
  describe '/protected' do
119
125
  it 'should present http basic challenge with no credentials' do
120
126
  get '/protected' do |response|
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+ require 'rack/test'
3
+ require 'dzl/examples/fun_with_scopes'
4
+
5
+ describe 'scopes' do
6
+ include Rack::Test::Methods
7
+ def app; Dzl::Examples::FunWithScopes; end
8
+
9
+ it 'make routes available under the specified path' do
10
+ get('/foo/bar') { |r| r.status.should == 200 }
11
+ get('/foo/foo') { |r| r.status.should == 404 }
12
+ get('/bar/foo') { |r| r.status.should == 200 }
13
+ get('/zoom') { |r| r.status.should == 200 }
14
+ end
15
+
16
+ it 'can be nested' do
17
+ ['/nest/this/first', '/nest/and', '/nest/that/second'].each do |route|
18
+ get(route) { |r| r.status.should == 200 }
19
+ end
20
+
21
+ ['/nest/that/first', '/nest/this/and', '/nest/this/and/that', '/nest/that/first'].each do |route|
22
+ get(route) { |r| r.status.should == 404 }
23
+ end
24
+ end
25
+
26
+ it 'must start with a slash' do
27
+ expect {
28
+ class Dzl::Examples::FunWithScopes
29
+ scope 'not-a-slash' do
30
+ two = 1 + 1
31
+ end
32
+ end
33
+ }.to raise_error(ArgumentError)
34
+ end
35
+
36
+ it "don't brake pblocks" do
37
+ # pblocks aren't scoped
38
+ expect {
39
+ class Dzl::Examples::FunWithScopes
40
+ scope '/whatever' do
41
+ pblock :foo do
42
+ required :foo
43
+ end
44
+
45
+ get '/foo' do
46
+ import_pblock :foo
47
+ end
48
+ end
49
+
50
+ scope '/another_scope' do
51
+ get '/foo' do
52
+ import_pblock :foo
53
+ end
54
+ end
55
+ end
56
+ }.to_not raise_error
57
+
58
+ get('/another_scope/foo') do |response|
59
+ response.status.should == 404
60
+ JSON.parse(response.body)['errors']['/another_scope/foo'].should == {
61
+ 'foo' => 'missing_required_param'
62
+ }
63
+ end
64
+ end
65
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dzl
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.beta2
4
+ version: 1.0.0.beta3
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-03-20 00:00:00.000000000 Z
13
+ date: 2012-03-21 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rack
17
- requirement: &70297835742860 !ruby/object:Gem::Requirement
17
+ requirement: &70140311025420 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 1.4.1
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70297835742860
25
+ version_requirements: *70140311025420
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: activesupport
28
- requirement: &70297835740780 !ruby/object:Gem::Requirement
28
+ requirement: &70140311024920 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ~>
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: 3.2.2
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *70297835740780
36
+ version_requirements: *70140311024920
37
37
  description: Small, fast racktivesupport web framework with handy DSL and explicit
38
38
  parameter validation.
39
39
  email:
@@ -74,12 +74,14 @@ files:
74
74
  - lib/dzl/dsl_subjects/parameter_block.rb
75
75
  - lib/dzl/dsl_subjects/protection.rb
76
76
  - lib/dzl/dsl_subjects/router.rb
77
+ - lib/dzl/dsl_subjects/scope.rb
77
78
  - lib/dzl/errors.rb
78
79
  - lib/dzl/examples/base.rb
79
80
  - lib/dzl/examples/fun_with_handlers.rb
80
81
  - lib/dzl/examples/fun_with_hooks.rb
81
82
  - lib/dzl/examples/fun_with_params.rb
82
83
  - lib/dzl/examples/fun_with_requests.rb
84
+ - lib/dzl/examples/fun_with_scopes.rb
83
85
  - lib/dzl/examples/route_profile.rb
84
86
  - lib/dzl/examples/trey.rb
85
87
  - lib/dzl/logger.rb
@@ -99,6 +101,7 @@ files:
99
101
  - spec/fun_with_hooks_spec.rb
100
102
  - spec/fun_with_params_spec.rb
101
103
  - spec/fun_with_requests_spec.rb
104
+ - spec/fun_with_scopes_spec.rb
102
105
  - spec/logger_spec.rb
103
106
  - spec/route_params_spec.rb
104
107
  - spec/router_doc_spec.rb
@@ -136,6 +139,7 @@ test_files:
136
139
  - spec/fun_with_hooks_spec.rb
137
140
  - spec/fun_with_params_spec.rb
138
141
  - spec/fun_with_requests_spec.rb
142
+ - spec/fun_with_scopes_spec.rb
139
143
  - spec/logger_spec.rb
140
144
  - spec/route_params_spec.rb
141
145
  - spec/router_doc_spec.rb