lib/builder.rb in sinatra_resource-0.1.0 vs lib/builder.rb in sinatra_resource-0.2.0
- old
+ new
@@ -1,11 +1,21 @@
module SinatraResource
class Builder
def initialize(klass)
- @klass = klass
+ @klass = klass
+
+ @resource_config = @klass.resource_config
+ @child_association = @resource_config[:child_association]
+ @model = @resource_config[:model]
+ @parent = @resource_config[:parent]
+ @path = @resource_config[:path]
+ if @parent
+ @parent_resource_config = @parent.resource_config
+ @parent_model = @parent_resource_config[:model]
+ end
end
def build
build_get_one
build_get_many
@@ -14,67 +24,155 @@
build_delete
build_helpers
end
def build_get_one
- @klass.get '/:id/?' do
- id = params.delete("id")
- role = get_role(id)
- check_permission(:read, role)
- check_params(:read, role)
- document = find_document!(id)
- resource = build_resource(role, document)
- display(:read, resource)
+ model = @model
+ resource_config = @resource_config
+ if !@parent
+ @klass.get '/:id/?' do
+ id = params.delete("id")
+ role = get_role(model, id)
+ document = document_for_get_one(role, model, resource_config, true, id, nil, nil)
+ resource = build_resource(role, document, resource_config)
+ display(:read, resource, resource_config)
+ end
+ else
+ association = @child_association
+ parent_model = @parent_model
+ parent_resource_config = @parent_resource_config
+ path = @path
+ @parent.get "/:parent_id/#{path}/:id/?" do
+ id = params.delete("id")
+ parent_id = params.delete("parent_id")
+ parent_role = get_role(parent_model, parent_id)
+ parent_document = document_for_get_one(parent_role, parent_model, parent_resource_config, false, parent_id, nil, nil)
+ # ------
+ role = get_role(model, id)
+ document = document_for_get_one(role, model, resource_config, true, id, parent_document, association)
+ resource = build_resource(role, document, resource_config)
+ display(:read, resource, resource_config)
+ end
end
end
def build_get_many
- @klass.get '/?' do
- role = get_role
- check_permission(:read, role)
- check_params(:read, role)
- documents = find_documents!
- resources = build_resources(documents)
- display(:read, resources)
+ model = @model
+ resource_config = @resource_config
+ if !@parent
+ @klass.get '/?' do
+ role = get_role(model)
+ documents = documents_for_get_many(role, model, resource_config, true, nil, nil)
+ resources = build_resources(documents, resource_config)
+ display(:read, resources, resource_config)
+ end
+ else
+ association = @child_association
+ parent_model = @parent_model
+ parent_resource_config = @parent_resource_config
+ path = @path
+ @parent.get "/:parent_id/#{path}/?" do
+ parent_id = params.delete("parent_id")
+ parent_role = get_role(parent_model, parent_id)
+ parent_document = document_for_get_one(parent_role, parent_model, parent_resource_config, false, parent_id, nil, nil)
+ # ------
+ role = get_role(model)
+ documents = documents_for_get_many(role, model, resource_config, true, parent_document, association)
+ resources = build_resources(documents, resource_config)
+ display(:read, resources, resource_config)
+ end
end
end
def build_post
- @klass.post '/?' do
- role = get_role
- check_permission(:create, role)
- check_params(:create, role)
- document = create_document!
- resource = build_resource(role, document)
- display(:create, resource)
+ model = @model
+ resource_config = @resource_config
+ if !@parent
+ @klass.post '/?' do
+ role = get_role(model)
+ document = document_for_post(role, model, resource_config, true, nil, nil)
+ resource = build_resource(role, document, resource_config)
+ display(:create, resource, resource_config)
+ end
+ else
+ association = @child_association
+ parent_model = @parent_model
+ parent_resource_config = @parent_resource_config
+ path = @path
+ @parent.post "/:parent_id/#{path}/?" do
+ parent_id = params.delete("parent_id")
+ parent_role = get_role(parent_model, parent_id)
+ parent_document = document_for_get_one(parent_role, parent_model, parent_resource_config, false, parent_id, nil, nil)
+ # ------
+ role = get_role(model)
+ document = document_for_post(role, model, resource_config, true, parent_document, association)
+ resource = build_resource(role, document, resource_config)
+ display(:create, resource, resource_config)
+ end
end
end
def build_put
- @klass.put '/:id/?' do
- id = params.delete("id")
- role = get_role(id)
- check_permission(:update, role)
- check_params(:update, role)
- document = update_document!(id)
- resource = build_resource(role, document)
- display(:update, resource)
+ model = @model
+ resource_config = @resource_config
+ if !@parent
+ @klass.put '/:id/?' do
+ id = params.delete("id")
+ role = get_role(model, id)
+ document = document_for_put(role, model, resource_config, true, id, nil, nil)
+ resource = build_resource(role, document, resource_config)
+ display(:update, resource, resource_config)
+ end
+ else
+ association = @child_association
+ parent_model = @parent_model
+ parent_resource_config = @parent_resource_config
+ path = @path
+ @parent.put "/:parent_id/#{path}/:id/?" do
+ id = params.delete("id")
+ parent_id = params.delete("parent_id")
+ parent_role = get_role(parent_model, parent_id)
+ parent_document = document_for_get_one(parent_role, parent_model, parent_resource_config, false, parent_id, id, id)
+ # ------
+ role = get_role(model, id)
+ document = document_for_put(role, model, resource_config, true, id, parent_document, association)
+ resource = build_resource(role, document, resource_config)
+ display(:update, resource, resource_config)
+ end
end
end
def build_delete
- @klass.delete '/:id/?' do
- id = params.delete("id")
- role = get_role(id)
- check_permission(:delete, role)
- check_params(:delete, role)
- delete_document!(id)
- display(:delete, "")
+ model = @model
+ resource_config = @resource_config
+ if !@parent
+ @klass.delete '/:id/?' do
+ id = params.delete("id")
+ role = get_role(model, id)
+ document_for_delete(role, model, resource_config, true, id, nil, nil)
+ display(:delete, "", resource_config)
+ end
+ else
+ association = @child_association
+ parent_model = @parent_model
+ parent_resource_config = @parent_resource_config
+ path = @path
+ @parent.delete "/:parent_id/#{path}/:id/?" do
+ id = params.delete("id")
+ parent_id = params.delete("parent_id")
+ parent_role = get_role(parent_model, parent_id)
+ parent_document = document_for_get_one(parent_role, parent_model, parent_resource_config, false, parent_id, nil, nil)
+ # ------
+ role = get_role(model, id)
+ document_for_delete(role, model, resource_config, true, id, parent_document, association)
+ display(:delete, "", resource_config)
+ end
end
end
def build_helpers
@klass.helpers do
+ include ActionDefinitions
include Helpers
include MongoHelpers
end
end