resource_mapper 0.0.1 → 0.0.4
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/Rakefile +6 -4
- data/VERSION +1 -1
- data/examples/lib/sinatra/resource_example.rb +88 -18
- data/examples/models.rb +21 -7
- data/lib/resource_mapper/actions.rb +5 -5
- data/lib/resource_mapper/class_methods.rb +46 -6
- data/lib/resource_mapper/controller.rb +53 -12
- data/lib/resource_mapper/helpers/current_objects.rb +20 -17
- data/lib/sinatra/resource.rb +16 -9
- data/resource_mapper.gemspec +17 -11
- metadata +48 -26
data/Rakefile
CHANGED
@@ -11,10 +11,12 @@ begin
|
|
11
11
|
gem.homepage = "http://github.com/adamelliot/resource_mapper"
|
12
12
|
gem.authors = ["Adam Elliot"]
|
13
13
|
gem.add_dependency "sinatra", ">= 1.0"
|
14
|
-
gem.add_dependency "
|
15
|
-
gem.add_dependency "
|
14
|
+
gem.add_dependency "plist", ">= 3.1.0"
|
15
|
+
gem.add_dependency "builder", ">= 2.1.2"
|
16
|
+
gem.add_dependency "activesupport", ">= 3.0.0"
|
17
|
+
gem.add_dependency "mongo_mapper", ">= 0.8.4"
|
16
18
|
gem.add_development_dependency "micronaut", ">= 0.3.0"
|
17
|
-
gem.add_development_dependency "rack-test", ">= 0.5.
|
19
|
+
gem.add_development_dependency "rack-test", ">= 0.5.5"
|
18
20
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
19
21
|
end
|
20
22
|
Jeweler::GemcutterTasks.new
|
@@ -26,7 +28,7 @@ begin
|
|
26
28
|
require 'micronaut/rake_task'
|
27
29
|
Micronaut::RakeTask.new(:examples) do |examples|
|
28
30
|
examples.pattern = 'examples/**/*_example.rb'
|
29
|
-
examples.ruby_opts << '-Ilib -Iexamples -
|
31
|
+
examples.ruby_opts << '-Ilib -Iexamples -rexample_helper.rb'
|
30
32
|
end
|
31
33
|
|
32
34
|
Micronaut::RakeTask.new(:rcov) do |examples|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4
|
@@ -1,5 +1,5 @@
|
|
1
1
|
describe Sinatra::Resource do
|
2
|
-
|
2
|
+
|
3
3
|
class BasicApp < Sinatra::Base
|
4
4
|
register Sinatra::Resource
|
5
5
|
resource Person
|
@@ -13,41 +13,41 @@ describe Sinatra::Resource do
|
|
13
13
|
def app
|
14
14
|
BasicApp
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
before :each do
|
18
18
|
Person.reset!
|
19
19
|
end
|
20
20
|
|
21
21
|
it "returns all people in JSON" do
|
22
|
-
get '/people'
|
22
|
+
get '/people.json'
|
23
23
|
last_response.body.should == Person.all.to_json
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
it "returns a specific person as a JSON object" do
|
27
|
-
get '/
|
27
|
+
get '/people/2.json'
|
28
28
|
last_response.body.should == Person.find(2).to_json
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
it "creates a new person and return the JSON object" do
|
32
32
|
len = Person.all.length
|
33
|
-
post '/
|
33
|
+
post '/people.json', {:person => {:id => 4, :name => 'Seth'}}
|
34
34
|
Person.all.length.should == len + 1
|
35
35
|
last_response.body.should == Person.find(4).to_json
|
36
36
|
end
|
37
37
|
|
38
38
|
it "updates an existing person and returns the JSON object" do
|
39
|
-
put '/
|
39
|
+
put '/people/3.json', {:person => {:id => 3, :name => 'Seth'}}
|
40
40
|
Person.find(3).name.should == 'Seth'
|
41
41
|
last_response.body.should == Person.find(3).to_json
|
42
42
|
end
|
43
|
-
|
43
|
+
|
44
44
|
it "deletes a record and return it's content in JSON" do
|
45
45
|
len = Person.all.length
|
46
|
-
delete '/
|
46
|
+
delete '/people/3.json'
|
47
47
|
last_response.body.should == ""
|
48
48
|
Person.all.length.should == len - 1
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
it "fails when requesting an invalid resource" do
|
52
52
|
get '/error'
|
53
53
|
last_response.should_not be_ok
|
@@ -87,19 +87,19 @@ describe Sinatra::Resource do
|
|
87
87
|
|
88
88
|
it "errors and the records don't get altered" do
|
89
89
|
len = Person.all.length
|
90
|
-
delete '/
|
90
|
+
delete '/people/1.json'
|
91
91
|
last_response.should_not be_ok
|
92
92
|
Person.all.length.should == len
|
93
93
|
end
|
94
94
|
|
95
95
|
it "sets the name to upper case in the before create section" do
|
96
|
-
post '/
|
96
|
+
post '/people.json', {:person => {:id => 4, :name => 'Adam'}}
|
97
97
|
last_response.should be_ok
|
98
98
|
Person.find(4).name.should == 'ADAM'
|
99
99
|
end
|
100
100
|
|
101
101
|
it "allows calling of sinatra helpers from the actions in the resource" do
|
102
|
-
put '/
|
102
|
+
put '/people/1.json', {:person => {:name => 'Astro'}}
|
103
103
|
last_response.should be_ok
|
104
104
|
Person.find(1).name.should == 'Astro'
|
105
105
|
end
|
@@ -130,21 +130,91 @@ describe Sinatra::Resource do
|
|
130
130
|
def app
|
131
131
|
BeforeAndAfterSetsApp
|
132
132
|
end
|
133
|
-
|
133
|
+
|
134
134
|
before :each do
|
135
135
|
Person.reset!
|
136
136
|
end
|
137
137
|
|
138
138
|
it "sets the same before action for create and update" do
|
139
|
-
post '/
|
139
|
+
post '/people.json', {:person => {:id => 4, :name => 'Adam'}}
|
140
140
|
last_response.should be_ok
|
141
141
|
Person.find(4).name.should == 'ADAM'
|
142
142
|
|
143
|
-
put '/
|
143
|
+
put '/people/4.json', {:person => {:id => 4, :name => 'Sandra'}}
|
144
144
|
last_response.should be_ok
|
145
145
|
Person.find(4).name.should == 'SANDRA'
|
146
146
|
end
|
147
|
-
|
147
|
+
|
148
|
+
end
|
149
|
+
|
150
|
+
class RenderTemplateThroughResourceApp < Sinatra::Base
|
151
|
+
register Sinatra::Resource
|
152
|
+
|
153
|
+
resource Person do
|
154
|
+
index.wants.html { erb '<%= 3 * 3 %>' }
|
155
|
+
end
|
148
156
|
end
|
149
157
|
|
158
|
+
describe "rendering template through a resource" do
|
159
|
+
def app
|
160
|
+
RenderTemplateThroughResourceApp
|
161
|
+
end
|
162
|
+
|
163
|
+
it "displays the erb rendered output" do
|
164
|
+
get '/people'
|
165
|
+
last_response.body.should == '9'
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
class RestrictWritingUnderActions < Sinatra::Base
|
170
|
+
register Sinatra::Resource
|
171
|
+
|
172
|
+
resource Person do
|
173
|
+
index_attrs :name, :phone, :kitty
|
174
|
+
show_attrs :id, :name
|
175
|
+
create_attrs :id, :name
|
176
|
+
update_attrs :phone
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
describe "restricting attributes based on action", :focused => true do
|
181
|
+
def app
|
182
|
+
RestrictWritingUnderActions
|
183
|
+
end
|
184
|
+
|
185
|
+
before :each do
|
186
|
+
Person.reset!
|
187
|
+
end
|
188
|
+
|
189
|
+
it "restricts the output for the index action" do
|
190
|
+
Person.clear!
|
191
|
+
Person.new(1, "Astro", '555-5555').save
|
192
|
+
get '/people.json'
|
193
|
+
last_response.should be_ok
|
194
|
+
last_response.body.should_not =~ /"id":1/
|
195
|
+
end
|
196
|
+
|
197
|
+
it "restricts the output for the show action" do
|
198
|
+
Person.clear!
|
199
|
+
Person.new(1, "Astro", '555-5555').save
|
200
|
+
get '/people/1.json'
|
201
|
+
last_response.should be_ok
|
202
|
+
last_response.body.should_not =~ /555\-5555/
|
203
|
+
end
|
204
|
+
|
205
|
+
it "restricts saved attributes for the create action" do
|
206
|
+
post '/people.json', {:person => {:id => 4, :name => 'Adam', :phone => '555-9999'}}
|
207
|
+
last_response.should be_ok
|
208
|
+
Person.find(4).phone.should == nil
|
209
|
+
Person.find(4).name.should == 'Adam'
|
210
|
+
end
|
211
|
+
|
212
|
+
it "restricts saved attributes for the update action" do
|
213
|
+
name = Person.find(1).name
|
214
|
+
put '/people/1.json', {:person => {:name => 'Adam', :phone => '555-9999'}}
|
215
|
+
last_response.should be_ok
|
216
|
+
Person.find(1).phone.should == '555-9999'
|
217
|
+
Person.find(1).name.should == name
|
218
|
+
end
|
219
|
+
end
|
150
220
|
end
|
data/examples/models.rb
CHANGED
@@ -1,17 +1,20 @@
|
|
1
|
-
# Shamelessly
|
1
|
+
# Shamelessly taken from sinatra-rest, adjusted for JSON
|
2
2
|
require 'active_support/json'
|
3
3
|
|
4
4
|
class Person
|
5
5
|
attr_accessor :id
|
6
6
|
attr_accessor :name
|
7
|
+
attr_accessor :phone
|
7
8
|
|
8
9
|
def initialize(*args)
|
9
10
|
if args.size == 0
|
10
11
|
@id = nil
|
11
12
|
@name = nil
|
12
|
-
|
13
|
+
@phone = nil
|
14
|
+
elsif args.size == 3
|
13
15
|
@id = args[0].to_i
|
14
16
|
@name = args[1]
|
17
|
+
@phone = args[2]
|
15
18
|
else args.size == 1
|
16
19
|
update_attributes(args[0])
|
17
20
|
end
|
@@ -36,11 +39,22 @@ class Person
|
|
36
39
|
unless hash.empty?
|
37
40
|
@id = hash['id'].to_i if hash.include?('id')
|
38
41
|
@name = hash['name'] if hash.include?('name')
|
42
|
+
@phone = hash['phone'] if hash.include?('phone')
|
39
43
|
end
|
40
44
|
end
|
41
45
|
|
42
|
-
def to_json(
|
43
|
-
|
46
|
+
def to_json(options={})
|
47
|
+
ret = {}
|
48
|
+
puts options
|
49
|
+
if !options[:only].nil?
|
50
|
+
options[:only].each { |key| ret[key] = self.send(key) }
|
51
|
+
else
|
52
|
+
ret[:id] = self.id
|
53
|
+
ret[:name] = self.name
|
54
|
+
ret[:phone] = self.phone
|
55
|
+
end
|
56
|
+
|
57
|
+
ret.to_json
|
44
58
|
end
|
45
59
|
alias_method :as_json, :to_json
|
46
60
|
|
@@ -68,9 +82,9 @@ class Person
|
|
68
82
|
|
69
83
|
def self.reset!
|
70
84
|
clear!
|
71
|
-
Person.new(1, 'Al').save
|
72
|
-
Person.new(2, 'Sol').save
|
73
|
-
Person.new(3, 'Alma').save
|
85
|
+
Person.new(1, 'Al', '555-1234').save
|
86
|
+
Person.new(2, 'Sol', '555-6666').save
|
87
|
+
Person.new(3, 'Alma', '555-5555').save
|
74
88
|
end
|
75
89
|
end
|
76
90
|
|
@@ -1,12 +1,12 @@
|
|
1
1
|
module ResourceMapper
|
2
2
|
module Actions
|
3
|
-
|
3
|
+
|
4
4
|
def index
|
5
5
|
load_collection
|
6
6
|
before :index
|
7
7
|
response_for :index
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
def show
|
11
11
|
load_object
|
12
12
|
before :show
|
@@ -16,7 +16,7 @@ module ResourceMapper
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def create
|
19
|
-
build_object
|
19
|
+
build_object(create_attrs)
|
20
20
|
load_object
|
21
21
|
before :create
|
22
22
|
if object.save
|
@@ -31,7 +31,7 @@ module ResourceMapper
|
|
31
31
|
def update
|
32
32
|
load_object
|
33
33
|
before :update
|
34
|
-
if object.update_attributes object_params
|
34
|
+
if object.update_attributes object_params(update_attrs)
|
35
35
|
after :update
|
36
36
|
response_for :update
|
37
37
|
else
|
@@ -51,6 +51,6 @@ module ResourceMapper
|
|
51
51
|
response_for :destroy_fails
|
52
52
|
end
|
53
53
|
end
|
54
|
-
|
54
|
+
|
55
55
|
end
|
56
56
|
end
|
@@ -45,21 +45,61 @@ module ResourceMapper
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
-
def
|
48
|
+
def read_attrs(*args)
|
49
49
|
class_eval <<-"end_eval", __FILE__, __LINE__
|
50
|
-
def
|
51
|
-
|
50
|
+
def index_attrs
|
51
|
+
#{args}
|
52
|
+
end
|
53
|
+
|
54
|
+
def show_attrs
|
55
|
+
#{args}
|
56
|
+
end
|
57
|
+
end_eval
|
58
|
+
end
|
59
|
+
|
60
|
+
def index_attrs(*args)
|
61
|
+
class_eval <<-"end_eval", __FILE__, __LINE__
|
62
|
+
def index_attrs
|
63
|
+
#{args}
|
64
|
+
end
|
65
|
+
end_eval
|
66
|
+
end
|
67
|
+
|
68
|
+
def show_attrs(*args)
|
69
|
+
class_eval <<-"end_eval", __FILE__, __LINE__
|
70
|
+
def show_attrs
|
71
|
+
#{args}
|
52
72
|
end
|
53
73
|
end_eval
|
54
74
|
end
|
55
75
|
|
56
|
-
def
|
76
|
+
def write_attrs(*args)
|
57
77
|
class_eval <<-"end_eval", __FILE__, __LINE__
|
58
|
-
def
|
78
|
+
def create_attrs
|
79
|
+
#{args}
|
80
|
+
end
|
81
|
+
|
82
|
+
def update_attrs
|
83
|
+
#{args}
|
84
|
+
end
|
85
|
+
end_eval
|
86
|
+
end
|
87
|
+
|
88
|
+
def create_attrs(*args)
|
89
|
+
class_eval <<-"end_eval", __FILE__, __LINE__
|
90
|
+
def create_attrs
|
91
|
+
#{args}
|
92
|
+
end
|
93
|
+
end_eval
|
94
|
+
end
|
95
|
+
|
96
|
+
def update_attrs(*args)
|
97
|
+
class_eval <<-"end_eval", __FILE__, __LINE__
|
98
|
+
def update_attrs
|
59
99
|
#{args}
|
60
100
|
end
|
61
101
|
end_eval
|
62
102
|
end
|
63
|
-
|
103
|
+
|
64
104
|
end
|
65
105
|
end
|
@@ -6,7 +6,7 @@ module ResourceMapper
|
|
6
6
|
include ResourceMapper::Actions
|
7
7
|
extend ResourceMapper::Accessors
|
8
8
|
extend ResourceMapper::ClassMethods
|
9
|
-
|
9
|
+
|
10
10
|
class_reader_writer :belongs_to, *NAME_ACCESSORS
|
11
11
|
NAME_ACCESSORS.each { |accessor| send(accessor, controller_name.singularize.underscore) }
|
12
12
|
|
@@ -20,29 +20,70 @@ module ResourceMapper
|
|
20
20
|
|
21
21
|
private
|
22
22
|
def self.init_default_actions(klass)
|
23
|
+
not_found = lambda { throw(:halt, [404, 'Not found.']) }
|
24
|
+
unprocessable_entity = lambda { throw(:halt, [422, 'Unprocessable entity.']) }
|
25
|
+
|
23
26
|
klass.class_eval do
|
24
|
-
index
|
25
|
-
|
27
|
+
index do
|
28
|
+
# wants.xml { collection.to_xml }
|
29
|
+
wants.json { collection.to_json(:only => index_attrs) }
|
30
|
+
# wants.yaml { collection.to_yaml }
|
31
|
+
# wants.plist { collection.to_plist }
|
32
|
+
end
|
33
|
+
|
26
34
|
show do
|
27
|
-
wants.
|
28
|
-
|
29
|
-
|
30
|
-
}
|
35
|
+
# wants.xml { object.to_xml }
|
36
|
+
wants.json { object.to_json(:only => show_attrs) }
|
37
|
+
# wants.yaml { object.to_yaml }
|
38
|
+
# wants.plist { object.to_plist }
|
39
|
+
|
40
|
+
failure.wants.html ¬_found
|
41
|
+
# failure.wants.xml ¬_found
|
42
|
+
failure.wants.json ¬_found
|
43
|
+
# failure.wants.yaml ¬_found
|
44
|
+
# failure.wants.plist ¬_found
|
31
45
|
end
|
32
46
|
|
33
47
|
create do
|
34
|
-
wants.
|
35
|
-
|
48
|
+
wants.html { object.to_json(:only => show_attrs) }
|
49
|
+
# wants.xml { object.to_xml }
|
50
|
+
wants.json { object.to_json(:only => show_attrs) }
|
51
|
+
# wants.yaml { object.to_xml }
|
52
|
+
# wants.plist { object.to_json }
|
53
|
+
|
54
|
+
failure.wants.html &unprocessable_entity
|
55
|
+
# failure.wants.xml &unprocessable_entity
|
56
|
+
failure.wants.json &unprocessable_entity
|
57
|
+
# failure.wants.yaml &unprocessable_entity
|
58
|
+
# failure.wants.plist &unprocessable_entity
|
36
59
|
end
|
37
60
|
|
38
61
|
update do
|
39
|
-
wants.
|
40
|
-
|
62
|
+
wants.html { object.to_json(:only => show_attrs) }
|
63
|
+
# wants.xml { object.to_xml }
|
64
|
+
wants.json { object.to_json(:only => show_attrs) }
|
65
|
+
# wants.yaml { object.to_xml }
|
66
|
+
# wants.plist { object.to_json }
|
67
|
+
|
68
|
+
failure.wants.html &unprocessable_entity
|
69
|
+
# failure.wants.xml &unprocessable_entity
|
70
|
+
failure.wants.json &unprocessable_entity
|
71
|
+
# failure.wants.yaml &unprocessable_entity
|
72
|
+
# failure.wants.plist &unprocessable_entity
|
41
73
|
end
|
42
74
|
|
43
75
|
destroy do
|
76
|
+
wants.html { "" }
|
77
|
+
# wants.xml { "" }
|
44
78
|
wants.json { "" }
|
45
|
-
|
79
|
+
# wants.yaml { "" }
|
80
|
+
# wants.plist { "" }
|
81
|
+
|
82
|
+
failure.wants.html &unprocessable_entity
|
83
|
+
# failure.wants.xml &unprocessable_entity
|
84
|
+
failure.wants.json &unprocessable_entity
|
85
|
+
# failure.wants.yaml &unprocessable_entity
|
86
|
+
# failure.wants.plist &unprocessable_entity
|
46
87
|
end
|
47
88
|
|
48
89
|
class << self
|
@@ -2,13 +2,13 @@ module ResourceMapper
|
|
2
2
|
module Helpers
|
3
3
|
module CurrentObjects
|
4
4
|
protected
|
5
|
-
# Used internally to return the model for your resource.
|
5
|
+
# Used internally to return the model for your resource.
|
6
6
|
#
|
7
7
|
def model
|
8
8
|
model_name.to_s.camelize.constantize
|
9
9
|
end
|
10
10
|
|
11
|
-
|
11
|
+
|
12
12
|
# Used to fetch the collection for the index method
|
13
13
|
#
|
14
14
|
# In order to customize the way the collection is fetched, to add something like pagination, for example, override this method.
|
@@ -16,20 +16,20 @@ module ResourceMapper
|
|
16
16
|
def collection
|
17
17
|
end_of_association_chain.all
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
# Returns the current param for key.
|
21
|
-
#
|
21
|
+
#
|
22
22
|
# Override this method if you'd like to use an alternate param name.
|
23
23
|
#
|
24
24
|
def param
|
25
25
|
params[:id]
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
# Returns the key used for finding.
|
29
29
|
def key
|
30
30
|
:id
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
33
|
# Used to fetch the current member object in all of the singular methods that operate on an existing member.
|
34
34
|
#
|
35
35
|
# Override this method if you'd like to fetch your objects in some alternate way, like using a permalink.
|
@@ -45,38 +45,41 @@ module ResourceMapper
|
|
45
45
|
@object ||= end_of_association_chain.first({key => param}) unless param.nil?
|
46
46
|
@object
|
47
47
|
end
|
48
|
-
|
48
|
+
|
49
49
|
# Used internally to load the member object in to an instance variable @#{model_name} (i.e. @post)
|
50
50
|
#
|
51
51
|
def load_object
|
52
52
|
instance_variable_set "@#{parent_type}", parent_object if parent?
|
53
53
|
instance_variable_set "@#{object_name}", object
|
54
54
|
end
|
55
|
-
|
55
|
+
|
56
56
|
# Used internally to load the collection in to an instance variable @#{model_name.pluralize} (i.e. @posts)
|
57
57
|
#
|
58
58
|
def load_collection
|
59
59
|
instance_variable_set "@#{parent_type}", parent_object if parent?
|
60
60
|
instance_variable_set "@#{object_name.to_s.pluralize}", collection
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
# Returns the form params. Defaults to params[model_name] (i.e. params["post"])
|
64
64
|
#
|
65
|
-
def object_params
|
65
|
+
def object_params(write_attrs = nil)
|
66
66
|
ret = params["#{object_name}"]
|
67
|
-
ret.delete_if { |k, v|
|
67
|
+
ret.delete_if { |k, v| write_attrs.index(k.to_sym).nil? } unless write_attrs.nil? || ret.nil?
|
68
68
|
ret
|
69
69
|
end
|
70
|
-
|
70
|
+
|
71
71
|
# Builds the object, but doesn't save it, during the new, and create action.
|
72
72
|
#
|
73
|
-
def build_object
|
74
|
-
@object = end_of_association_chain.send parent? ? :build : :new, object_params
|
73
|
+
def build_object(write_attrs = nil)
|
74
|
+
@object = end_of_association_chain.send parent? ? :build : :new, object_params(write_attrs)
|
75
75
|
end
|
76
76
|
|
77
|
-
def
|
78
|
-
def
|
79
|
-
|
77
|
+
def index_attrs ; nil end
|
78
|
+
def show_attrs ; nil end
|
79
|
+
|
80
|
+
def create_attrs ; nil end
|
81
|
+
def update_attrs ; nil end
|
82
|
+
|
80
83
|
end
|
81
84
|
end
|
82
85
|
end
|
data/lib/sinatra/resource.rb
CHANGED
@@ -3,7 +3,7 @@ require 'active_support/inflector'
|
|
3
3
|
|
4
4
|
module Sinatra
|
5
5
|
module Resource
|
6
|
-
DEFAULT_FORMAT = :
|
6
|
+
DEFAULT_FORMAT = :html
|
7
7
|
|
8
8
|
def get_with_formats(path, options = {}, &block)
|
9
9
|
route_method_for_formats :get, path, options, &block
|
@@ -27,6 +27,8 @@ module Sinatra
|
|
27
27
|
klass.class_eval <<-"end_eval", __FILE__, __LINE__
|
28
28
|
attr_accessor :app, :wants
|
29
29
|
delegate :request, :response, :params, :session, :to => :app
|
30
|
+
# Link in the various view renderers
|
31
|
+
delegate :haml, :sass, :erb, :erubris, :builder, :less, :to => :app
|
30
32
|
|
31
33
|
def self.controller_name
|
32
34
|
"#{model.to_s.demodulize.downcase.pluralize}"
|
@@ -35,7 +37,7 @@ module Sinatra
|
|
35
37
|
def self.model_name
|
36
38
|
"#{model.to_s.demodulize.downcase}"
|
37
39
|
end
|
38
|
-
|
40
|
+
|
39
41
|
def self.model
|
40
42
|
model
|
41
43
|
end
|
@@ -68,10 +70,15 @@ module Sinatra
|
|
68
70
|
block.bind(self).call(wants)
|
69
71
|
halt 404 if wants[format].nil?
|
70
72
|
|
73
|
+
# TODO: Add support for data in format's other than form-data
|
74
|
+
# params['person'] = case format
|
75
|
+
# when :json ; ActiveSupport::JSON.decode(request.body)
|
76
|
+
# end
|
77
|
+
|
71
78
|
wants[format].call
|
72
79
|
end
|
73
80
|
end
|
74
|
-
|
81
|
+
|
75
82
|
def route_handler(resource_klass, method)
|
76
83
|
lambda do |wants|
|
77
84
|
resource = resource_klass.new
|
@@ -80,15 +87,15 @@ module Sinatra
|
|
80
87
|
resource.method(method).call
|
81
88
|
end
|
82
89
|
end
|
83
|
-
|
90
|
+
|
84
91
|
def setup_routes(model, resource)
|
85
92
|
name = model.to_s.demodulize.downcase
|
86
93
|
|
87
|
-
get_with_formats "/#{name.pluralize}",
|
88
|
-
get_with_formats "/#{name}/:id",
|
89
|
-
post_with_formats "/#{name}",
|
90
|
-
put_with_formats "/#{name}/:id",
|
91
|
-
delete_with_formats "/#{name}/:id",
|
94
|
+
get_with_formats "/#{name.pluralize}", {}, &route_handler(resource, :index) unless resource.instance_methods.index(:index).nil?
|
95
|
+
get_with_formats "/#{name.pluralize}/:id", {}, &route_handler(resource, :show) unless resource.instance_methods.index(:show).nil?
|
96
|
+
post_with_formats "/#{name.pluralize}", {}, &route_handler(resource, :create) unless resource.instance_methods.index(:create).nil?
|
97
|
+
put_with_formats "/#{name.pluralize}/:id", {}, &route_handler(resource, :update) unless resource.instance_methods.index(:update).nil?
|
98
|
+
delete_with_formats "/#{name.pluralize}/:id", {}, &route_handler(resource, :destroy) unless resource.instance_methods.index(:destroy).nil?
|
92
99
|
end
|
93
100
|
end
|
94
101
|
|
data/resource_mapper.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{resource_mapper}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Adam Elliot"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-10-28}
|
13
13
|
s.description = %q{Creates a resource for a model in sinatra}
|
14
14
|
s.email = %q{adam@adamelliot.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -58,23 +58,29 @@ Gem::Specification.new do |s|
|
|
58
58
|
|
59
59
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
60
60
|
s.add_runtime_dependency(%q<sinatra>, [">= 1.0"])
|
61
|
-
s.add_runtime_dependency(%q<
|
62
|
-
s.add_runtime_dependency(%q<
|
61
|
+
s.add_runtime_dependency(%q<plist>, [">= 3.1.0"])
|
62
|
+
s.add_runtime_dependency(%q<builder>, [">= 2.1.2"])
|
63
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 3.0.0"])
|
64
|
+
s.add_runtime_dependency(%q<mongo_mapper>, [">= 0.8.4"])
|
63
65
|
s.add_development_dependency(%q<micronaut>, [">= 0.3.0"])
|
64
|
-
s.add_development_dependency(%q<rack-test>, [">= 0.5.
|
66
|
+
s.add_development_dependency(%q<rack-test>, [">= 0.5.5"])
|
65
67
|
else
|
66
68
|
s.add_dependency(%q<sinatra>, [">= 1.0"])
|
67
|
-
s.add_dependency(%q<
|
68
|
-
s.add_dependency(%q<
|
69
|
+
s.add_dependency(%q<plist>, [">= 3.1.0"])
|
70
|
+
s.add_dependency(%q<builder>, [">= 2.1.2"])
|
71
|
+
s.add_dependency(%q<activesupport>, [">= 3.0.0"])
|
72
|
+
s.add_dependency(%q<mongo_mapper>, [">= 0.8.4"])
|
69
73
|
s.add_dependency(%q<micronaut>, [">= 0.3.0"])
|
70
|
-
s.add_dependency(%q<rack-test>, [">= 0.5.
|
74
|
+
s.add_dependency(%q<rack-test>, [">= 0.5.5"])
|
71
75
|
end
|
72
76
|
else
|
73
77
|
s.add_dependency(%q<sinatra>, [">= 1.0"])
|
74
|
-
s.add_dependency(%q<
|
75
|
-
s.add_dependency(%q<
|
78
|
+
s.add_dependency(%q<plist>, [">= 3.1.0"])
|
79
|
+
s.add_dependency(%q<builder>, [">= 2.1.2"])
|
80
|
+
s.add_dependency(%q<activesupport>, [">= 3.0.0"])
|
81
|
+
s.add_dependency(%q<mongo_mapper>, [">= 0.8.4"])
|
76
82
|
s.add_dependency(%q<micronaut>, [">= 0.3.0"])
|
77
|
-
s.add_dependency(%q<rack-test>, [">= 0.5.
|
83
|
+
s.add_dependency(%q<rack-test>, [">= 0.5.5"])
|
78
84
|
end
|
79
85
|
end
|
80
86
|
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resource_mapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 29
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
7
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
8
|
+
- 4
|
9
|
+
version: 0.0.4
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Adam Elliot
|
@@ -15,7 +14,7 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2010-
|
17
|
+
date: 2010-10-28 00:00:00 -06:00
|
19
18
|
default_executable:
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
@@ -26,7 +25,6 @@ dependencies:
|
|
26
25
|
requirements:
|
27
26
|
- - ">="
|
28
27
|
- !ruby/object:Gem::Version
|
29
|
-
hash: 15
|
30
28
|
segments:
|
31
29
|
- 1
|
32
30
|
- 0
|
@@ -34,69 +32,95 @@ dependencies:
|
|
34
32
|
type: :runtime
|
35
33
|
version_requirements: *id001
|
36
34
|
- !ruby/object:Gem::Dependency
|
37
|
-
name:
|
35
|
+
name: plist
|
38
36
|
prerelease: false
|
39
37
|
requirement: &id002 !ruby/object:Gem::Requirement
|
40
38
|
none: false
|
41
39
|
requirements:
|
42
40
|
- - ">="
|
43
41
|
- !ruby/object:Gem::Version
|
44
|
-
hash: 9
|
45
42
|
segments:
|
46
|
-
- 2
|
47
43
|
- 3
|
48
|
-
-
|
49
|
-
|
44
|
+
- 1
|
45
|
+
- 0
|
46
|
+
version: 3.1.0
|
50
47
|
type: :runtime
|
51
48
|
version_requirements: *id002
|
52
49
|
- !ruby/object:Gem::Dependency
|
53
|
-
name:
|
50
|
+
name: builder
|
54
51
|
prerelease: false
|
55
52
|
requirement: &id003 !ruby/object:Gem::Requirement
|
56
53
|
none: false
|
57
54
|
requirements:
|
58
55
|
- - ">="
|
59
56
|
- !ruby/object:Gem::Version
|
60
|
-
hash: 15
|
61
57
|
segments:
|
62
|
-
-
|
63
|
-
-
|
64
|
-
-
|
65
|
-
version:
|
58
|
+
- 2
|
59
|
+
- 1
|
60
|
+
- 2
|
61
|
+
version: 2.1.2
|
66
62
|
type: :runtime
|
67
63
|
version_requirements: *id003
|
68
64
|
- !ruby/object:Gem::Dependency
|
69
|
-
name:
|
65
|
+
name: activesupport
|
70
66
|
prerelease: false
|
71
67
|
requirement: &id004 !ruby/object:Gem::Requirement
|
72
68
|
none: false
|
73
69
|
requirements:
|
74
70
|
- - ">="
|
75
71
|
- !ruby/object:Gem::Version
|
76
|
-
|
72
|
+
segments:
|
73
|
+
- 3
|
74
|
+
- 0
|
75
|
+
- 0
|
76
|
+
version: 3.0.0
|
77
|
+
type: :runtime
|
78
|
+
version_requirements: *id004
|
79
|
+
- !ruby/object:Gem::Dependency
|
80
|
+
name: mongo_mapper
|
81
|
+
prerelease: false
|
82
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
- 8
|
90
|
+
- 4
|
91
|
+
version: 0.8.4
|
92
|
+
type: :runtime
|
93
|
+
version_requirements: *id005
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: micronaut
|
96
|
+
prerelease: false
|
97
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
77
102
|
segments:
|
78
103
|
- 0
|
79
104
|
- 3
|
80
105
|
- 0
|
81
106
|
version: 0.3.0
|
82
107
|
type: :development
|
83
|
-
version_requirements: *
|
108
|
+
version_requirements: *id006
|
84
109
|
- !ruby/object:Gem::Dependency
|
85
110
|
name: rack-test
|
86
111
|
prerelease: false
|
87
|
-
requirement: &
|
112
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
88
113
|
none: false
|
89
114
|
requirements:
|
90
115
|
- - ">="
|
91
116
|
- !ruby/object:Gem::Version
|
92
|
-
hash: 13
|
93
117
|
segments:
|
94
118
|
- 0
|
95
119
|
- 5
|
96
|
-
-
|
97
|
-
version: 0.5.
|
120
|
+
- 5
|
121
|
+
version: 0.5.5
|
98
122
|
type: :development
|
99
|
-
version_requirements: *
|
123
|
+
version_requirements: *id007
|
100
124
|
description: Creates a resource for a model in sinatra
|
101
125
|
email: adam@adamelliot.com
|
102
126
|
executables: []
|
@@ -144,7 +168,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
144
168
|
requirements:
|
145
169
|
- - ">="
|
146
170
|
- !ruby/object:Gem::Version
|
147
|
-
hash: 3
|
148
171
|
segments:
|
149
172
|
- 0
|
150
173
|
version: "0"
|
@@ -153,7 +176,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
153
176
|
requirements:
|
154
177
|
- - ">="
|
155
178
|
- !ruby/object:Gem::Version
|
156
|
-
hash: 3
|
157
179
|
segments:
|
158
180
|
- 0
|
159
181
|
version: "0"
|