Permalink
Browse files

don't discriminate against hash based stores

Signed-off-by: Pat Nakajima <patnakajima@gmail.com>
  • Loading branch information...
1 parent 919f6bb commit e2ee15d57bb6831af4769c94ce01604e0546c1d6 Jake Howerton committed with Mar 10, 2009
Showing with 13 additions and 13 deletions.
  1. +13 −13 lib/sinatras-hat/responder.rb
View
26 lib/sinatras-hat/responder.rb
@@ -5,13 +5,13 @@ module Hat
# context of a new Response object, or serializes the data.
class Responder
delegate :model, :to => :maker
-
+
attr_reader :maker
-
+
def initialize(maker)
@maker = maker
end
-
+
def defaults
@defaults ||= {
:show => {
@@ -48,20 +48,20 @@ def defaults
}
}
end
-
+
# Called when a request is handled successfully. For most GET
# requests, this is always the case. For update/create actions,
# it is when the record is created/updated successfully.
def success(name, request, data)
handle(:success, name, request, data)
end
-
+
# Called when a request is not able to handled. This could be
# because a record could not be created or saved.
def failure(name, request, data)
handle(:failure, name, request, data)
end
-
+
# Serializes the data passed in, first looking for a custom formatter,
# then falling back on trying to call to_[format] on the data. If neither
# are available, returns an error with the status code 406.
@@ -72,9 +72,9 @@ def serialize(data, format)
formatter = to_format(name)
formatter[data] ? [formatter[data], mime] : nil
end
-
+
private
-
+
def handle(result, name, request, data)
if format = request.params[:format] || maker.format
response, mime = serialize(data, format)
@@ -86,15 +86,15 @@ def handle(result, name, request, data)
response.instance_exec(data, &defaults[name][result])
end
end
-
+
def get_mime_type(format)
Rack::Mime::MIME_TYPES['.' + format.to_s]
end
-
+
def ivar_name(data)
- "@" + (data.respond_to?(:each) ? model.plural : model.singular)
+ "@" + (data.kind_of?(Array) ? model.plural : model.singular)
end
-
+
def to_format(name)
maker.formats[name] || Proc.new do |data|
method_name = "to_#{name}"
@@ -103,4 +103,4 @@ def to_format(name)
end
end
end
-end
+end

0 comments on commit e2ee15d

Please sign in to comment.