bebop 0.1.2 → 0.1.3

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,16 +1,16 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'bebop'
3
- s.version = '0.1.2'
3
+ s.version = '0.1.3'
4
4
  s.date = '2010-1-10'
5
-
5
+
6
6
  s.summary = s.description = "A small Sinatra/Monk extension for resource routing"
7
-
7
+
8
8
  s.authors = ["John Bender"]
9
9
  s.homepage = 'http://github.com/johnbender/bebop'
10
10
  s.email = 'john.m.bender@gmail.com'
11
-
11
+
12
12
  # = MANIFEST =
13
- s.files = %w[
13
+ s.files = %w[
14
14
  README.markdown
15
15
  Rakefile
16
16
  bebop.gemspec
@@ -20,12 +20,12 @@ lib/bebop/ext.rb
20
20
  lib/bebop.rb
21
21
  spec/bebop_spec.rb
22
22
  ]
23
- # = MANIFEST =
23
+ # = MANIFEST =
24
24
  s.add_dependency 'sinatra', '>= 0.9.4'
25
25
  s.add_dependency 'activesupport', '>= 2.3.5'
26
26
 
27
27
  s.add_development_dependency 'rspec', '>=1.2.9'
28
-
28
+
29
29
  s.has_rdoc = false
30
30
  s.require_paths = %w[lib]
31
31
  s.rubyforge_project = 'bebop'
@@ -1,19 +1,19 @@
1
1
  require File.join(File.dirname(__FILE__), '..', 'lib', 'bebop')
2
- require 'sinatra'
2
+ require 'sinatra/base'
3
3
 
4
4
  class MyApp < Sinatra::Base
5
5
  register Bebop
6
6
 
7
7
  resource :foos do |foos|
8
- # Bebop provides some simple targeting for before and after filters, with the
9
- # caveat that they must be defined before the routes they target within the resource block
8
+ # Bebop provides some simple targeting for before and after filters, with the
9
+ # caveat that they must be defined before the routes they target within the resource block
10
10
  # To have your filter run before all routes under a given resource and its child resources
11
11
  # pass :all as the first parameter
12
- #
12
+ #
13
13
  foos.before :all do
14
14
  @all = 'all'
15
15
  end
16
-
16
+
17
17
  # To have your filter run before a specific route, the route must be one of the seven helper
18
18
  # routes (see example/routes.rb) or specify the :identifier parameter
19
19
  #
@@ -24,7 +24,7 @@ class MyApp < Sinatra::Base
24
24
  foos.new do
25
25
  "#{@all} #{@new}" # => 'all new'
26
26
  end
27
-
27
+
28
28
  # You can target the vanila methods by providing the :identifier hash option
29
29
  #
30
30
  # GET /foos/baz
@@ -33,11 +33,11 @@ class MyApp < Sinatra::Base
33
33
  end
34
34
 
35
35
  # You can also specify a before filter for nested routes by using the child resource name
36
- #
36
+ #
37
37
  foos.before :bars do
38
38
  @bars = 'some bars'
39
39
  end
40
-
40
+
41
41
  foos.resource :bars do |bars|
42
42
  bars.get '/some_bars' do
43
43
  @bars # => 'some bars'
@@ -49,12 +49,14 @@ class MyApp < Sinatra::Base
49
49
  end
50
50
 
51
51
  # Finally you can specify many different methods in your filters by passing many identifiers
52
- #
52
+ #
53
53
  foos.before :bak, :baz do
54
54
  @bak_baz = "bak 'n' baz"
55
55
  end
56
56
 
57
57
  foos.get('/something', :identifier => :bak) { @bak_baz }
58
- foos.get('/anything', :identifier => :baz) { @bak_baz }
58
+ foos.get('/anything', :identifier => :baz) { @bak_baz }
59
59
  end
60
60
  end
61
+
62
+ MyApp.run!
@@ -1,5 +1,5 @@
1
1
  require File.join(File.dirname(__FILE__), '..', 'lib', 'bebop')
2
- require 'sinatra'
2
+ require 'sinatra/base'
3
3
 
4
4
  class MyApp < Sinatra::Base
5
5
  register Bebop
@@ -14,7 +14,7 @@ class MyApp < Sinatra::Base
14
14
  foos.get '/baz' do
15
15
  'baz'
16
16
  end
17
-
17
+
18
18
  # There are 7 helper methods for the traditional resource actions. They
19
19
  # are index, new, create, update, edit, destroy, and show. Each is a wrapper
20
20
  # for the corresponding Sinatra method
@@ -36,7 +36,7 @@ class MyApp < Sinatra::Base
36
36
 
37
37
  # PUT /foos/:foo_id
38
38
  foos.update {}
39
-
39
+
40
40
  # GET /foos/:foo_id/edit
41
41
  foos.edit {}
42
42
 
@@ -46,30 +46,30 @@ class MyApp < Sinatra::Base
46
46
  # For each of the helper methods, and any generic method (eg foos.get) that specifies the :identifier
47
47
  # option, bebop will define a relative path helper. See the nested resource below for the nameing of those
48
48
  # methods
49
- #
50
- # POST /foos/do/redirect
49
+ #
50
+ # GET /foos/do/redirect
51
51
  foos.get 'do/redirect' do
52
52
  # Redirects to /foos/1
53
53
  redirect foos_show_path(1)
54
54
  end
55
55
 
56
56
  # If you want to represent the relationship of your models through nested resources use
57
- # the resource method with the block parameter and all new routes will be nested and
57
+ # the resource method with the block parameter and all new routes will be nested and
58
58
  # parameterized properly
59
59
  #
60
60
  # Prefix all with /foos/:foo_id
61
61
  foos.resource :bars do |bars|
62
-
62
+
63
63
  # GET /foos/:foo_id/bars/:bar_id/edit
64
64
  bars.edit do
65
65
  "foo: #{params[:foo_id]} bar: #{params[:bar_id]}"
66
- end
67
-
66
+ end
67
+
68
68
  bars.get '/redirect' do
69
-
69
+
70
70
  # The route helper method naming convention is simple and easy to remember as it follows
71
71
  # the order of nesting for the given route starting with the original parent resource and
72
- # ending with the method identifier.
72
+ # ending with the method identifier.
73
73
  #
74
74
  # Redirects to /foos/1/bars/2/edit
75
75
  redirect foos_bars_edit_path(1, 2)
@@ -77,3 +77,5 @@ class MyApp < Sinatra::Base
77
77
  end
78
78
  end
79
79
  end
80
+
81
+ MyApp.run!
@@ -1,7 +1,6 @@
1
1
  module Bebop
2
2
  class InvalidPathArgumentError < ArgumentError; end
3
3
  PARAM_REGEX = /:[a-zA-Z0-9_]+/
4
-
5
4
 
6
5
  def resource(name, &block)
7
6
  resource = ResourceRouter.new
@@ -18,45 +17,32 @@ module Bebop
18
17
  unless route.scan(PARAM_REGEX).length == args.length
19
18
  raise InvalidPathArgumentError.new("invalid number of parameters #{args.length} for: #{route}")
20
19
  end
21
-
20
+
22
21
  args.inject(route.dup) do |acc, arg|
23
22
  #handle fixnums and ar objects
24
- arg = (arg.kind_of?(Fixnum) ? arg : arg.id)
25
- acc.sub!(PARAM_REGEX, arg.to_s)
23
+ final_argument = arg.respond_to?(:to_param) ? arg.to_param : arg
24
+ acc.sub!(PARAM_REGEX, final_argument.to_s)
26
25
  end
27
26
  end
28
27
  end
29
-
30
- class Action
31
- attr_accessor :route, :options, :block
32
- def initialize(route, options, block)
33
- @route, @options, @block = route, options, block
34
- end
35
28
 
36
- def method
37
- self.class.to_s.downcase.to_sym
38
- end
39
- end
40
-
41
- class Get < Action; end
42
-
43
29
  class ResourceRouter
44
- attr_accessor :routes
30
+ attr_accessor :routes
45
31
 
46
32
  def logger
47
33
  @@logger ||= Logger.new(STDOUT)
48
34
  end
49
-
35
+
50
36
  def logger=(val)
51
37
  @@logger = val
52
38
  end
53
-
39
+
54
40
  def initialize(parent_resources=[], before_all=[], after_all=[])
55
41
  @current_resource = parent_resources.pop
56
42
  @parent_resources = parent_resources
57
43
  @before, @after, @routes = before_all, after_all, []
58
44
  end
59
-
45
+
60
46
  def get(route, options={}, &block)
61
47
  add_route(:get, route, options, block)
62
48
  end
@@ -64,7 +50,7 @@ module Bebop
64
50
  def put(route, options={}, &block)
65
51
  add_route(:put, route, options, block)
66
52
  end
67
-
53
+
68
54
  def post(route, options={}, &block)
69
55
  add_route(:post, route, options, block)
70
56
  end
@@ -80,7 +66,7 @@ module Bebop
80
66
  def new(options={}, &block)
81
67
  get 'new', options.merge(:identifier => :new), &block
82
68
  end
83
-
69
+
84
70
  def create(options={}, &block)
85
71
  post '' , options.merge(:identifier => :create), &block
86
72
  end
@@ -116,7 +102,7 @@ module Bebop
116
102
  def resource(name, &block)
117
103
  before_filters = filters(@before, :all, name)
118
104
  after_filters = filters(@after, :all, name)
119
-
105
+
120
106
  router = self.class.new(all_resources + [name], before_filters, after_filters)
121
107
  yield(router)
122
108
  @routes += router.routes
@@ -131,11 +117,11 @@ module Bebop
131
117
  logger.info " identifier: #{identifier} " if identifier
132
118
  end
133
119
  end
134
-
120
+
135
121
  private
136
-
122
+
137
123
  def all_resources
138
- #in the initial call to resource there is current_resource
124
+ #in the initial call to resource there isn't a current_resource
139
125
  @current_resource ? @parent_resources + [@current_resource] : @parent_resources
140
126
  end
141
127
 
@@ -147,8 +133,8 @@ module Bebop
147
133
  params = [:all] if params.empty?
148
134
  type << {:routes => params, :block => block}
149
135
  end
150
-
151
- def add_route(method, route, options, block)
136
+
137
+ def add_route(method, route, options, block)
152
138
  identifier = options[:identifier]
153
139
  route = append_to_path(route)
154
140
  block = add_filters_to_block(block, identifier, method)
@@ -178,7 +164,7 @@ module Bebop
178
164
 
179
165
  def route_helper(identifier)
180
166
  helper_tokens = (@parent_resources.dup << @current_resource) << identifier
181
- "#{helper_tokens.join('_')}_path".to_sym
167
+ "#{helper_tokens.compact.join('_')}_path".to_sym
182
168
  end
183
169
 
184
170
  def add_filters_to_block(block, identifier, method)
@@ -1,9 +1,12 @@
1
1
  require File.join(File.dirname(__FILE__), '..', 'lib', 'bebop')
2
2
  require 'rack/test'
3
- require 'sinatra'
3
+ require 'sinatra/base'
4
+ require 'ruby-debug'
4
5
 
5
6
  class TestClass < Sinatra::Base
6
7
  register Bebop
8
+ set :show_exceptions, true
9
+
7
10
  def self.global
8
11
  @@global
9
12
  end
@@ -14,34 +17,31 @@ end
14
17
 
15
18
  describe Bebop do
16
19
  include Rack::Test::Methods
17
-
20
+
18
21
  def app
19
- @test_app ||= TestClass.new
22
+ @test_app ||= TestClass.new
20
23
  @test_app
21
24
  end
22
25
 
23
26
  before :all do
24
27
  app
25
28
  @class = TestClass
26
- use_bebop
29
+ use_bebop
27
30
  end
28
31
 
29
32
  before :each do
30
33
  TestClass.global = nil
31
34
  end
32
35
 
33
- it "should define route helpers properly for routes specifying an identifier" do
34
- @class.instance_methods.should include('foos_bars_index_path')
35
- @class.instance_methods.should include('foos_create_path')
36
- end
37
-
38
36
  it "should provide the correct relative url from the route helpers" do
39
- @test_app.foos_bars_index_path(1).should == '/foos/1/bars'
40
- @test_app.foos_bars_update_path(1,2).should == '/foos/1/bars/2'
37
+ get '/foos/route_helper_test'
38
+ last_response.body.should include('/foos/1/bars')
39
+ last_response.body.should include('/foos/1/bars/2')
41
40
  end
42
41
 
43
42
  it "should raise an error when the wrong number of paramters are passed to a route helper" do
44
- lambda {@test_app.foos_bars_update_path(1)}.should raise_error(Bebop::InvalidPathArgumentError)
43
+ get '/foos/exception'
44
+ last_response.body.should include('Bebop::InvalidPathArgumentError')
45
45
  end
46
46
 
47
47
  it "should define a route with new for the new method" do
@@ -91,24 +91,24 @@ describe Bebop do
91
91
 
92
92
  get '/foos/1/bars'
93
93
  last_response.body.should match(/#{BEFORE_ALL}/)
94
-
94
+
95
95
  delete '/foos/1/bars/1'
96
- last_response.body.should match(/#{BEFORE_ALL}/)
96
+ last_response.body.should match(/#{BEFORE_ALL}/)
97
97
 
98
98
  put '/foos/1/bars/1'
99
99
  last_response.body.should match(/#{BEFORE_ALL}/)
100
100
  end
101
-
101
+
102
102
  it "should call before and after resource blocks only on the nested resource routes" do
103
103
  get '/foos/1/bars'
104
104
  last_response.body.should match(/#{BEFORE_BARS}/)
105
-
105
+
106
106
  delete '/foos/1/bars/1'
107
107
  last_response.body.should match(/#{BEFORE_BARS}/)
108
108
  end
109
-
109
+
110
110
  it "should not call before and after resource blocks on non nested resource blocks" do
111
- post '/foos'
111
+ post '/foos'
112
112
  last_response.body.should_not match(/#{BEFORE_BARS}/)
113
113
  end
114
114
 
@@ -121,14 +121,14 @@ describe Bebop do
121
121
 
122
122
  get '/foos/1/bars'
123
123
  last_response.body.should match(/#{BEFORE_ALL_2}/)
124
-
124
+
125
125
  delete '/foos/1/bars/1'
126
- last_response.body.should match(/#{BEFORE_ALL_2}/)
126
+ last_response.body.should match(/#{BEFORE_ALL_2}/)
127
127
 
128
128
  put '/foos/1/bars/1'
129
129
  last_response.body.should match(/#{BEFORE_ALL_2}/)
130
130
  end
131
-
131
+
132
132
  it "should call before and after filters that specify multiple identifiers before the proper routes" do
133
133
  post '/foos'
134
134
  TestClass.global.should == AFTER_VALUE
@@ -136,8 +136,8 @@ describe Bebop do
136
136
  put '/foos/1'
137
137
  TestClass.global.should == AFTER_VALUE
138
138
  end
139
-
140
- it "should not call before and after filters that specify multiple parameters on anyting else" do
139
+
140
+ it "should not call before and after filters that specify multiple parameters on anything else" do
141
141
  get '/foos/new'
142
142
  TestClass.global = nil
143
143
  end
@@ -148,10 +148,10 @@ describe Bebop do
148
148
  end
149
149
 
150
150
  it "should produce correct routes for more than 2 levels of nesting" do
151
- get '/foos/1/bars/2/bazs'
151
+ get '/foos/1/bars/2/bazs'
152
152
  last_response.body.should match(/#{BEFORE_ALL_2}/)
153
153
  end
154
-
154
+
155
155
  BEFORE_BARS = '__before_bars__'
156
156
  BEFORE_UPDATE = '__before_update__'
157
157
  BEFORE_ALL = '__all__'
@@ -159,9 +159,8 @@ describe Bebop do
159
159
  AFTER_VALUE = '__after__'
160
160
 
161
161
  def use_bebop
162
- # ENV['PROUTES']='t'
163
162
  @class.resource :foos do |foo|
164
-
163
+
165
164
  foo.before :all do
166
165
  @all = BEFORE_ALL
167
166
  end
@@ -173,7 +172,7 @@ describe Bebop do
173
172
  foo.before :update do
174
173
  @update = BEFORE_UPDATE
175
174
  end
176
-
175
+
177
176
  foo.before :bars do
178
177
  @bars = BEFORE_BARS
179
178
  end
@@ -182,7 +181,7 @@ describe Bebop do
182
181
  TestClass.global = AFTER_VALUE
183
182
  end
184
183
 
185
- foo.get(:arbitrary) { 'baz' }
184
+ foo.get(:arbitrary) { 'baz' }
186
185
 
187
186
  foo.create { "#{@all2}#{@all}#{@update}#{@bars}" }
188
187
  foo.update { "#{@all2}#{@all}#{@update}#{@bars}" }
@@ -203,6 +202,14 @@ describe Bebop do
203
202
  end
204
203
 
205
204
  foo.get('/do/something') { 'success' }
206
- end
205
+
206
+ foo.get '/route_helper_test' do
207
+ foos_bars_update_path(1,2) + foos_bars_path(1)
208
+ end
209
+
210
+ foo.get '/exception' do
211
+ foos_bars_update_path(1)
212
+ end
213
+ end
207
214
  end
208
215
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bebop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 3
9
+ version: 0.1.3
5
10
  platform: ruby
6
11
  authors:
7
12
  - John Bender
@@ -14,34 +19,46 @@ default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: sinatra
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 9
30
+ - 4
23
31
  version: 0.9.4
24
- version:
32
+ type: :runtime
33
+ version_requirements: *id001
25
34
  - !ruby/object:Gem::Dependency
26
35
  name: activesupport
27
- type: :runtime
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
30
38
  requirements:
31
39
  - - ">="
32
40
  - !ruby/object:Gem::Version
41
+ segments:
42
+ - 2
43
+ - 3
44
+ - 5
33
45
  version: 2.3.5
34
- version:
46
+ type: :runtime
47
+ version_requirements: *id002
35
48
  - !ruby/object:Gem::Dependency
36
49
  name: rspec
37
- type: :development
38
- version_requirement:
39
- version_requirements: !ruby/object:Gem::Requirement
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
40
52
  requirements:
41
53
  - - ">="
42
54
  - !ruby/object:Gem::Version
55
+ segments:
56
+ - 1
57
+ - 2
58
+ - 9
43
59
  version: 1.2.9
44
- version:
60
+ type: :development
61
+ version_requirements: *id003
45
62
  description: A small Sinatra/Monk extension for resource routing
46
63
  email: john.m.bender@gmail.com
47
64
  executables: []
@@ -59,7 +76,7 @@ files:
59
76
  - lib/bebop/ext.rb
60
77
  - lib/bebop.rb
61
78
  - spec/bebop_spec.rb
62
- has_rdoc: true
79
+ has_rdoc: false
63
80
  homepage: http://github.com/johnbender/bebop
64
81
  licenses: []
65
82
 
@@ -72,18 +89,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
72
89
  requirements:
73
90
  - - ">="
74
91
  - !ruby/object:Gem::Version
92
+ segments:
93
+ - 0
75
94
  version: "0"
76
- version:
77
95
  required_rubygems_version: !ruby/object:Gem::Requirement
78
96
  requirements:
79
97
  - - ">="
80
98
  - !ruby/object:Gem::Version
99
+ segments:
100
+ - 0
81
101
  version: "0"
82
- version:
83
102
  requirements: []
84
103
 
85
104
  rubyforge_project: bebop
86
- rubygems_version: 1.3.5
105
+ rubygems_version: 1.3.6
87
106
  signing_key:
88
107
  specification_version: 3
89
108
  summary: A small Sinatra/Monk extension for resource routing