Class: Utopia::Controller::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/controller/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.actionsObject



51
52
53
# File 'lib/utopia/controller/base.rb', line 51

def actions
  @actions ||= Action.new
end

.base_pathObject



26
27
28
# File 'lib/utopia/controller/base.rb', line 26

def self.base_path
  self.const_get(:BASE_PATH)
end

.controllerObject



34
35
36
# File 'lib/utopia/controller/base.rb', line 34

def self.controller
  self.const_get(:CONTROLLER)
end

.direct?(path) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/utopia/controller/base.rb', line 39

def direct?(path)
  path.dirname == uri_path
end

.lookup(path) ⇒ Object



63
64
65
66
67
# File 'lib/utopia/controller/base.rb', line 63

def lookup(path)
  relative_path = (path - uri_path).to_a
  
  possible_actions = actions.select(relative_path)
end

.match(pattern, &block) ⇒ Object



47
48
49
# File 'lib/utopia/controller/base.rb', line 47

def match(pattern, &block)
  patterns << [pattern, block]
end

.on(first, *path, **options, &block) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/utopia/controller/base.rb', line 55

def on(first, *path, **options, &block)
  if first.is_a? Symbol
    first = ['**', first]
  end
  
  actions.define(Path.split(first) + path, options, &block)
end

.patternsObject



43
44
45
# File 'lib/utopia/controller/base.rb', line 43

def patterns
  @patterns ||= []
end

.uri_pathObject



30
31
32
# File 'lib/utopia/controller/base.rb', line 30

def self.uri_path
  self.const_get(:URI_PATH)
end

Instance Method Details

#actions_for_request(request, path) ⇒ Object

Given a path, look up all matched actions.



71
72
73
# File 'lib/utopia/controller/base.rb', line 71

def actions_for_request(request, path)
  self.class.lookup(path)
end

#body_for(status, headers, options) ⇒ Object

Generate the body for the given status, headers and options.



151
152
153
154
155
156
157
# File 'lib/utopia/controller/base.rb', line 151

def body_for(status, headers, options)
  if body = options[:body]
    return body
  elsif content = options[:content]
    return [content]
  end
end

#call(env) ⇒ Object

Call into the next app as defined by rack.



104
105
106
# File 'lib/utopia/controller/base.rb', line 104

def call(env)
  self.class.controller.app.call(env)
end

#catch_responseObject



75
76
77
78
79
# File 'lib/utopia/controller/base.rb', line 75

def catch_response
  catch(:response) do
    yield and nil
  end
end

#copy_instance_variables(from) ⇒ Object

Copy the instance variables from the previous controller to the next controller (usually only a few). This allows controllers to share effectively the same instance variables while still being separate classes/instances.



97
98
99
100
101
# File 'lib/utopia/controller/base.rb', line 97

def copy_instance_variables(from)
  from.instance_variables.each do |name|
    instance_variable_set(name, from.instance_variable_get(name))
  end
end

#fail!(error = 400, message = nil) ⇒ Object

Respond with an error which indiciates some kind of failure.



132
133
134
135
136
137
# File 'lib/utopia/controller/base.rb', line 132

def fail!(error = 400, message = nil)
  status = HTTP::Status.new(error, 400...600)
  
  message ||= status.to_s
  respond! [status.to_i, {}, [message]]
end

#goto!(target, status = 302) ⇒ Object

Controller relative redirect.



127
128
129
# File 'lib/utopia/controller/base.rb', line 127

def goto!(target, status = 302)
  redirect! self.class.uri_path + target
end

#ignore!Object

This will cause the controller middleware to pass on the request.



114
115
116
# File 'lib/utopia/controller/base.rb', line 114

def ignore!
  throw :response, nil
end

#passthrough(request, path) ⇒ Object

Given a request, call associated actions if at least one exists.



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/utopia/controller/base.rb', line 82

def passthrough(request, path)
  actions = actions_for_request(request, path)
  
  unless actions.empty?
    return catch_response do
      actions.each do |action|
        action.invoke!(self, request, path)
      end
    end
  end
  
  return nil
end

#process!(request, path) ⇒ Object

Return nil if this controller didn’t do anything. Request will keep on processing. Return a valid rack response if the controller can do so.



163
164
165
# File 'lib/utopia/controller/base.rb', line 163

def process!(request, path)
  passthrough(request, path)
end

#redirect!(target, status = 302) ⇒ Object

Request relative redirect. Respond with a redirect to the given target.



119
120
121
122
123
124
# File 'lib/utopia/controller/base.rb', line 119

def redirect! (target, status = 302)
  status = HTTP::Status.new(status, 300...400)
  location = target.to_s
  
  respond! [status.to_i, {HTTP::LOCATION => location}, [status.to_s]]
end

#respond!(response) ⇒ Object

This will cause the middleware to generate a response.



109
110
111
# File 'lib/utopia/controller/base.rb', line 109

def respond!(response)
  throw :response, response
end

#succeed!(status: 200, headers: {}, **options) ⇒ Object Also known as: success!



139
140
141
142
143
144
145
146
147
148
# File 'lib/utopia/controller/base.rb', line 139

def succeed!(status: 200, headers: {}, **options)
  status = HTTP::Status.new(status, 200...300)
  
  if options[:type]
    headers[Rack::CONTENT_TYPE] = options[:type].to_s
  end
  
  body = body_for(status, headers, options)
  respond! [status.to_i, headers, body || []]
end