Class: Sinatra::Hat::Responder

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatras-hat/responder.rb

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

Instance Method Summary collapse

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

#makerObject (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

#defaultsObject



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