blindgaenger-sinatra-rest 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ task :default => :test
4
4
 
5
5
  desc "Run tests"
6
6
  Spec::Rake::SpecTask.new :test do |t|
7
- t.spec_opts = %w(--format specdoc --color --backtrace)
7
+ t.spec_opts = %w(--format specdoc --color) #--backtrace
8
8
  t.spec_files = FileList['test/*_spec.rb']
9
9
  end
10
10
 
@@ -0,0 +1,67 @@
1
+ ---
2
+ :index:
3
+ :verb: GET
4
+ :url: /PLURAL
5
+ :control: |-
6
+ @PLURAL = call_model_method(MODEL, :all, mp)
7
+ :render: |-
8
+ RENDERER :'PLURAL/index', options
9
+
10
+ :new:
11
+ :verb: GET
12
+ :url: /PLURAL/new
13
+ :control: |-
14
+ @SINGULAR = call_model_method(MODEL, :new, mp)
15
+ :render: |-
16
+ RENDERER :'PLURAL/new', options
17
+
18
+ :create:
19
+ :verb: POST
20
+ :url: /PLURAL
21
+ :control: |-
22
+ @SINGULAR = call_model_method(MODEL, :new, mp)
23
+ @SINGULAR.save
24
+ :render: |-
25
+ redirect url_for_PLURAL_show(@SINGULAR), 'SINGULAR created'
26
+
27
+ :show:
28
+ :verb: GET
29
+ :url: /PLURAL/:id
30
+ :control: |-
31
+ @SINGULAR = call_model_method(MODEL, :find_by_id, mp[:id])
32
+ :render: |-
33
+ if @SINGULAR.nil?
34
+ throw :halt, [404, 'SINGULAR not found']
35
+ else
36
+ RENDERER :'PLURAL/show', options
37
+ end
38
+
39
+ :edit:
40
+ :verb: GET
41
+ :url: /PLURAL/:id/edit
42
+ :control: |-
43
+ @SINGULAR = call_model_method(MODEL, :find_by_id, mp[:id])
44
+ :render: |-
45
+ RENDERER :'PLURAL/edit', options
46
+
47
+ :update:
48
+ :verb: PUT
49
+ :url: /PLURAL/:id
50
+ :control: |-
51
+ @SINGULAR = call_model_method(MODEL, :find_by_id, mp[:id])
52
+ @SINGULAR.update_attributes(mp) unless @SINGULAR.nil?
53
+ :render: |-
54
+ if @SINGULAR.nil?
55
+ throw :halt, [404, 'SINGULAR not found']
56
+ else
57
+ redirect url_for_PLURAL_show(@SINGULAR), 'SINGULAR updated'
58
+ end
59
+
60
+ :destroy:
61
+ :verb: DELETE
62
+ :url: /PLURAL/:id
63
+ :control: |-
64
+ call_model_method(MODEL, :delete, mp[:id])
65
+ :render: |-
66
+ redirect url_for_PLURAL_index, 'SINGULAR destroyed'
67
+
data/lib/sinatra/rest.rb CHANGED
@@ -4,6 +4,7 @@ require 'english/inflect'
4
4
  libdir = File.dirname(__FILE__) + "/rest"
5
5
  $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
6
6
  require 'adapters'
7
+ require 'yaml'
7
8
 
8
9
  module Sinatra
9
10
 
@@ -13,13 +14,14 @@ module Sinatra
13
14
  # adds restful routes and url helpers for the model
14
15
  def rest(model_class, options={}, &block)
15
16
  parse_args(model_class, options)
17
+ read_config('rest/rest.yaml')
16
18
 
17
19
  # register model specific helpers
18
- helpers read_module_template('rest/helpers.tpl.rb')
20
+ helpers generate_helpers
19
21
 
20
22
  # create an own module, to override the template with custom methods
21
23
  # this way, you can still use #super# in the overridden methods
22
- controller = read_module_template('rest/controller.tpl.rb')
24
+ controller = generate_controller
23
25
  if block_given?
24
26
  custom = CustomController.new(@plural)
25
27
  custom.instance_eval &block
@@ -29,7 +31,7 @@ module Sinatra
29
31
  helpers controller
