lib/make_restful/specification/builder.rb in make_restful-0.1.1 vs lib/make_restful/specification/builder.rb in make_restful-0.1.2
- old
+ new
@@ -1,67 +1,75 @@
class MakeRestful::Specification::Builder
- attr_reader :controller, :_methods, :_description, :_formats, :_attributes, :_resource, :_default_methods, :_default_attributes, :_default_formats
+ attr_reader :_controller_, :_methods_, :_description_, :_formats_, :_attributes_, :_resource_, :_default_methods_, :_default_attributes_, :_default_formats_
+ private
+
def initialize(controller, &block)
- @_controller = controller
- @_methods = []
- @_default_methods = controller.supported_methods.collect do |m|
- method m[:method], path: m[:path], returns: m[:returns]
- end
- @_methods = []
- @_attributes = []
- @_default_attributes = controller.resource_class.new.attributes.keys
- @_default_formats = controller.allowed_formats
- @_formats = []
- @_description = "Api for #{controller.resource_name.titleize}"
- @_resource = controller.instance_name
+ @_controller_ = controller
+ @_methods_ = []
+ @_default_methods_ = default_supported_methods
+ @_methods_ = []
+ @_attributes_ = []
+ @_default_attributes_ = controller.resource_class.new.attributes.keys
+ @_default_formats_ = controller.allowed_formats
+ @_formats_ = []
+ @_description_ = "Api for #{controller.resource_name.titleize}"
+ @_resource_ = controller.instance_name
instance_eval(&block) if block_given?
end
def method(method, options={})
method = {
method: method,
path: options[:path],
returns: options[:returns]
}
- @_methods << method
+ @_methods_ << method
method
end
def description(string)
- @_description = string
+ @_description_ = string
end
def resource(string)
- @_resource = string
+ @_resource_ = string
end
def attribute(attr)
- @_attributes << attr
+ @_attributes_ << attr
attr
end
def attributes(*args)
- @_attributes += args.flatten
+ @_attributes_ += args.flatten
end
def format(attr)
- @_formats << attr
+ @_formats_ << attr
attr
end
def formats(*args)
- @_formats += args
+ @_formats_ += args
end
+ def default_supported_methods
+ method :get, path: "/", returns: "collection" if _controller_.allow_method?(:list) || _controller_.allow_method?(:index)
+ method :post, path: "/", returns: "resource" if _controller_.allow_method?(:create) || _controller_.allow_method?(:post)
+ method :get, path: "/:id", returns: "resource" if _controller_.allow_method?(:show) || _controller_.allow_method?(:get)
+ method :put, path: "/:id", returns: "resource" if _controller_.allow_method?(:update) || _controller_.allow_method?(:put)
+ method :delete, path: "/:id", returns: "success" if _controller_.allow_method?(:destroy) || _controller_.allow_method?(:delete)
+ end
+
def render
{
- resource: _resource,
- formats: _formats.present? ? _formats : _default_formats,
- description: _description,
- attributes: _attributes.present? ? _attributes : _default_attributes,
- methods: _methods.present? ? _methods : _default_methods
+ resource: _resource_,
+ formats: _formats_.present? ? _formats_ : _default_formats_,
+ description: _description_,
+ attributes: _attributes_.present? ? _attributes_ : _default_attributes_,
+ methods: _methods_.present? ? _methods_ : _default_methods_
}
end
end
\ No newline at end of file