Class: Sinatra::Hat::Responder
Overview
The responder assigns data to instance variables, then either gets the appropriate response proc and instance_exec’s it in the context of a new Response object, or serializes the data.
Instance Attribute Summary collapse
-
#maker ⇒ Object
readonly
Returns the value of attribute maker.
Instance Method Summary collapse
- #defaults ⇒ Object
- #failure(name, request, data) ⇒ Object
-
#initialize(maker) ⇒ Responder
constructor
A new instance of Responder.
- #not_found(request) ⇒ Object
- #serialize(request, data) ⇒ Object
- #success(name, request, data) ⇒ Object
Constructor Details
#initialize(maker) ⇒ Responder
Returns a new instance of Responder.
11 12 13 |
# File 'lib/sinatras-hat/responder.rb', line 11 def initialize(maker) @maker = maker end |
Instance Attribute Details
#maker ⇒ Object (readonly)
Returns the value of attribute maker.
9 10 11 |
# File 'lib/sinatras-hat/responder.rb', line 9 def maker @maker end |
Instance Method Details
#defaults ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/sinatras-hat/responder.rb', line 15 def defaults @defaults ||= { :show => { :success => proc { |data| render(:show) }, :failure => proc { |data| redirect('/') } }, :index => { :success => proc { |data| render(:index) }, :failure => proc { |data| redirect('/') } }, :create => { :success => proc { |data| redirect(data) }, :failure => proc { |data| render(:new) } }, :new => { :success => proc { |data| render(:new) }, :failure => proc { |data| redirect('/') } }, :edit => { :success => proc { |data| render(:edit) } }, :destroy => { :success => proc { |data| redirect(resource_path('/')) } }, :update => { :success => proc { |data| redirect(data) }, :failure => proc { |data| render(:edit) } } } end |
#failure(name, request, data) ⇒ Object
56 57 58 |
# File 'lib/sinatras-hat/responder.rb', line 56 def failure(name, request, data) handle(:failure, name, request, data) end |
#not_found(request) ⇒ Object
66 67 68 |
# File 'lib/sinatras-hat/responder.rb', line 66 def not_found(request) request.not_found end |
#serialize(request, data) ⇒ Object
60 61 62 63 64 |
# File 'lib/sinatras-hat/responder.rb', line 60 def serialize(request, data) name = request.params[:format].to_sym formatter = to_format(name) formatter[data] || request.error(406) end |
#success(name, request, data) ⇒ Object
52 53 54 |
# File 'lib/sinatras-hat/responder.rb', line 52 def success(name, request, data) handle(:success, name, request, data) end |