30
32
 
31
33
  # register routes as DSL extension
32
- instance_eval read_template('rest/routes.tpl.rb')
34
+ instance_eval generate_routes
33
35
  end
34
36
 
35
37
  protected
@@ -48,14 +50,16 @@ module Sinatra
48
50
  end
49
51
 
50
52
  def parse_routes(routes)
51
- [*routes].map {|route| ROUTES[route] || route}.flatten.uniq
53
+ routes = [*routes].map {|route| ROUTES[route] || route}.flatten.uniq
54
+ # keep the order of :all routes
55
+ ROUTES[:all].select{|route| routes.include? route}
52
56
  end
53
57
 
54
- def gsub_routes(routes, template)
55
- routes.each {|route| template.gsub!(/#{route.to_s.upcase}/, true.to_s) }
56
- (ROUTES[:all] - routes).each {|route| template.gsub!(/#{route.to_s.upcase}/, false.to_s) }
58
+ def read_config(filename)
59
+ file = File.read(File.join(File.dirname(__FILE__), filename))
60
+ @config = YAML.load file
57
61
  end
58
-
62
+
59
63
  #
60
64
  # creates the necessary forms of the model name
61
65
  # pretty much like ActiveSupport's inflections, but don't like to depend on
@@ -65,27 +69,80 @@ module Sinatra
65
69
  return model, singular, singular.pluralize
66
70
  end
67
71
 
68
- #
69
- # read the file and do some substitutions
70
- def read_template(filename)
71
- t = File.read(File.join(File.dirname(__FILE__), filename))
72
+ def replace_variables(t, route=nil)
73
+ if route
74
+ t.gsub!('NAME', route.to_s)
75
+ t.gsub!('VERB', @config[route][:verb].downcase)
76
+ t.gsub!('URL', @config[route][:url])
77
+ t.gsub!('CONTROL', @config[route][:control])
78
+ t.gsub!('RENDER', @config[route][:render])
79
+ end
72
80
  t.gsub!(/PLURAL/, @plural)
73
81
  t.gsub!(/SINGULAR/, @singular)
74
82
  t.gsub!(/MODEL/, @model)
75
83
  t.gsub!(/RENDERER/, @renderer)
76
- gsub_routes(@route_flags, t)
77
- t
84
+ t
78
85
  end
79
86
 
80
- #
81
- # read the template and put it into an anonymous module
82
- def read_module_template(filename)
83
- t = read_template(filename)
87
+ def generate_routes
88
+ @route_flags.map{|r| route_template(r)}.join("\n\n")
89
+ end
90
+
91
+ def route_template(route)
92
+ t = <<-RUBY
93
+ VERB 'URL' do
94
+ PLURAL_before :NAME
95
+ PLURAL_NAME
96
+ PLURAL_after :NAME
97
+ RENDER
98
+ end
99
+ RUBY
100
+ replace_variables(t, route)
101
+ end
102
+
103
+ def generate_helpers
84
104
  m = Module.new
85
- m.module_eval(t, filename)
105
+ @route_flags.each {|r|
106
+ m.module_eval helpers_template(r)
107
+ }
86
108
  m
87
109
  end
110
+
111
+ def helpers_template(route)
112
+ t = <<-RUBY
113
+ def url_for_PLURAL_NAME(model=nil)
114
+ "URL"
115
+ end
116
+ RUBY
117
+ helper_route = @config[route][:url].gsub(':id', '#{escape_model_id(model)}')
118
+ t.gsub!('URL', helper_route)
119
+ replace_variables(t, route)
120
+ end
88
121
 
122
+ def generate_controller
123
+ m = Module.new
124
+ t = <<-RUBY
125
+ def PLURAL_before(name); end
126
+ def PLURAL_after(name); end
127
+ RUBY
128
+ m.module_eval replace_variables(t)
129
+
130
+ @route_flags.each {|route|
131
+ m.module_eval controller_template(route)
132
+ }
133
+ m
134
+ end
135
+
136
+ def controller_template(route)
137
+ t = <<-RUBY
138
+ def PLURAL_NAME(options=params)
139
+ mp = filter_model_params(options)
140
+ CONTROL
141
+ end
142
+ RUBY
143
+ replace_variables(t, route)
144
+ end
145
+
89
146
  #
90
147
  # model unspecific helpers, will be included once
91
148
  module Helpers
@@ -110,7 +167,7 @@ module Sinatra
110
167
 
111
168
  def call_model_method(model_class, name, options={})
112
169
  method = model_class.method(name)
113
- if method.arity == 0
170
+ if options.nil? || method.arity == 0
114
171
  Kernel.warn "warning: calling #{model_class.to_s}##{name} with args, although it doesn't take args" if options
115
172
  method.call
116
173
  else
@@ -129,15 +186,15 @@ module Sinatra
129
186
  @module = Module.new
130
187
  end
131
188
 
132
- def before(&block) prefix :before, &block; end
133
- def after(&block) prefix :after, &block; end
134
- def index(&block) prefix :index, &block; end
135
- def new(&block) prefix :new, &block; end
136
- def create(&block) prefix :create, &block; end
137
- def show(&block) prefix :show, &block; end
138
- def edit(&block) prefix :edit, &block; end
139
- def update(&block) prefix :update, &block; end
140
- def destroy(&block) prefix :destroy, &block; end
189
+ def before(options={}, &block) prefix :before, &block; end
190
+ def after(options={}, &block) prefix :after, &block; end
191
+ def index(options={}, &block) prefix :index, &block; end
192
+ def new(options={}, &block) prefix :new, &block; end
193
+ def create(options={}, &block) prefix :create, &block; end
194
+ def show(options={}, &block) prefix :show, &block; end
195
+ def edit(options={}, &block) prefix :edit, &block; end
196
+ def update(options={}, &block) prefix :update, &block; end
197
+ def destroy(options={}, &block) prefix :destroy, &block; end
141
198
 
142
199
  private
143
200
  def prefix(name, &block)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blindgaenger-sinatra-rest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - blindgaenger
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-10 00:00:00 -07:00
12
+ date: 2009-04-26 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -45,9 +45,7 @@ files:
45
45
  - README.textile
46
46
  - lib/sinatra/rest.rb
47
47
  - lib/sinatra/rest/adapters.rb
48
- - lib/sinatra/rest/controller.tpl.rb
49
- - lib/sinatra/rest/helpers.tpl.rb
50
- - lib/sinatra/rest/routes.tpl.rb
48
+ - lib/sinatra/rest/rest.yaml
51
49
  - test/call_order_spec.rb
52
50
  - test/crud_spec.rb
53
51
  - test/helper.rb
@@ -1,65 +0,0 @@
1
- def PLURAL_before(name)
2
- # override
3
- end
4
-
5
- def PLURAL_after(name)
6
- # override
7
- end
8
-
9
- # index GET /models
10
- if INDEX
11
- def PLURAL_index(options=nil)
12
- @PLURAL = call_model_method(MODEL, :all, options)
13
- end
14
- end
15
-
16
- # new GET /models/new
17
- if NEW
18
- def PLURAL_new(options=nil)
19
- @SINGULAR = call_model_method(MODEL, :new, options)
20
- end
21
- end
22
-
23
- # create POST /models
24
- if CREATE
25
- def PLURAL_create(options=params)
26
- mp = filter_model_params(options)
27
- @SINGULAR = call_model_method(MODEL, :new, mp)
28
- @SINGULAR.save
29
- end
30
- end
31
-
32
- # show GET /models/1
33
- if SHOW
34
- def PLURAL_show(options=params)
35
- mp = filter_model_params(options)
36
- @SINGULAR = call_model_method(MODEL, :find_by_id, mp[:id])
37
- end
38
- end
39
-
40
- # edit GET /models/1/edit
41
- if EDIT
42
- def PLURAL_edit(options=params)
43
- mp = filter_model_params(options)
44
- @SINGULAR = call_model_method(MODEL, :find_by_id, mp[:id])
45
- end
46
- end
47
-
48
- # update PUT /models/1
49
- if UPDATE
50
- def PLURAL_update(options=params)
51
- mp = filter_model_params(options)
52
- @SINGULAR = call_model_method(MODEL, :find_by_id, mp[:id])
53
- @SINGULAR.update_attributes(mp) unless @SINGULAR.nil?
54
- end
55
- end
56
-
57
- # destroy DELETE /models/1
58
- if DESTROY
59
- def PLURAL_destroy(options=params)
60
- mp = filter_model_params(options)
61
- call_model_method(MODEL, :delete, mp[:id])
62
- end
63
- end
64
-
65
-
@@ -1,49 +0,0 @@
1
- # index GET /models
2
- if INDEX
3
- def url_for_PLURAL_index
4
- '/PLURAL'
5
- end
6
- end
7
-
8
- # new GET /models/new
9
- if NEW
10
- def url_for_PLURAL_new
11
- '/PLURAL/new'
12
- end
13
- end
14
-
15
- # create POST /models
16
- if CREATE
17
- def url_for_PLURAL_create
18
- '/PLURAL'
19
- end
20
- end
21
-
22
- # show GET /models/1
23
- if SHOW
24
- def url_for_PLURAL_show(model)
25
- "/PLURAL/#{escape_model_id(model)}"
26
- end
27
- end
28
-
29
- # edit GET /models/1/edit
30
- if EDIT
31
- def url_for_PLURAL_edit(model)
32
- "/PLURAL/#{escape_model_id(model)}/edit"
33
- end
34
- end
35
-
36
- # update PUT /models/1
37
- if UPDATE
38
- def url_for_PLURAL_update(model)
39
- "/PLURAL/#{escape_model_id(model)}"
40
- end
41
- end
42
-
43
- # destroy DELETE /models/1
44
- if DESTROY
45
- def url_for_PLURAL_destroy(model)
46
- "/PLURAL/#{escape_model_id(model)}"
47
- end
48
- end
49
-
@@ -1,78 +0,0 @@
1
- # index GET /models
2
- if INDEX
3
- get '/PLURAL' do
4
- PLURAL_before :index
5
- PLURAL_index
6
- PLURAL_after :index
7
- RENDERER :"PLURAL/index", options
8
- end
9
- end
10
-
11
- # new GET /models/new
12
- if NEW
13
- get '/PLURAL/new' do
14
- PLURAL_before :new
15
- PLURAL_new
16
- PLURAL_after :new
17
- RENDERER :"PLURAL/new", options
18
- end
19
- end
20
-
21
- # create POST /models
22
- if CREATE
23
- post '/PLURAL' do
24
- PLURAL_before :create
25
- PLURAL_create
26
- PLURAL_after :create
27
- redirect url_for_PLURAL_show(@SINGULAR), 'SINGULAR created'
28
- end
29
- end
30
-
31
- # show GET /models/1
32
- if SHOW
33
- get '/PLURAL/:id' do
34
- PLURAL_before :show
35
- PLURAL_show
36
- PLURAL_after :show
37
- if @SINGULAR.nil?
38
- throw :halt, [404, 'SINGULAR not found']
39
- else
40
- RENDERER :"PLURAL/show", options
41
- end
42
- end
43
- end
44
-
45
- # edit GET /models/1/edit
46
- if EDIT
47
- get '/PLURAL/:id/edit' do
48
- PLURAL_before :edit
49
- PLURAL_edit
50
- PLURAL_after :edit
51
- RENDERER :"PLURAL/edit", options
52
- end
53
- end
54
-
55
- # update PUT /models/1
56
- if UPDATE
57
- put '/PLURAL/:id' do
58
- PLURAL_before :update
59
- PLURAL_update
60
- PLURAL_after :update
61
- if @SINGULAR.nil?
62
- throw :halt, [404, 'SINGULAR not found']
63
- else
64
- redirect url_for_PLURAL_show(@SINGULAR), 'SINGULAR updated'
65
- end
66
- end
67
- end
68
-
69
- # destroy DELETE /models/1
70
- if DESTROY
71
- delete '/PLURAL/:id' do
72
- PLURAL_before :destroy
73
- PLURAL_destroy
74
- PLURAL_after :destroy
75
- redirect url_for_PLURAL_index, 'SINGULAR destroyed'
76
- end
77
- end
78
-