Skip to content

Commit

Permalink
Start sinatra extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephHalter committed May 30, 2011
1 parent 67fcdce commit 8746317
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/bubble.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Bubble
end
require File.expand_path("../bubble/sinatra", __FILE__)
56 changes: 56 additions & 0 deletions lib/bubble/sinatra.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module Bubble
module Sinatra
module Extensions
def resource(name, opts={})
actions = [:index, :show, :create, :update, :destroy]
actions &= opts.delete(:only) if opts.has_key? :only
actions -= opts.delete(:except) if opts.has_key? :except
index name, opts if actions.include? :index
show name, opts if actions.include? :show
create name if actions.include? :create
update name if actions.include? :update
destroy name if actions.include? :destroy
end
private
def index(name, opts={})
get "/#{name}" do
opts = find_defaults opts
collection = opts[:class].search(params).collect{|c| c.as_json :index}
output = {
name => collection,
:link => [
{:rel => "self", :href => "#{base_url}/#{name}"},
],
}
render :json => output
end
end
def show(name, opts={})
end
def create(name, opts={})
end
def update(name, opts={})
end
def destroy(name, opts={})
end
def base_url
"#{request.scheme}://#{request.host_with_port}"
end
def render(hash)
if hash[:json]
content_type :json
Yajl::Encoder.encode hash[:json]
end
end
def find_defaults(opts)
opts[:class_name] ||= name.to_s.split("_").collect(&:capitalize).join
opts[:class] = opts[:class_name].constantize
opts
end
end

def self.registered(app)
app.helpers Extensions
end
end
end

0 comments on commit 8746317

Please sign in to comment.