hanami-controller 1.3.3 → 2.0.0.alpha1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +46 -7
- data/README.md +295 -537
- data/hanami-controller.gemspec +3 -3
- data/lib/hanami/action.rb +653 -38
- data/lib/hanami/action/base_params.rb +2 -2
- data/lib/hanami/action/cache.rb +1 -139
- data/lib/hanami/action/cache/cache_control.rb +4 -4
- data/lib/hanami/action/cache/conditional_get.rb +4 -5
- data/lib/hanami/action/cache/directives.rb +1 -1
- data/lib/hanami/action/cache/expires.rb +3 -3
- data/lib/hanami/action/cookie_jar.rb +3 -3
- data/lib/hanami/action/cookies.rb +3 -62
- data/lib/hanami/action/flash.rb +2 -2
- data/lib/hanami/action/glue.rb +5 -31
- data/lib/hanami/action/halt.rb +12 -0
- data/lib/hanami/action/mime.rb +77 -491
- data/lib/hanami/action/params.rb +3 -3
- data/lib/hanami/action/rack/file.rb +1 -1
- data/lib/hanami/action/request.rb +30 -20
- data/lib/hanami/action/response.rb +174 -0
- data/lib/hanami/action/session.rb +8 -117
- data/lib/hanami/action/validatable.rb +2 -2
- data/lib/hanami/controller.rb +0 -210
- data/lib/hanami/controller/configuration.rb +51 -506
- data/lib/hanami/controller/version.rb +1 -1
- metadata +12 -21
- data/lib/hanami/action/callable.rb +0 -92
- data/lib/hanami/action/callbacks.rb +0 -214
- data/lib/hanami/action/configurable.rb +0 -50
- data/lib/hanami/action/exposable.rb +0 -126
- data/lib/hanami/action/exposable/guard.rb +0 -104
- data/lib/hanami/action/head.rb +0 -121
- data/lib/hanami/action/rack.rb +0 -411
- data/lib/hanami/action/rack/callable.rb +0 -47
- data/lib/hanami/action/rack/errors.rb +0 -53
- data/lib/hanami/action/redirect.rb +0 -59
- data/lib/hanami/action/throwable.rb +0 -169
data/lib/hanami/controller.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
require 'hanami/utils/class_attribute'
|
2
1
|
require 'hanami/action'
|
3
2
|
require 'hanami/controller/configuration'
|
4
3
|
require 'hanami/controller/version'
|
@@ -62,214 +61,5 @@ module Hanami
|
|
62
61
|
super("To use `#{session_method}', add `include Hanami::Action::Session`.")
|
63
62
|
end
|
64
63
|
end
|
65
|
-
|
66
|
-
include Utils::ClassAttribute
|
67
|
-
|
68
|
-
# Framework configuration
|
69
|
-
#
|
70
|
-
# @since 0.2.0
|
71
|
-
# @api private
|
72
|
-
class_attribute :configuration
|
73
|
-
self.configuration = Configuration.new
|
74
|
-
|
75
|
-
# Configure the framework.
|
76
|
-
# It yields the given block in the context of the configuration
|
77
|
-
#
|
78
|
-
# @param blk [Proc] the configuration block
|
79
|
-
#
|
80
|
-
# @since 0.2.0
|
81
|
-
#
|
82
|
-
# @see Hanami::Controller::Configuration
|
83
|
-
#
|
84
|
-
# @example
|
85
|
-
# require 'hanami/controller'
|
86
|
-
#
|
87
|
-
# Hanami::Controller.configure do
|
88
|
-
# handle_exceptions false
|
89
|
-
# end
|
90
|
-
def self.configure(&blk)
|
91
|
-
configuration.instance_eval(&blk)
|
92
|
-
end
|
93
|
-
|
94
|
-
# Duplicate Hanami::Controller in order to create a new separated instance
|
95
|
-
# of the framework.
|
96
|
-
#
|
97
|
-
# The new instance of the framework will be completely decoupled from the
|
98
|
-
# original. It will inherit the configuration, but all the changes that
|
99
|
-
# happen after the duplication, won't be reflected on the other copies.
|
100
|
-
#
|
101
|
-
# @return [Module] a copy of Hanami::Controller
|
102
|
-
#
|
103
|
-
# @since 0.2.0
|
104
|
-
# @api private
|
105
|
-
#
|
106
|
-
# @example Basic usage
|
107
|
-
# require 'hanami/controller'
|
108
|
-
#
|
109
|
-
# module MyApp
|
110
|
-
# Controller = Hanami::Controller.dupe
|
111
|
-
# end
|
112
|
-
#
|
113
|
-
# MyApp::Controller == Hanami::Controller # => false
|
114
|
-
#
|
115
|
-
# MyApp::Controller.configuration ==
|
116
|
-
# Hanami::Controller.configuration # => false
|
117
|
-
#
|
118
|
-
# @example Inheriting configuration
|
119
|
-
# require 'hanami/controller'
|
120
|
-
#
|
121
|
-
# Hanami::Controller.configure do
|
122
|
-
# handle_exceptions false
|
123
|
-
# end
|
124
|
-
#
|
125
|
-
# module MyApp
|
126
|
-
# Controller = Hanami::Controller.dupe
|
127
|
-
# end
|
128
|
-
#
|
129
|
-
# module MyApi
|
130
|
-
# Controller = Hanami::Controller.dupe
|
131
|
-
# Controller.configure do
|
132
|
-
# handle_exceptions true
|
133
|
-
# end
|
134
|
-
# end
|
135
|
-
#
|
136
|
-
# Hanami::Controller.configuration.handle_exceptions # => false
|
137
|
-
# MyApp::Controller.configuration.handle_exceptions # => false
|
138
|
-
# MyApi::Controller.configuration.handle_exceptions # => true
|
139
|
-
def self.dupe
|
140
|
-
dup.tap do |duplicated|
|
141
|
-
duplicated.configuration = configuration.duplicate
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
# Duplicate the framework and generate modules for the target application
|
146
|
-
#
|
147
|
-
# @param mod [Module] the Ruby namespace of the application
|
148
|
-
# @param controllers [String] the optional namespace where the application's
|
149
|
-
# controllers will live
|
150
|
-
# @param blk [Proc] an optional block to configure the framework
|
151
|
-
#
|
152
|
-
# @return [Module] a copy of Hanami::Controller
|
153
|
-
#
|
154
|
-
# @since 0.2.0
|
155
|
-
#
|
156
|
-
# @see Hanami::Controller#dupe
|
157
|
-
# @see Hanami::Controller::Configuration
|
158
|
-
# @see Hanami::Controller::Configuration#action_module
|
159
|
-
#
|
160
|
-
# @example Basic usage
|
161
|
-
# require 'hanami/controller'
|
162
|
-
#
|
163
|
-
# module MyApp
|
164
|
-
# Controller = Hanami::Controller.duplicate(self)
|
165
|
-
# end
|
166
|
-
#
|
167
|
-
# # It will:
|
168
|
-
# #
|
169
|
-
# # 1. Generate MyApp::Controller
|
170
|
-
# # 2. Generate MyApp::Action
|
171
|
-
# # 3. Generate MyApp::Controllers
|
172
|
-
# # 4. Configure MyApp::Action as the default module for actions
|
173
|
-
#
|
174
|
-
# module MyApp::Controllers::Dashboard
|
175
|
-
# include MyApp::Controller
|
176
|
-
#
|
177
|
-
# action 'Index' do # this will inject MyApp::Action
|
178
|
-
# def call(params)
|
179
|
-
# # ...
|
180
|
-
# end
|
181
|
-
# end
|
182
|
-
# end
|
183
|
-
#
|
184
|
-
# @example Compare code
|
185
|
-
# require 'hanami/controller'
|
186
|
-
#
|
187
|
-
# module MyApp
|
188
|
-
# Controller = Hanami::Controller.duplicate(self) do
|
189
|
-
# # ...
|
190
|
-
# end
|
191
|
-
# end
|
192
|
-
#
|
193
|
-
# # it's equivalent to:
|
194
|
-
#
|
195
|
-
# module MyApp
|
196
|
-
# Controller = Hanami::Controller.dupe
|
197
|
-
# Action = Hanami::Action.dup
|
198
|
-
#
|
199
|
-
# module Controllers
|
200
|
-
# end
|
201
|
-
#
|
202
|
-
# Controller.configure do
|
203
|
-
# action_module MyApp::Action
|
204
|
-
# end
|
205
|
-
#
|
206
|
-
# Controller.configure do
|
207
|
-
# # ...
|
208
|
-
# end
|
209
|
-
# end
|
210
|
-
#
|
211
|
-
# @example Custom controllers module
|
212
|
-
# require 'hanami/controller'
|
213
|
-
#
|
214
|
-
# module MyApp
|
215
|
-
# Controller = Hanami::Controller.duplicate(self, 'Ctrls')
|
216
|
-
# end
|
217
|
-
#
|
218
|
-
# defined?(MyApp::Controllers) # => nil
|
219
|
-
# defined?(MyApp::Ctrls) # => "constant"
|
220
|
-
#
|
221
|
-
# # Developers can namespace controllers under Ctrls
|
222
|
-
# module MyApp::Ctrls::Dashboard
|
223
|
-
# # ...
|
224
|
-
# end
|
225
|
-
#
|
226
|
-
# @example Nil controllers module
|
227
|
-
# require 'hanami/controller'
|
228
|
-
#
|
229
|
-
# module MyApp
|
230
|
-
# Controller = Hanami::Controller.duplicate(self, nil)
|
231
|
-
# end
|
232
|
-
#
|
233
|
-
# defined?(MyApp::Controllers) # => nil
|
234
|
-
#
|
235
|
-
# # Developers can namespace controllers under MyApp
|
236
|
-
# module MyApp::DashboardController
|
237
|
-
# # ...
|
238
|
-
# end
|
239
|
-
#
|
240
|
-
# @example Block usage
|
241
|
-
# require 'hanami/controller'
|
242
|
-
#
|
243
|
-
# module MyApp
|
244
|
-
# Controller = Hanami::Controller.duplicate(self) do
|
245
|
-
# handle_exceptions false
|
246
|
-
# end
|
247
|
-
# end
|
248
|
-
#
|
249
|
-
# Hanami::Controller.configuration.handle_exceptions # => true
|
250
|
-
# MyApp::Controller.configuration.handle_exceptions # => false
|
251
|
-
def self.duplicate(mod, controllers = 'Controllers', &blk)
|
252
|
-
dupe.tap do |duplicated|
|
253
|
-
mod.module_eval %{ module #{ controllers }; end } if controllers
|
254
|
-
mod.module_eval %{ Action = Hanami::Action.dup }
|
255
|
-
|
256
|
-
duplicated.module_eval %{
|
257
|
-
configure do
|
258
|
-
action_module #{ mod }::Action
|
259
|
-
end
|
260
|
-
}
|
261
|
-
|
262
|
-
duplicated.configure(&blk) if block_given?
|
263
|
-
end
|
264
|
-
end
|
265
|
-
|
266
|
-
# Framework loading entry point
|
267
|
-
#
|
268
|
-
# @return [void]
|
269
|
-
#
|
270
|
-
# @since 0.3.0
|
271
|
-
def self.load!
|
272
|
-
configuration.load!
|
273
|
-
end
|
274
64
|
end
|
275
65
|
end
|
@@ -44,126 +44,25 @@ module Hanami
|
|
44
44
|
'text/html' => :html
|
45
45
|
}.freeze
|
46
46
|
|
47
|
-
# Return a copy of the configuration of the framework instance associated
|
48
|
-
# with the given class.
|
49
|
-
#
|
50
|
-
# When multiple instances of Hanami::Controller are used in the same
|
51
|
-
# application, we want to make sure that a controller or an action will
|
52
|
-
# receive the expected configuration.
|
53
|
-
#
|
54
|
-
# @param base [Class, Module] a controller or an action
|
55
|
-
#
|
56
|
-
# @return [Hanami::Controller::Configuration] the configuration associated
|
57
|
-
# to the given class.
|
58
|
-
#
|
59
|
-
# @since 0.2.0
|
60
|
-
# @api private
|
61
|
-
#
|
62
|
-
# @example Direct usage of the framework
|
63
|
-
# require 'hanami/controller'
|
64
|
-
#
|
65
|
-
# class Show
|
66
|
-
# include Hanami::Action
|
67
|
-
# end
|
68
|
-
#
|
69
|
-
# Hanami::Controller::Configuration.for(Show)
|
70
|
-
# # => will duplicate from Hanami::Controller
|
71
|
-
#
|
72
|
-
# @example Multiple instances of the framework
|
73
|
-
# require 'hanami/controller'
|
74
|
-
#
|
75
|
-
# module MyApp
|
76
|
-
# Controller = Hanami::Controller.duplicate(self)
|
77
|
-
#
|
78
|
-
# module Controllers::Dashboard
|
79
|
-
# class Index
|
80
|
-
# include MyApp::Action
|
81
|
-
#
|
82
|
-
# def call(params)
|
83
|
-
# # ...
|
84
|
-
# end
|
85
|
-
# end
|
86
|
-
# end
|
87
|
-
# end
|
88
|
-
#
|
89
|
-
# class Show
|
90
|
-
# include Hanami::Action
|
91
|
-
# end
|
92
|
-
#
|
93
|
-
# Hanami::Controller::Configuration.for(Show)
|
94
|
-
# # => will duplicate from Hanami::Controller
|
95
|
-
#
|
96
|
-
# Hanami::Controller::Configuration.for(MyApp::Controllers::Dashboard)
|
97
|
-
# # => will duplicate from MyApp::Controller
|
98
|
-
def self.for(base)
|
99
|
-
namespace = Utils::String.namespace(base.name)
|
100
|
-
framework = Utils::Class.load("#{namespace}::Controller") || Utils::Class.load!('Hanami::Controller')
|
101
|
-
framework.configuration.duplicate
|
102
|
-
end
|
103
|
-
|
104
47
|
# Initialize a configuration instance
|
105
48
|
#
|
106
49
|
# @return [Hanami::Controller::Configuration] a new configuration's
|
107
50
|
# instance
|
108
51
|
#
|
109
52
|
# @since 0.2.0
|
110
|
-
def initialize
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
# Handle exceptions with an HTTP status or let them uncaught
|
125
|
-
#
|
126
|
-
# If this value is set to `true`, the configured exceptions will return
|
127
|
-
# the specified HTTP status, the rest of them with `500`.
|
128
|
-
#
|
129
|
-
# If this value is set to `false`, the exceptions won't be caught.
|
130
|
-
#
|
131
|
-
# This is part of a DSL, for this reason when this method is called with
|
132
|
-
# an argument, it will set the corresponding instance variable. When
|
133
|
-
# called without, it will return the already set value, or the default.
|
134
|
-
#
|
135
|
-
# @overload handle_exceptions(value)
|
136
|
-
# Sets the given value
|
137
|
-
# @param value [TrueClass, FalseClass] true or false, default to true
|
138
|
-
#
|
139
|
-
# @overload handle_exceptions
|
140
|
-
# Gets the value
|
141
|
-
# @return [TrueClass, FalseClass]
|
142
|
-
#
|
143
|
-
# @since 0.2.0
|
144
|
-
#
|
145
|
-
# @see Hanami::Controller::Configuration#handle_exception
|
146
|
-
# @see Hanami::Controller#configure
|
147
|
-
# @see Hanami::Action::Throwable
|
148
|
-
# @see http://httpstatus.es/500
|
149
|
-
#
|
150
|
-
# @example Getting the value
|
151
|
-
# require 'hanami/controller'
|
152
|
-
#
|
153
|
-
# Hanami::Controller.configuration.handle_exceptions # => true
|
154
|
-
#
|
155
|
-
# @example Setting the value
|
156
|
-
# require 'hanami/controller'
|
157
|
-
#
|
158
|
-
# Hanami::Controller.configure do
|
159
|
-
# handle_exceptions false
|
160
|
-
# end
|
161
|
-
def handle_exceptions(value = nil)
|
162
|
-
if value.nil?
|
163
|
-
@handle_exceptions
|
164
|
-
else
|
165
|
-
@handle_exceptions = value
|
166
|
-
end
|
53
|
+
def initialize(&blk)
|
54
|
+
@handled_exceptions = {}
|
55
|
+
@formats = DEFAULT_FORMATS.dup
|
56
|
+
@mime_types = nil
|
57
|
+
@default_request_format = nil
|
58
|
+
@default_response_format = nil
|
59
|
+
@default_charset = nil
|
60
|
+
@default_headers = {}
|
61
|
+
@cookies = {}
|
62
|
+
@root_directory = ::Pathname.new(Dir.pwd).realpath
|
63
|
+
@public_directory = root_directory.join(DEFAULT_PUBLIC_DIRECTORY).to_s
|
64
|
+
instance_eval(&blk) unless blk.nil?
|
65
|
+
freeze
|
167
66
|
end
|
168
67
|
|
169
68
|
# Specify how to handle an exception with an HTTP status
|
@@ -176,7 +75,6 @@ module Hanami
|
|
176
75
|
#
|
177
76
|
# @since 0.2.0
|
178
77
|
#
|
179
|
-
# @see Hanami::Controller::Configuration#handle_exceptions
|
180
78
|
# @see Hanami::Controller#configure
|
181
79
|
# @see Hanami::Action::Throwable
|
182
80
|
#
|
@@ -188,178 +86,6 @@ module Hanami
|
|
188
86
|
# end
|
189
87
|
def handle_exception(exception)
|
190
88
|
@handled_exceptions.merge!(exception)
|
191
|
-
_sort_handled_exceptions!
|
192
|
-
end
|
193
|
-
|
194
|
-
# Return a callable handler for the given exception
|
195
|
-
#
|
196
|
-
# @param exception [Exception] an exception
|
197
|
-
#
|
198
|
-
# @since 0.3.0
|
199
|
-
# @api private
|
200
|
-
#
|
201
|
-
# @see Hanami::Controller::Configuration#handle_exception
|
202
|
-
def exception_handler(exception)
|
203
|
-
exception_handler_for(exception) || DEFAULT_ERROR_CODE
|
204
|
-
end
|
205
|
-
|
206
|
-
# Check if the given exception is handled.
|
207
|
-
#
|
208
|
-
# @param exception [Exception] an exception
|
209
|
-
#
|
210
|
-
# @since 0.3.2
|
211
|
-
# @api private
|
212
|
-
#
|
213
|
-
# @see Hanami::Controller::Configuration#handle_exception
|
214
|
-
def handled_exception?(exception)
|
215
|
-
handled_exceptions &&
|
216
|
-
!exception_handler_for(exception).nil?
|
217
|
-
end
|
218
|
-
|
219
|
-
# Finds configured handler for given exception, or nil if not found.
|
220
|
-
#
|
221
|
-
# @param exception [Exception] an exception
|
222
|
-
#
|
223
|
-
# @since 1.0.0
|
224
|
-
# @api private
|
225
|
-
#
|
226
|
-
# @see Hanami::Controller::Configuration#handle_exception
|
227
|
-
def exception_handler_for(exception)
|
228
|
-
@handled_exceptions.each do |exception_class, handler|
|
229
|
-
return handler if exception.kind_of?(exception_class)
|
230
|
-
end
|
231
|
-
|
232
|
-
nil
|
233
|
-
end
|
234
|
-
|
235
|
-
# Specify which is the default action module to be included when we use
|
236
|
-
# the `Hanami::Controller.action` method.
|
237
|
-
#
|
238
|
-
# This setting is useful when we use multiple instances of the framework
|
239
|
-
# in the same process, so we want to ensure that the actions will include
|
240
|
-
# `MyApp::Action`, rather than `AnotherApp::Action`.
|
241
|
-
#
|
242
|
-
# If not set, the default value is `Hanami::Action`
|
243
|
-
#
|
244
|
-
# This is part of a DSL, for this reason when this method is called with
|
245
|
-
# an argument, it will set the corresponding instance variable. When
|
246
|
-
# called without, it will return the already set value, or the default.
|
247
|
-
#
|
248
|
-
# @overload action_module(value)
|
249
|
-
# Sets the given value
|
250
|
-
# @param value [Module] the module to be included in all the actions
|
251
|
-
#
|
252
|
-
# @overload action_module
|
253
|
-
# Gets the value
|
254
|
-
# @return [Module]
|
255
|
-
#
|
256
|
-
# @since 0.2.0
|
257
|
-
#
|
258
|
-
# @see Hanami::Controller::Dsl#action
|
259
|
-
# @see Hanami::Controller#duplicate
|
260
|
-
#
|
261
|
-
# @example Getting the value
|
262
|
-
# require 'hanami/controller'
|
263
|
-
#
|
264
|
-
# Hanami::Controller.configuration.action_module # => Hanami::Action
|
265
|
-
#
|
266
|
-
# @example Setting the value
|
267
|
-
# require 'hanami/controller'
|
268
|
-
#
|
269
|
-
# module MyAction
|
270
|
-
# end
|
271
|
-
#
|
272
|
-
# Hanami::Controller.configure do
|
273
|
-
# action_module MyAction
|
274
|
-
# end
|
275
|
-
#
|
276
|
-
# module Dashboard
|
277
|
-
# # It includes MyAction, instead of Hanami::Action
|
278
|
-
# class Index
|
279
|
-
# include MyAction
|
280
|
-
#
|
281
|
-
# def call(params)
|
282
|
-
# # ...
|
283
|
-
# end
|
284
|
-
# end
|
285
|
-
# end
|
286
|
-
#
|
287
|
-
# @example Duplicated framework
|
288
|
-
# require 'hanami/controller'
|
289
|
-
#
|
290
|
-
# module MyApp
|
291
|
-
# Controller = Hanami::Controller.duplicate(self)
|
292
|
-
#
|
293
|
-
# module Controllers::Dashboard
|
294
|
-
# include MyApp::Controller
|
295
|
-
#
|
296
|
-
# # It includes MyApp::Action, instead of Hanami::Action
|
297
|
-
# class Index
|
298
|
-
# include MyApp::Action
|
299
|
-
#
|
300
|
-
# def call(params)
|
301
|
-
# # ...
|
302
|
-
# end
|
303
|
-
# end
|
304
|
-
# end
|
305
|
-
# end
|
306
|
-
def action_module(value = nil)
|
307
|
-
if value.nil?
|
308
|
-
@action_module
|
309
|
-
else
|
310
|
-
@action_module = value
|
311
|
-
end
|
312
|
-
end
|
313
|
-
|
314
|
-
# Configure the logic to be executed when Hanami::Action is included
|
315
|
-
# This is useful to DRY code by having a single place where to configure
|
316
|
-
# shared behaviors like authentication, sessions, cookies etc.
|
317
|
-
#
|
318
|
-
# This method can be called multiple times.
|
319
|
-
#
|
320
|
-
# @param blk [Proc] the code block
|
321
|
-
#
|
322
|
-
# @return [void]
|
323
|
-
#
|
324
|
-
# @raise [ArgumentError] if called without passing a block
|
325
|
-
#
|
326
|
-
# @since 0.3.0
|
327
|
-
#
|
328
|
-
# @see Hanami::Controller.configure
|
329
|
-
# @see Hanami::Controller.duplicate
|
330
|
-
#
|
331
|
-
# @example Configure shared logic.
|
332
|
-
# require 'hanami/controller'
|
333
|
-
#
|
334
|
-
# Hanami::Controller.configure do
|
335
|
-
# prepare do
|
336
|
-
# include Hanami::Action::Session
|
337
|
-
# include MyAuthentication
|
338
|
-
# use SomeMiddleWare
|
339
|
-
#
|
340
|
-
# before { authenticate! }
|
341
|
-
# end
|
342
|
-
# end
|
343
|
-
#
|
344
|
-
# module Dashboard
|
345
|
-
# class Index
|
346
|
-
# # When Hanami::Action is included, it will:
|
347
|
-
# # * Include `Hanami::Action::Session` and `MyAuthentication`
|
348
|
-
# # * Configure to use `SomeMiddleWare`
|
349
|
-
# # * Configure a `before` callback that triggers `#authenticate!`
|
350
|
-
# include Hanami::Action
|
351
|
-
#
|
352
|
-
# def call(params)
|
353
|
-
# # ...
|
354
|
-
# end
|
355
|
-
# end
|
356
|
-
# end
|
357
|
-
def prepare(&blk)
|
358
|
-
if block_given?
|
359
|
-
@modules.push(blk)
|
360
|
-
else
|
361
|
-
raise ArgumentError.new('Please provide a block')
|
362
|
-
end
|
363
89
|
end
|
364
90
|
|
365
91
|
# Register a format
|
@@ -418,7 +144,6 @@ module Hanami
|
|
418
144
|
symbol, mime_type = *Utils::Kernel.Array(hash)
|
419
145
|
|
420
146
|
@formats[Utils::Kernel.String(mime_type)] = Utils::Kernel.Symbol(symbol)
|
421
|
-
@mime_types = nil
|
422
147
|
end
|
423
148
|
|
424
149
|
# Return the configured format's MIME types
|
@@ -427,24 +152,10 @@ module Hanami
|
|
427
152
|
# @api private
|
428
153
|
#
|
429
154
|
# @see Hanami::Controller::Configuration#format
|
430
|
-
# @see Hanami::Action::Mime::MIME_TYPES
|
431
155
|
def mime_types
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
end
|
436
|
-
end
|
437
|
-
|
438
|
-
# Restrict the MIME types set only to the given set
|
439
|
-
#
|
440
|
-
# @param mime_types [Array] the set of MIME types
|
441
|
-
#
|
442
|
-
# @since 1.0.0
|
443
|
-
# @api private
|
444
|
-
#
|
445
|
-
# @see Hanami::Action::Mime::ClassMethods#accept
|
446
|
-
def restrict_mime_types!(mime_types)
|
447
|
-
@mime_types = self.mime_types & mime_types
|
156
|
+
# FIXME: this isn't efficient. speed it up!
|
157
|
+
((@formats.keys - DEFAULT_FORMATS.keys) +
|
158
|
+
Hanami::Action::Mime::TYPES.values).freeze
|
448
159
|
end
|
449
160
|
|
450
161
|
# Set a format as default fallback for all the requests without a strict
|
@@ -456,42 +167,17 @@ module Hanami
|
|
456
167
|
#
|
457
168
|
# By default this value is nil.
|
458
169
|
#
|
459
|
-
# This is part of a DSL, for this reason when this method is called with
|
460
|
-
# an argument, it will set the corresponding instance variable. When
|
461
|
-
# called without, it will return the already set value, or the default.
|
462
|
-
#
|
463
|
-
# @overload default_request_format(format)
|
464
|
-
# Sets the given value
|
465
|
-
# @param format [#to_sym] the symbol format
|
466
|
-
# @raise [TypeError] if it cannot be coerced to a symbol
|
467
|
-
#
|
468
|
-
# @overload default_request_format
|
469
|
-
# Gets the value
|
470
|
-
# @return [Symbol,nil]
|
471
|
-
#
|
472
170
|
# @since 0.5.0
|
473
171
|
#
|
474
172
|
# @see Hanami::Action::Mime
|
475
173
|
#
|
476
|
-
#
|
477
|
-
|
478
|
-
|
479
|
-
# Hanami::Controller.configuration.default_request_format # => nil
|
480
|
-
#
|
481
|
-
# @example Setting the value
|
482
|
-
# require 'hanami/controller'
|
483
|
-
#
|
484
|
-
# Hanami::Controller.configure do
|
485
|
-
# default_request_format :html
|
486
|
-
# end
|
487
|
-
def default_request_format(format = nil)
|
488
|
-
if format
|
489
|
-
@default_request_format = Utils::Kernel.Symbol(format)
|
490
|
-
else
|
491
|
-
@default_request_format
|
492
|
-
end
|
174
|
+
# FIXME: new API docs
|
175
|
+
def default_request_format=(value)
|
176
|
+
@default_request_format = Utils::Kernel.Symbol(value) unless value.nil?
|
493
177
|
end
|
494
178
|
|
179
|
+
attr_reader :default_request_format
|
180
|
+
|
495
181
|
# Set a format to be used for all responses regardless of the request type.
|
496
182
|
#
|
497
183
|
# The given format must be coercible to a symbol, and be a valid mime type
|
@@ -500,42 +186,17 @@ module Hanami
|
|
500
186
|
#
|
501
187
|
# By default this value is nil.
|
502
188
|
#
|
503
|
-
# This is part of a DSL, for this reason when this method is called with
|
504
|
-
# an argument, it will set the corresponding instance variable. When
|
505
|
-
# called without, it will return the already set value, or the default.
|
506
|
-
#
|
507
|
-
# @overload default_response_format(format)
|
508
|
-
# Sets the given value
|
509
|
-
# @param format [#to_sym] the symbol format
|
510
|
-
# @raise [TypeError] if it cannot be coerced to a symbol
|
511
|
-
#
|
512
|
-
# @overload default_response_format
|
513
|
-
# Gets the value
|
514
|
-
# @return [Symbol,nil]
|
515
|
-
#
|
516
189
|
# @since 0.5.0
|
517
190
|
#
|
518
191
|
# @see Hanami::Action::Mime
|
519
192
|
#
|
520
|
-
#
|
521
|
-
|
522
|
-
|
523
|
-
# Hanami::Controller.configuration.default_response_format # => nil
|
524
|
-
#
|
525
|
-
# @example Setting the value
|
526
|
-
# require 'hanami/controller'
|
527
|
-
#
|
528
|
-
# Hanami::Controller.configure do
|
529
|
-
# default_response_format :json
|
530
|
-
# end
|
531
|
-
def default_response_format(format = nil)
|
532
|
-
if format
|
533
|
-
@default_response_format = Utils::Kernel.Symbol(format)
|
534
|
-
else
|
535
|
-
@default_response_format
|
536
|
-
end
|
193
|
+
# FIXME: new API docs
|
194
|
+
def default_response_format=(value)
|
195
|
+
@default_response_format = Utils::Kernel.Symbol(value) unless value.nil?
|
537
196
|
end
|
538
197
|
|
198
|
+
attr_reader :default_response_format
|
199
|
+
|
539
200
|
# Set a charset as default fallback for all the requests without a strict
|
540
201
|
# requirement for the charset.
|
541
202
|
#
|
@@ -545,24 +206,10 @@ module Hanami
|
|
545
206
|
#
|
546
207
|
# @see Hanami::Action::Mime
|
547
208
|
#
|
548
|
-
#
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
#
|
553
|
-
# @example Setting the value
|
554
|
-
# require 'hanami/controller'
|
555
|
-
#
|
556
|
-
# Hanami::Controller.configure do
|
557
|
-
# default_charset 'koi8-r'
|
558
|
-
# end
|
559
|
-
def default_charset(charset = nil)
|
560
|
-
if charset
|
561
|
-
@default_charset = charset
|
562
|
-
else
|
563
|
-
@default_charset
|
564
|
-
end
|
565
|
-
end
|
209
|
+
# FIXME: new API docs
|
210
|
+
attr_accessor :default_charset
|
211
|
+
|
212
|
+
attr_reader :default_headers
|
566
213
|
|
567
214
|
# Set default headers for all responses
|
568
215
|
#
|
@@ -578,21 +225,19 @@ module Hanami
|
|
578
225
|
# @example Setting the value
|
579
226
|
# require 'hanami/controller'
|
580
227
|
#
|
581
|
-
# Hanami::Controller.
|
582
|
-
# default_headers
|
228
|
+
# Hanami::Controller::Configuration.new do |config|
|
229
|
+
# config.default_headers = {
|
583
230
|
# 'X-Frame-Options' => 'DENY'
|
584
|
-
# }
|
231
|
+
# }
|
585
232
|
# end
|
586
|
-
def default_headers(headers
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
)
|
591
|
-
else
|
592
|
-
@default_headers
|
593
|
-
end
|
233
|
+
def default_headers=(headers)
|
234
|
+
@default_headers.merge!(
|
235
|
+
headers.reject { |_, v| v.nil? }
|
236
|
+
)
|
594
237
|
end
|
595
238
|
|
239
|
+
attr_reader :cookies
|
240
|
+
|
596
241
|
# Set default cookies options for all responses
|
597
242
|
#
|
598
243
|
# By default this value is an empty hash.
|
@@ -607,22 +252,18 @@ module Hanami
|
|
607
252
|
# @example Setting the value
|
608
253
|
# require 'hanami/controller'
|
609
254
|
#
|
610
|
-
# Hanami::Controller.
|
611
|
-
# cookies
|
255
|
+
# Hanami::Controller::Configuration.new do |config|
|
256
|
+
# config.cookies = {
|
612
257
|
# domain: 'hanamirb.org',
|
613
258
|
# path: '/controller',
|
614
259
|
# secure: true,
|
615
260
|
# httponly: true
|
616
|
-
# }
|
261
|
+
# }
|
617
262
|
# end
|
618
|
-
def cookies(options
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
)
|
623
|
-
else
|
624
|
-
@cookies
|
625
|
-
end
|
263
|
+
def cookies=(options)
|
264
|
+
@cookies.merge!(
|
265
|
+
options.reject { |_, v| v.nil? }
|
266
|
+
)
|
626
267
|
end
|
627
268
|
|
628
269
|
# Returns a format for the given mime type
|
@@ -655,109 +296,13 @@ module Hanami
|
|
655
296
|
# @since 1.0.0
|
656
297
|
attr_reader :root_directory
|
657
298
|
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
else
|
662
|
-
@public_directory = root_directory.join(value).to_s
|
663
|
-
end
|
664
|
-
end
|
665
|
-
|
666
|
-
# Duplicate by copying the settings in a new instance.
|
667
|
-
#
|
668
|
-
# @return [Hanami::Controller::Configuration] a copy of the configuration
|
669
|
-
#
|
670
|
-
# @since 0.2.0
|
671
|
-
# @api private
|
672
|
-
def duplicate
|
673
|
-
Configuration.new.tap do |c|
|
674
|
-
c.handle_exceptions = handle_exceptions
|
675
|
-
c.handled_exceptions = handled_exceptions.dup
|
676
|
-
c.action_module = action_module
|
677
|
-
c.modules = modules.dup
|
678
|
-
c.formats = formats.dup
|
679
|
-
c.default_request_format = default_request_format
|
680
|
-
c.default_response_format = default_response_format
|
681
|
-
c.default_charset = default_charset
|
682
|
-
c.default_headers = default_headers.dup
|
683
|
-
c.public_directory = public_directory
|
684
|
-
c.cookies = cookies.dup
|
685
|
-
end
|
686
|
-
end
|
687
|
-
|
688
|
-
# Return included modules
|
689
|
-
#
|
690
|
-
# @return [Array<Proc>] array of included blocks
|
691
|
-
#
|
692
|
-
# @since 0.2.0
|
693
|
-
# @api private
|
694
|
-
#
|
695
|
-
# @see Hanami::Controller::Configuration#prepare
|
696
|
-
attr_reader :modules
|
697
|
-
|
698
|
-
# Reset all the values to the defaults
|
699
|
-
#
|
700
|
-
# @since 0.2.0
|
701
|
-
# @api private
|
702
|
-
def reset!
|
703
|
-
@handle_exceptions = true
|
704
|
-
@handled_exceptions = {}
|
705
|
-
@modules = []
|
706
|
-
@formats = DEFAULT_FORMATS.dup
|
707
|
-
@mime_types = nil
|
708
|
-
@default_request_format = nil
|
709
|
-
@default_response_format = nil
|
710
|
-
@default_charset = nil
|
711
|
-
@default_headers = {}
|
712
|
-
@cookies = {}
|
713
|
-
@root_directory = ::Pathname.new(Dir.pwd).realpath
|
714
|
-
@public_directory = root_directory.join(DEFAULT_PUBLIC_DIRECTORY).to_s
|
715
|
-
@action_module = ::Hanami::Action
|
716
|
-
end
|
717
|
-
|
718
|
-
# Copy the configuration for the given action
|
719
|
-
#
|
720
|
-
# @param base [Class] the target action
|
721
|
-
#
|
722
|
-
# @return void
|
723
|
-
#
|
724
|
-
# @since 0.3.0
|
725
|
-
# @api private
|
726
|
-
#
|
727
|
-
# @see Hanami::Controller::Configurable.included
|
728
|
-
def copy!(base)
|
729
|
-
modules.each do |mod|
|
730
|
-
base.class_eval(&mod)
|
731
|
-
end
|
732
|
-
end
|
733
|
-
|
734
|
-
# Load the framework
|
735
|
-
#
|
736
|
-
# @since 0.3.0
|
737
|
-
# @api private
|
738
|
-
def load!
|
739
|
-
freeze
|
740
|
-
end
|
741
|
-
|
742
|
-
protected
|
743
|
-
# @since 0.5.0
|
744
|
-
# @api private
|
745
|
-
def _sort_handled_exceptions!
|
746
|
-
@handled_exceptions = Hash[
|
747
|
-
@handled_exceptions.sort{|(ex1,_),(ex2,_)| ex1.ancestors.include?(ex2) ? -1 : 1 }
|
748
|
-
]
|
299
|
+
# FIXME: API docs
|
300
|
+
def public_directory=(value)
|
301
|
+
@public_directory = root_directory.join(value).to_s
|
749
302
|
end
|
750
303
|
|
751
|
-
|
752
|
-
|
753
|
-
attr_writer :action_module
|
754
|
-
attr_writer :modules
|
755
|
-
attr_writer :default_request_format
|
756
|
-
attr_writer :default_response_format
|
757
|
-
attr_writer :default_charset
|
758
|
-
attr_writer :default_headers
|
759
|
-
attr_writer :cookies
|
760
|
-
attr_writer :public_directory
|
304
|
+
attr_reader :public_directory
|
305
|
+
attr_reader :handled_exceptions
|
761
306
|
end
|
762
307
|
end
|
763
308
|
end
|