hanami 0.0.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (168) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +214 -0
  3. data/FEATURES.md +156 -0
  4. data/LICENSE.md +22 -0
  5. data/README.md +80 -15
  6. data/bin/hanami +5 -0
  7. data/hanami.gemspec +27 -12
  8. data/lib/hanami.rb +78 -2
  9. data/lib/hanami/action/csrf_protection.rb +167 -0
  10. data/lib/hanami/action/routing_helpers.rb +40 -0
  11. data/lib/hanami/application.rb +244 -0
  12. data/lib/hanami/application_name.rb +101 -0
  13. data/lib/hanami/cli.rb +119 -0
  14. data/lib/hanami/cli_sub_commands/assets.rb +29 -0
  15. data/lib/hanami/cli_sub_commands/db.rb +124 -0
  16. data/lib/hanami/cli_sub_commands/destroy.rb +102 -0
  17. data/lib/hanami/cli_sub_commands/generate.rb +127 -0
  18. data/lib/hanami/commands/assets/precompile.rb +35 -0
  19. data/lib/hanami/commands/console.rb +90 -0
  20. data/lib/hanami/commands/db/abstract.rb +19 -0
  21. data/lib/hanami/commands/db/apply.rb +14 -0
  22. data/lib/hanami/commands/db/console.rb +50 -0
  23. data/lib/hanami/commands/db/create.rb +14 -0
  24. data/lib/hanami/commands/db/drop.rb +14 -0
  25. data/lib/hanami/commands/db/migrate.rb +19 -0
  26. data/lib/hanami/commands/db/prepare.rb +14 -0
  27. data/lib/hanami/commands/db/version.rb +14 -0
  28. data/lib/hanami/commands/generate/abstract.rb +63 -0
  29. data/lib/hanami/commands/generate/action.rb +262 -0
  30. data/lib/hanami/commands/generate/app.rb +116 -0
  31. data/lib/hanami/commands/generate/mailer.rb +118 -0
  32. data/lib/hanami/commands/generate/migration.rb +63 -0
  33. data/lib/hanami/commands/generate/model.rb +96 -0
  34. data/lib/hanami/commands/new/abstract.rb +128 -0
  35. data/lib/hanami/commands/new/app.rb +116 -0
  36. data/lib/hanami/commands/new/container.rb +102 -0
  37. data/lib/hanami/commands/routes.rb +41 -0
  38. data/lib/hanami/commands/server.rb +79 -0
  39. data/lib/hanami/config/configure.rb +17 -0
  40. data/lib/hanami/config/cookies.rb +68 -0
  41. data/lib/hanami/config/framework_configuration.rb +42 -0
  42. data/lib/hanami/config/load_paths.rb +27 -0
  43. data/lib/hanami/config/mapper.rb +36 -0
  44. data/lib/hanami/config/mapping.rb +12 -0
  45. data/lib/hanami/config/routes.rb +16 -0
  46. data/lib/hanami/config/security.rb +58 -0
  47. data/lib/hanami/config/sessions.rb +97 -0
  48. data/lib/hanami/configuration.rb +1728 -0
  49. data/lib/hanami/container.rb +59 -0
  50. data/lib/hanami/environment.rb +485 -0
  51. data/lib/hanami/frameworks.rb +14 -0
  52. data/lib/hanami/generators/action/action.rb.tt +8 -0
  53. data/lib/hanami/generators/action/action_spec.minitest.tt +12 -0
  54. data/lib/hanami/generators/action/action_spec.rspec.tt +11 -0
  55. data/lib/hanami/generators/action/action_without_view.rb.tt +9 -0
  56. data/lib/hanami/generators/action/template.tt +0 -0
  57. data/lib/hanami/generators/action/view.rb.tt +5 -0
  58. data/lib/hanami/generators/action/view_spec.minitest.tt +13 -0
  59. data/lib/hanami/generators/action/view_spec.rspec.tt +12 -0
  60. data/lib/hanami/generators/app/.gitkeep.tt +1 -0
  61. data/lib/hanami/generators/app/application.rb.tt +273 -0
  62. data/lib/hanami/generators/app/config/initializers/.gitkeep +0 -0
  63. data/lib/hanami/generators/app/config/routes.rb.tt +2 -0
  64. data/lib/hanami/generators/app/favicon.ico +0 -0
  65. data/lib/hanami/generators/app/templates/application.html.erb.tt +10 -0
  66. data/lib/hanami/generators/app/views/application_layout.rb.tt +7 -0
  67. data/lib/hanami/generators/application/app/.env.development.tt +4 -0
  68. data/lib/hanami/generators/application/app/.env.test.tt +4 -0
  69. data/lib/hanami/generators/application/app/.env.tt +1 -0
  70. data/lib/hanami/generators/application/app/.gitignore +0 -0
  71. data/lib/hanami/generators/application/app/.gitkeep +1 -0
  72. data/lib/hanami/generators/application/app/Gemfile.tt +37 -0
  73. data/lib/hanami/generators/application/app/Rakefile.minitest.tt +11 -0
  74. data/lib/hanami/generators/application/app/Rakefile.rspec.tt +6 -0
  75. data/lib/hanami/generators/application/app/apps/.gitkeep.tt +1 -0
  76. data/lib/hanami/generators/application/app/capybara.rb.rspec.tt +8 -0
  77. data/lib/hanami/generators/application/app/config.ru.tt +3 -0
  78. data/lib/hanami/generators/application/app/config/application.rb.tt +270 -0
  79. data/lib/hanami/generators/application/app/config/environment.rb.tt +5 -0
  80. data/lib/hanami/generators/application/app/config/initializers/.gitkeep +0 -0
  81. data/lib/hanami/generators/application/app/config/routes.rb.tt +2 -0
  82. data/lib/hanami/generators/application/app/db/.gitkeep +1 -0
  83. data/lib/hanami/generators/application/app/favicon.ico +0 -0
  84. data/lib/hanami/generators/application/app/features_helper.rb.minitest.tt +11 -0
  85. data/lib/hanami/generators/application/app/features_helper.rb.rspec.tt +12 -0
  86. data/lib/hanami/generators/application/app/gitignore.tt +2 -0
  87. data/lib/hanami/generators/application/app/gitignore_with_db.tt +4 -0
  88. data/lib/hanami/generators/application/app/hanamirc.tt +3 -0
  89. data/lib/hanami/generators/application/app/lib/app_name.rb.tt +59 -0
  90. data/lib/hanami/generators/application/app/lib/chirp/entities/.gitkeep +1 -0
  91. data/lib/hanami/generators/application/app/lib/chirp/repositories/.gitkeep +1 -0
  92. data/lib/hanami/generators/application/app/lib/config/mapping.rb.tt +7 -0
  93. data/lib/hanami/generators/application/app/rspec.rspec.tt +2 -0
  94. data/lib/hanami/generators/application/app/schema.sql.tt +0 -0
  95. data/lib/hanami/generators/application/app/spec_helper.rb.minitest.tt +7 -0
  96. data/lib/hanami/generators/application/app/spec_helper.rb.rspec.tt +104 -0
  97. data/lib/hanami/generators/application/app/templates/application.html.erb.tt +10 -0
  98. data/lib/hanami/generators/application/app/views/application_layout.rb.tt +7 -0
  99. data/lib/hanami/generators/application/container/.env.development.tt +3 -0
  100. data/lib/hanami/generators/application/container/.env.test.tt +3 -0
  101. data/lib/hanami/generators/application/container/.env.tt +1 -0
  102. data/lib/hanami/generators/application/container/.gitignore +0 -0
  103. data/lib/hanami/generators/application/container/.gitkeep +1 -0
  104. data/lib/hanami/generators/application/container/Gemfile.tt +36 -0
  105. data/lib/hanami/generators/application/container/Rakefile.minitest.tt +11 -0
  106. data/lib/hanami/generators/application/container/Rakefile.rspec.tt +6 -0
  107. data/lib/hanami/generators/application/container/capybara.rb.rspec.tt +8 -0
  108. data/lib/hanami/generators/application/container/config.ru.tt +3 -0
  109. data/lib/hanami/generators/application/container/config/environment.rb.tt +7 -0
  110. data/lib/hanami/generators/application/container/config/initializers/.gitkeep +0 -0
  111. data/lib/hanami/generators/application/container/db/.gitkeep +1 -0
  112. data/lib/hanami/generators/application/container/features_helper.rb.minitest.tt +11 -0
  113. data/lib/hanami/generators/application/container/features_helper.rb.rspec.tt +12 -0
  114. data/lib/hanami/generators/application/container/gitignore.tt +2 -0
  115. data/lib/hanami/generators/application/container/gitignore_with_db.tt +4 -0
  116. data/lib/hanami/generators/application/container/hanamirc.tt +3 -0
  117. data/lib/hanami/generators/application/container/lib/app_name.rb.tt +60 -0
  118. data/lib/hanami/generators/application/container/lib/chirp/entities/.gitkeep +1 -0
  119. data/lib/hanami/generators/application/container/lib/chirp/mailers/.gitkeep +0 -0
  120. data/lib/hanami/generators/application/container/lib/chirp/mailers/templates/.gitkeep +0 -0
  121. data/lib/hanami/generators/application/container/lib/chirp/repositories/.gitkeep +1 -0
  122. data/lib/hanami/generators/application/container/lib/config/mapping.rb.tt +7 -0
  123. data/lib/hanami/generators/application/container/rspec.rspec.tt +2 -0
  124. data/lib/hanami/generators/application/container/schema.sql.tt +0 -0
  125. data/lib/hanami/generators/application/container/spec_helper.rb.minitest.tt +7 -0
  126. data/lib/hanami/generators/application/container/spec_helper.rb.rspec.tt +104 -0
  127. data/lib/hanami/generators/database_config.rb +99 -0
  128. data/lib/hanami/generators/generatable.rb +51 -0
  129. data/lib/hanami/generators/generator.rb +35 -0
  130. data/lib/hanami/generators/mailer/mailer.rb.tt +7 -0
  131. data/lib/hanami/generators/mailer/mailer_spec.rb.tt +7 -0
  132. data/lib/hanami/generators/mailer/template.html.tt +0 -0
  133. data/lib/hanami/generators/mailer/template.txt.tt +0 -0
  134. data/lib/hanami/generators/migration/migration.rb.tt +4 -0
  135. data/lib/hanami/generators/model/entity.rb.tt +3 -0
  136. data/lib/hanami/generators/model/entity_spec.minitest.tt +5 -0
  137. data/lib/hanami/generators/model/entity_spec.rspec.tt +3 -0
  138. data/lib/hanami/generators/model/repository.rb.tt +3 -0
  139. data/lib/hanami/generators/model/repository_spec.minitest.tt +5 -0
  140. data/lib/hanami/generators/model/repository_spec.rspec.tt +3 -0
  141. data/lib/hanami/generators/test_framework.rb +42 -0
  142. data/lib/hanami/hanamirc.rb +152 -0
  143. data/lib/hanami/loader.rb +258 -0
  144. data/lib/hanami/mailer/glue.rb +68 -0
  145. data/lib/hanami/middleware.rb +143 -0
  146. data/lib/hanami/rake_helper.rb +68 -0
  147. data/lib/hanami/rake_tasks.rb +2 -0
  148. data/lib/hanami/rendering_policy.rb +77 -0
  149. data/lib/hanami/repositories/car_repository.rb +3 -0
  150. data/lib/hanami/repositories/name_repository.rb +3 -0
  151. data/lib/hanami/root.rb +7 -0
  152. data/lib/hanami/routes.rb +151 -0
  153. data/lib/hanami/routing/default.rb +25 -0
  154. data/lib/hanami/setup.rb +3 -0
  155. data/lib/hanami/static.rb +77 -0
  156. data/lib/hanami/templates/default.html.erb +9 -0
  157. data/lib/hanami/templates/welcome.html.erb +52 -0
  158. data/lib/hanami/version.rb +4 -1
  159. data/lib/hanami/views/default.rb +34 -0
  160. data/lib/hanami/views/default_template_finder.rb +20 -0
  161. data/lib/hanami/views/null_view.rb +17 -0
  162. data/lib/hanami/welcome.rb +40 -0
  163. metadata +357 -16
  164. data/.gitignore +0 -9
  165. data/Gemfile +0 -4
  166. data/Rakefile +0 -2
  167. data/bin/console +0 -14
  168. data/bin/setup +0 -8
@@ -0,0 +1,1728 @@
1
+ require 'hanami/utils/kernel'
2
+ require 'hanami/utils/deprecation'
3
+ require 'hanami/environment'
4
+ require 'hanami/config/framework_configuration'
5
+ require 'hanami/config/load_paths'
6
+ require 'hanami/config/routes'
7
+ require 'hanami/config/mapping'
8
+ require 'hanami/config/sessions'
9
+ require 'hanami/config/configure'
10
+ require 'hanami/config/security'
11
+ require 'hanami/config/cookies'
12
+
13
+ module Hanami
14
+ # Configuration for a Hanami application
15
+ #
16
+ # @since 0.1.0
17
+ class Configuration
18
+ # @since 0.2.0
19
+ # @api private
20
+ #
21
+ # @see Hanami::Configuration#ssl?
22
+ SSL_SCHEME = 'https'.freeze
23
+
24
+ # Initialize a new configuration instance
25
+ #
26
+ # @return [Hanami::Configuration]
27
+ #
28
+ # @since 0.1.0
29
+ # @api private
30
+ def initialize
31
+ @blk = Proc.new{}
32
+ @env = Environment.new
33
+ @configurations = Hash.new { |k, v| k[v] = [] }
34
+ end
35
+
36
+ # Set a block yield when the configuration will be loaded or
37
+ # set a path for the specific environment.
38
+ #
39
+ # @param environment [Symbol,nil] the configuration environment name
40
+ # @param blk [Proc] the configuration block
41
+ #
42
+ # @return [self]
43
+ #
44
+ # @since 0.1.0
45
+ # @api private
46
+ def configure(environment = nil, path = nil, &blk)
47
+ if environment && path
48
+ @configurations[environment.to_s] << Config::Configure.new(root, path, &blk)
49
+ elsif environment
50
+ @configurations[environment.to_s] << blk
51
+ else
52
+ @blk = blk
53
+ end
54
+
55
+ self
56
+ end
57
+
58
+ # Load the configuration
59
+ #
60
+ # @param namespace [String,nil] the application namespace
61
+ #
62
+ # @return [self]
63
+ #
64
+ # @since 0.1.0
65
+ # @api private
66
+ def load!(namespace = nil)
67
+ @namespace = namespace
68
+ evaluate_configurations!
69
+
70
+ self
71
+ end
72
+
73
+ # Returns the security policy
74
+ #
75
+ # @return [Hanami::Config::Security]
76
+ #
77
+ # @since 0.3.0
78
+ #
79
+ # @see Hanami::Config::Security
80
+ #
81
+ # @example Getting values
82
+ # require 'hanami'
83
+ #
84
+ # module Bookshelf
85
+ # class Application < Hanami::Application
86
+ # configure do
87
+ # security.x_frame_options "ALLOW ALL"
88
+ # security.content_security_policy "script-src 'self' https://apis.example.com"
89
+ # end
90
+ # end
91
+ # end
92
+ #
93
+ # Bookshelf::Application.configuration.security.x_frame_options # => "ALLOW ALL"
94
+ # Bookshelf::Application.configuration.security.content_security_policy # => "script-src 'self' https://apis.example.com"
95
+ #
96
+ # @example Setting values
97
+ # require 'hanami'
98
+ #
99
+ # module Bookshelf
100
+ # class Application < Hanami::Application
101
+ # configure do
102
+ # security.x_frame_options "ALLOW ALL"
103
+ # security.content_security_policy "script-src 'self' https://apis.example.com"
104
+ # end
105
+ # end
106
+ # end
107
+ def security
108
+ @security ||= Config::Security.new
109
+ end
110
+
111
+ # Force ssl redirection if http scheme is set
112
+ #
113
+ # @return [Boolean]
114
+ #
115
+ # @since 0.4.0
116
+ #
117
+ # @see Hanami::Routing::ForceSsl
118
+ def force_ssl(value = nil)
119
+ if value
120
+ @force_ssl = value
121
+ else
122
+ @force_ssl || false
123
+ end
124
+ end
125
+
126
+ # The root of the application
127
+ #
128
+ # By default it returns the current directory, for this reason, **all the
129
+ # commands must be executed from the top level directory of the project**.
130
+ #
131
+ # If for some reason, that constraint above cannot be satisfied, please
132
+ # configure the root directory, so that commands can be executed from
133
+ # everywhere.
134
+ #
135
+ # This is part of a DSL, for this reason when this method is called with
136
+ # an argument, it will set the corresponding instance variable. When
137
+ # called without, it will return the already set value, or the default.
138
+ #
139
+ # @overload root(value)
140
+ # Sets the given value
141
+ # @param value [String,Pathname,#to_pathname] The root directory of the app
142
+ #
143
+ # @overload root
144
+ # Gets the value
145
+ # @return [Pathname]
146
+ # @raise [Errno::ENOENT] if the path cannot be found
147
+ #
148
+ # @since 0.1.0
149
+ #
150
+ # @see http://www.ruby-doc.org/core-2.1.2/Dir.html#method-c-pwd
151
+ #
152
+ # @example Getting the value
153
+ # require 'hanami'
154
+ #
155
+ # module Bookshelf
156
+ # class Application < Hanami::Application
157
+ # end
158
+ # end
159
+ #
160
+ # Bookshelf::Application.configuration.root # => #<Pathname:/path/to/root>
161
+ #
162
+ # @example Setting the value
163
+ # require 'hanami'
164
+ #
165
+ # module Bookshelf
166
+ # class Application < Hanami::Application
167
+ # configure do
168
+ # root '/path/to/another/root'
169
+ # end
170
+ # end
171
+ # end
172
+ def root(value = nil)
173
+ if value
174
+ @root = value
175
+ else
176
+ Utils::Kernel.Pathname(@root || Dir.pwd).realpath
177
+ end
178
+ end
179
+
180
+ # The application namespace
181
+ #
182
+ # By default it returns the Ruby namespace of the application. For instance
183
+ # for an application `Bookshelf::Application`, it returns `Bookshelf`.
184
+ #
185
+ # This value isn't set at the init time, but when the configuration is
186
+ # loaded with `#load!`.
187
+ #
188
+ # Hanami applications are namespaced: all the controllers and views live
189
+ # under the application module, without polluting the global namespace.
190
+ # However, if for some reason, you want top level classes, set this value
191
+ # to `Object` (which is the top level namespace for Ruby).
192
+ #
193
+ # This is part of a DSL, for this reason when this method is called with
194
+ # an argument, it will set the corresponding instance variable. When
195
+ # called without, it will return the already set value, or the default.
196
+ #
197
+ # @overload namespace(value)
198
+ # Sets the given value
199
+ # @param value [Class,Module] A valid Ruby namespace
200
+ #
201
+ # @overload namespace
202
+ # Gets the value
203
+ # @return [Class,Module] a Ruby namespace
204
+ #
205
+ # @since 0.1.0
206
+ #
207
+ # @example Getting the value
208
+ # require 'hanami'
209
+ #
210
+ # module Bookshelf
211
+ # class Application < Hanami::Application
212
+ # end
213
+ # end
214
+ #
215
+ # Bookshelf::Application.configuration.namespace # => Bookshelf
216
+ #
217
+ # # It will lookup namespaced controllers under Bookshelf
218
+ # # eg. Bookshelf::Controllers::Dashboard
219
+ #
220
+ # @example Setting the value
221
+ # require 'hanami'
222
+ #
223
+ # module Bookshelf
224
+ # class Application < Hanami::Application
225
+ # configure do
226
+ # namespace Object
227
+ # end
228
+ # end
229
+ # end
230
+ #
231
+ # Bookshelf::Application.configuration.namespace # => Object
232
+ #
233
+ # # It will lookup top level controllers under Object
234
+ # # eg. DashboardController
235
+ def namespace(value = nil)
236
+ if value
237
+ @namespace = value
238
+ else
239
+ @namespace
240
+ end
241
+ end
242
+
243
+ # A Hanami::Layout for this application
244
+ #
245
+ # By default it's `nil`.
246
+ #
247
+ # It accepts a Symbol as layout name. When the application is loaded, it
248
+ # will lookup for the corresponding class.
249
+ #
250
+ # All the views will use this layout, unless otherwise specified.
251
+ #
252
+ # This is part of a DSL, for this reason when this method is called with
253
+ # an argument, it will set the corresponding instance variable. When
254
+ # called without, it will return the already set value, or the default.
255
+ #
256
+ # @overload layout(value)
257
+ # Sets the given value
258
+ # @param value [Symbol] the layout name
259
+ #
260
+ # @overload layout
261
+ # Gets the value
262
+ # @return [Symbol,nil] the layout name
263
+ #
264
+ # @since 0.1.0
265
+ #
266
+ # @see http://rdoc.info/gems/hanami-view/Hanami/Layout
267
+ # @see http://rdoc.info/gems/hanami-view/Hanami/View/Configuration:layout
268
+ #
269
+ # @example Getting the value
270
+ # require 'hanami'
271
+ #
272
+ # module Bookshelf
273
+ # class Application < Hanami::Application
274
+ # end
275
+ # end
276
+ #
277
+ # Bookshelf::Application.configuration.layout # => nil
278
+ #
279
+ # # All the views will render without a layout
280
+ #
281
+ # @example Setting the value
282
+ # require 'hanami'
283
+ #
284
+ # module Bookshelf
285
+ # class Application < Hanami::Application
286
+ # configure do
287
+ # layout :application
288
+ # end
289
+ # end
290
+ #
291
+ # module Views
292
+ # module Dashboard
293
+ # class Index
294
+ # include Bookshelf::Views
295
+ # end
296
+ #
297
+ # class JsonIndex < Index
298
+ # layout nil
299
+ # end
300
+ # end
301
+ # end
302
+ # end
303
+ #
304
+ # Bookshelf::Application.configuration.namespace layout => :application
305
+ #
306
+ # # All the views will use Bookshelf::Views::ApplicationLayout, unless
307
+ # # they set a different value.
308
+ #
309
+ # Bookshelf::Views::Dashboard::Index.layout
310
+ # # => Bookshelf::Views::ApplicationLayout
311
+ #
312
+ # Bookshelf::Views::Dashboard::JsonIndex.layout
313
+ # # => Hanami::View::Rendering::NullLayout
314
+ def layout(value = nil)
315
+ if value
316
+ @layout = value
317
+ else
318
+ @layout
319
+ end
320
+ end
321
+
322
+ # Templates root.
323
+ # The application will recursively look for templates under this path.
324
+ #
325
+ # By default it's equal to the application `root`.
326
+ #
327
+ # Otherwise, you can specify a different relative path under `root`.
328
+ #
329
+ # This is part of a DSL, for this reason when this method is called with
330
+ # an argument, it will set the corresponding instance variable. When
331
+ # called without, it will return the already set value, or the default.
332
+ #
333
+ # @overload templates(value)
334
+ # Sets the given value
335
+ # @param value [String] the relative path to the templates root.
336
+ #
337
+ # @overload templates
338
+ # Gets the value
339
+ # @return [Pathname] templates root
340
+ #
341
+ # @since 0.1.0
342
+ #
343
+ # @see Hanami::Configuration#root
344
+ # @see http://rdoc.info/gems/hanami-view/Hanami/View/Configuration:root
345
+ #
346
+ # @example Getting the value
347
+ # require 'hanami'
348
+ #
349
+ # module Bookshelf
350
+ # class Application < Hanami::Application
351
+ # end
352
+ # end
353
+ #
354
+ # Bookshelf::Application.configuration.templates
355
+ # # => #<Pathname:/root/path>
356
+ #
357
+ # @example Setting the value
358
+ # require 'hanami'
359
+ #
360
+ # module Bookshelf
361
+ # class Application < Hanami::Application
362
+ # configure do
363
+ # templates 'app/templates'
364
+ # end
365
+ # end
366
+ # end
367
+ #
368
+ # Bookshelf::Application.configuration.templates
369
+ # # => #<Pathname:/root/path/app/templates>
370
+ def templates(value = nil)
371
+ if value
372
+ @templates = value
373
+ else
374
+ root.join @templates.to_s
375
+ end
376
+ end
377
+
378
+ # The application will serve the static assets under these directories.
379
+ #
380
+ # By default it's equal to the `public/` directory under the application
381
+ # `root`.
382
+ #
383
+ # Otherwise, you can add differents relatives paths under `root`.
384
+ #
385
+ # @overload assets
386
+ # Gets the value
387
+ # @return [Hanami::Config::Assets] assets root
388
+ #
389
+ # @since 0.1.0
390
+ #
391
+ # @example Getting the value
392
+ # require 'hanami'
393
+ #
394
+ # module Bookshelf
395
+ # class Application < Hanami::Application
396
+ # end
397
+ # end
398
+ #
399
+ # Bookshelf::Application.configuration.assets
400
+ # # => #<Pathname:/root/path/public>
401
+ #
402
+ # @example Adding new assets paths
403
+ # require 'hanami'
404
+ #
405
+ # module Bookshelf
406
+ # class Application < Hanami::Application
407
+ # configure do
408
+ # assets do
409
+ # sources << [
410
+ # 'vendor/assets'
411
+ # ]
412
+ # end
413
+ # end
414
+ # end
415
+ # end
416
+ #
417
+ # Bookshelf::Application.configuration.assets
418
+ # # => #<Hanami::Config::Assets @root=#<Pathname:/root/path/assets>, @paths=["public"]>
419
+ def assets(&blk)
420
+ if @assets
421
+ @assets.__add(&blk)
422
+ else
423
+ @assets ||= Config::FrameworkConfiguration.new(&blk)
424
+ end
425
+ end
426
+
427
+ # Configure cookies
428
+ # Enable cookies (disabled by default).
429
+ #
430
+ # This is part of a DSL, for this reason when this method is called with
431
+ # an argument, it will set the corresponding instance variable. When
432
+ # called without, it will return the already set value, or the default.
433
+ #
434
+ # @overload cookies(options)
435
+ # Sets the given value with their options.
436
+ # @param options [Hash, TrueClass, FalseClass]
437
+ #
438
+ # @overload cookies
439
+ # Gets the value.
440
+ # @return [Hanami::Config::Cookies]
441
+ #
442
+ # @example Getting the value
443
+ # require 'hanami'
444
+ #
445
+ # module Bookshelf
446
+ # class Application < Hanami::Application
447
+ # end
448
+ # end
449
+ #
450
+ # Bookshelf::Application.configuration.cookies
451
+ # # => #<Hanami::Config::Cookies:0x0000000329f880 @options={}, @default_options={:httponly=>true, :secure=>false}>
452
+ #
453
+ # @example Setting the value
454
+ # require 'hanami'
455
+ #
456
+ # module Bookshelf
457
+ # class Application < Hanami::Application
458
+ # configure do
459
+ # cookies domain: 'hanamirb.org'
460
+ # end
461
+ # end
462
+ # end
463
+ #
464
+ # Bookshelf::Application.configuration.cookies
465
+ # # => #<Hanami::Config::Cookies:0x0000000329f880 @options={:domain=>'hanamirb.org'}, @default_options={:domain=>'hanamirb.org', :httponly=>true, :secure=>false}>
466
+ def cookies(options = nil)
467
+ if options.nil?
468
+ @cookies ||= Config::Cookies.new(self, options)
469
+ else
470
+ @cookies = Config::Cookies.new(self, options)
471
+ end
472
+ end
473
+
474
+ # Configure sessions
475
+ # Enable sessions (disabled by default).
476
+ #
477
+ # This is part of a DSL, for this reason when this method is called with
478
+ # an argument, it will set the corresponding instance variable. When
479
+ # called without, it will return the already set value, or the default.
480
+ #
481
+ # Given Class as adapter it will be used as sessions middleware.
482
+ # Given String as adapter it will be resolved as class name and used as
483
+ # sessions middleware.
484
+ # Given Symbol as adapter it is assumed it's name of the class under
485
+ # Rack::Session namespace that will be used as sessions middleware
486
+ # (e.g. :cookie for Rack::Session::Cookie).
487
+ #
488
+ # By default options include domain inferred from host configuration, and
489
+ # secure flag inferred from scheme configuration.
490
+ #
491
+ # @overload sessions(adapter, options)
492
+ # Sets the given value.
493
+ # @param adapter [Class, String, Symbol] Rack middleware for sessions management
494
+ # @param options [Hash] options to pass to sessions middleware
495
+ #
496
+ # @overload sessions(false)
497
+ # Disables sessions
498
+ #
499
+ # @overload sessions
500
+ # Gets the value.
501
+ # @return [Hanami::Config::Sessions] sessions configuration
502
+ #
503
+ # @since 0.2.0
504
+ #
505
+ # @see Hanami::Configuration#host
506
+ # @see Hanami::Configuration#scheme
507
+ #
508
+ # @example Getting the value
509
+ # require 'hanami'
510
+ #
511
+ # module Bookshelf
512
+ # class Application < Hanami::Application
513
+ # end
514
+ # end
515
+ #
516
+ # Bookshelf::Application.configuration.sessions
517
+ # # => #<Hanami::Config::Sessions:0x00000001ca0c28 @enabled=false>
518
+ #
519
+ # @example Setting the value with symbol
520
+ # require 'hanami'
521
+ #
522
+ # module Bookshelf
523
+ # class Application < Hanami::Application
524
+ # configure do
525
+ # sessions :cookie, secret: 'abc123'
526
+ # end
527
+ # end
528
+ # end
529
+ #
530
+ # Bookshelf::Application.configuration.sessions
531
+ # # => #<Hanami::Config::Sessions:0x00000001589458 @enabled=true, @adapter=:cookie, @options={:domain=>"localhost", :secure=>false}>
532
+ #
533
+ # @example Disabling previusly enabled sessions
534
+ # require 'hanami'
535
+ #
536
+ # module Bookshelf
537
+ # class Application < Hanami::Application
538
+ # configure do
539
+ # sessions :cookie
540
+ # sessions false
541
+ # end
542
+ # end
543
+ # end
544
+ #
545
+ # Bookshelf::Application.configuration.sessions
546
+ # # => #<Hanami::Config::Sessions:0x00000002460d78 @enabled=false>
547
+ #
548
+ def sessions(adapter = nil, options = {})
549
+ if adapter.nil?
550
+ @sessions ||= Config::Sessions.new
551
+ else
552
+ @sessions = Config::Sessions.new(adapter, options, self)
553
+ end
554
+ end
555
+
556
+ # Application load paths
557
+ # The application will recursively load all the Ruby files under these paths.
558
+ #
559
+ # By default it's empty in order to allow developers to decide their own
560
+ # app structure.
561
+ #
562
+ # @return [Hanami::Config::LoadPaths] a set of load paths
563
+ #
564
+ # @since 0.1.0
565
+ #
566
+ # @see Hanami::Configuration#root
567
+ #
568
+ # @example Getting the value
569
+ # require 'hanami'
570
+ #
571
+ # module Bookshelf
572
+ # class Application < Hanami::Application
573
+ # end
574
+ # end
575
+ #
576
+ # Bookshelf::Application.configuration.load_paths
577
+ # # => #<Hanami::Config::LoadPaths:0x007ff4fa212310 @paths=[]>
578
+ #
579
+ # @example Setting the value
580
+ # require 'hanami'
581
+ #
582
+ # module Bookshelf
583
+ # class Application < Hanami::Application
584
+ # configure do
585
+ # load_paths << [
586
+ # 'app/controllers',
587
+ # 'app/views
588
+ # ]
589
+ # end
590
+ # end
591
+ # end
592
+ #
593
+ # Bookshelf::Application.configuration.assets
594
+ # # => #<Hanami::Config::LoadPaths:0x007fe3a20b18e0 @paths=[["app/controllers", "app/views"]]>
595
+ def load_paths
596
+ @load_paths ||= Config::LoadPaths.new
597
+ end
598
+
599
+ # Application routes.
600
+ #
601
+ # Specify a set of routes for the application, by passing a block, or a
602
+ # relative path where to find the file that describes them.
603
+ #
604
+ # By default it's `nil`.
605
+ #
606
+ # This is part of a DSL, for this reason when this method is called with
607
+ # an argument, it will set the corresponding instance variable. When
608
+ # called without, it will return the already set value, or the default.
609
+ #
610
+ # @overload routes(blk)
611
+ # Specify a set of routes in the given block
612
+ # @param blk [Proc] the routes definitions
613
+ #
614
+ # @overload routes(path)
615
+ # Specify a relative path where to find the routes file
616
+ # @param path [String] the relative path
617
+ #
618
+ # @overload routes
619
+ # Gets the value
620
+ # @return [Hanami::Config::Routes] the set of routes
621
+ #
622
+ # @since 0.1.0
623
+ #
624
+ # @see http://rdoc.info/gems/hanami-router/Hanami/Router
625
+ #
626
+ # @example Getting the value
627
+ # require 'hanami'
628
+ #
629
+ # module Bookshelf
630
+ # class Application < Hanami::Application
631
+ # end
632
+ # end
633
+ #
634
+ # Bookshelf::Application.configuration.routes
635
+ # # => nil
636
+ #
637
+ # @example Setting the value, by passing a block
638
+ # require 'hanami'
639
+ #
640
+ # module Bookshelf
641
+ # class Application < Hanami::Application
642
+ # configure do
643
+ # routes do
644
+ # get '/', to: 'dashboard#index'
645
+ # resources :books
646
+ # end
647
+ # end
648
+ # end
649
+ # end
650
+ #
651
+ # Bookshelf::Application.configuration.routes
652
+ # # => #<Hanami::Config::Routes:0x007ff50a991388 @blk=#<Proc:0x007ff50a991338@(irb):4>, @path=#<Pathname:.>>
653
+ #
654
+ # @example Setting the value, by passing a relative path
655
+ # require 'hanami'
656
+ #
657
+ # module Bookshelf
658
+ # class Application < Hanami::Application
659
+ # configure do
660
+ # routes 'config/routes'
661
+ # end
662
+ # end
663
+ # end
664
+ #
665
+ # Bookshelf::Application.configuration.routes
666
+ # # => #<Hanami::Config::Routes:0x007ff50a991388 @blk=nil, @path=#<Pathname:config/routes.rb>>
667
+ def routes(path = nil, &blk)
668
+ if path or block_given?
669
+ @routes = Config::Routes.new(root, path, &blk)
670
+ else
671
+ @routes
672
+ end
673
+ end
674
+
675
+ # Body parsing configuration.
676
+ #
677
+ # Specify a set of parsers for specific mime types that your application will use. This method will
678
+ # return the application's parsers which you can use to add existing and new custom parsers for your
679
+ # application to use.
680
+ #
681
+ # By default it's an empty `Array`
682
+ #
683
+ # This is part of a DSL, for this reason when this method is called with
684
+ # an argument, it will set the corresponding instance variable. When
685
+ # called without, it will return the already set value, or the default.
686
+ #
687
+ # @overload body_parsers(parsers)
688
+ # Specify a set of body parsers.
689
+ # @param parsers [Array] the body parser definitions
690
+ #
691
+ # @overload body_parsers
692
+ # Gets the value
693
+ # @return [Array] the set of parsers
694
+ #
695
+ # @since 0.2.0
696
+ #
697
+ # @example Getting the value
698
+ # require 'hanami'
699
+ #
700
+ # module Bookshelf
701
+ # class Application < Hanami::Application
702
+ # end
703
+ # end
704
+ #
705
+ # Bookshelf::Application.configuration.body_parsers
706
+ # # => []
707
+ #
708
+ # @example Setting the value
709
+ # require 'hanami'
710
+ #
711
+ # module Bookshelf
712
+ # class Application < Hanami::Application
713
+ # configure do
714
+ # body_parsers :json, XmlParser.new
715
+ # end
716
+ # end
717
+ # end
718
+ #
719
+ # Bookshelf::Application.configuration.body_parsers
720
+ # # => [:json, XmlParser.new]
721
+ #
722
+ # @example Setting a new value after one is set.
723
+ # require 'hanami'
724
+ #
725
+ # module Bookshelf
726
+ # class Application < Hanami::Application
727
+ # configure do
728
+ # body_parsers :json
729
+ # body_parsers XmlParser.new
730
+ # end
731
+ # end
732
+ # end
733
+ #
734
+ # Bookshelf::Application.configuration.body_parsers
735
+ # # => [XmlParser.new]
736
+ #
737
+ def body_parsers(*parsers)
738
+ if parsers.empty?
739
+ @body_parsers ||= []
740
+ else
741
+ @body_parsers = parsers
742
+ end
743
+ end
744
+
745
+ # Application middleware.
746
+ #
747
+ # Specify middleware that your application will use. This method will return
748
+ # the application's underlying Middleware stack which you can use to add new
749
+ # middleware for your application to use. By default, the middleware stack
750
+ # will contain only `Rack::Static` and `Rack::MethodOverride`. However, if
751
+ # `assets false` was specified # in the configuration block, the default
752
+ # `Rack::Static` will be removed.
753
+ #
754
+ # @since 0.2.0
755
+ #
756
+ # @see http://rdoc.info/gems/rack/Rack/Static
757
+ # @see Hanami::Middleware#use
758
+ #
759
+ # @example
760
+ # require 'hanami'
761
+ #
762
+ # module Bookshelf
763
+ # class Application < Hanami::Application
764
+ # configure do
765
+ # middleware.use Rack::MethodOverride, nil, 'max-age=0, private, must-revalidate'
766
+ # middleware.use Rack::ETag
767
+ # end
768
+ # end
769
+ # end
770
+ def middleware
771
+ @middleware ||= Hanami::Middleware.new(self)
772
+ end
773
+
774
+ # Application collection mapping.
775
+ #
776
+ # Specify a set of collections for the application, by passing a block, or a
777
+ # relative path where to find the file that describes them.
778
+ #
779
+ # By default it's `nil`.
780
+ #
781
+ # This is part of a DSL, for this reason when this method is called with
782
+ # an argument, it will set the corresponding instance variable. When
783
+ # called without, it will return the already set value, or the default.
784
+ #
785
+ # @overload mapping(blk)
786
+ # Specify a set of mapping in the given block
787
+ # @param blk [Proc] the mapping definitions
788
+ #
789
+ # @overload mapping(path)
790
+ # Specify a relative path where to find the mapping file
791
+ # @param path [String] the relative path
792
+ #
793
+ # @overload mapping
794
+ # Gets the value
795
+ # @return [Hanami::Config::Mapping] the set of mappings
796
+ #
797
+ # @since 0.2.0
798
+ #
799
+ # @see http://rdoc.info/gems/hanami-model/Hanami/Mapper
800
+ #
801
+ # @example Getting the value
802
+ # require 'hanami'
803
+ #
804
+ # module Bookshelf
805
+ # class Application < Hanami::Application
806
+ # end
807
+ # end
808
+ #
809
+ # Bookshelf::Application.configuration.mapping
810
+ # # => nil
811
+ #
812
+ # @example Setting the value, by passing a block
813
+ # require 'hanami'
814
+ #
815
+ # module Bookshelf
816
+ # class Application < Hanami::Application
817
+ # configure do
818
+ # mapping do
819
+ # collection :users do
820
+ # entity User
821
+ #
822
+ # attribute :id, Integer
823
+ # attribute :name, String
824
+ # end
825
+ # end
826
+ # end
827
+ # end
828
+ # end
829
+ #
830
+ # Bookshelf::Application.configuration.mapping
831
+ # # => #<Hanami::Config::Mapping:0x007ff50a991388 @blk=#<Proc:0x007ff123991338@(irb):4>, @path=#<Pathname:.>>
832
+ #
833
+ # @example Setting the value, by passing a relative path
834
+ # require 'hanami'
835
+ #
836
+ # module Bookshelf
837
+ # class Application < Hanami::Application
838
+ # configure do
839
+ # mapping 'config/mapping'
840
+ # end
841
+ # end
842
+ # end
843
+ #
844
+ # Bookshelf::Application.configuration.mapping
845
+ # # => #<Hanami::Config::Routes:0x007ff50a991388 @blk=nil, @path=#<Pathname:config/mapping.rb>>
846
+ def mapping(path = nil, &blk)
847
+ if path or block_given?
848
+ @mapping = Config::Mapping.new(root, path, &blk)
849
+ else
850
+ @mapping
851
+ end
852
+ end
853
+
854
+ # Adapter configuration.
855
+ # The application will instantiate adapter instance based on this configuration.
856
+ #
857
+ # The given options must have key pairs :type and :uri
858
+ # If it isn't, at the runtime the framework will raise a
859
+ # `ArgumentError`.
860
+ #
861
+ # This is part of a DSL, for this reason when this method is called with
862
+ # an argument, it will set the corresponding instance variable. When
863
+ # called without, it will return the already set value, or the default.
864
+ #
865
+ # @overload adapter(options)
866
+ # Sets the given type and uri
867
+ # @param options [Hash] a set of options for adapter
868
+ #
869
+ # @overload adapter
870
+ # Gets the value
871
+ # @return [Hash] adapter options
872
+ #
873
+ # @since 0.2.0
874
+ #
875
+ # @see Hanami::Configuration#adapter
876
+ # @see http://rdoc.info/gems/hanami-model/Hanami/Model/Configuration:adapter
877
+ #
878
+ # @example
879
+ # require 'hanami'
880
+ #
881
+ # module Bookshelf
882
+ # class Application < Hanami::Application
883
+ # configure do
884
+ # adapter type: :sql, uri: 'sqlite3://uri'
885
+ # end
886
+ # end
887
+ # end
888
+ #
889
+ # Bookshelf::Application.configuration.adapter
890
+ # # => {type: :sql, uri: 'sqlite3://uri'}
891
+ def adapter(options = {})
892
+ if !options.empty?
893
+ @adapter = options
894
+ else
895
+ @adapter
896
+ end
897
+ end
898
+
899
+ # Set a format as default fallback for all the requests without a strict
900
+ # requirement for the mime type.
901
+ #
902
+ # The given format must be coercible to a symbol, and be a valid mime type
903
+ # alias. If it isn't, at the runtime the framework will raise a
904
+ # `Hanami::Controller::UnknownFormatError`.
905
+ #
906
+ # By default this value is `:html`.
907
+ #
908
+ # This is part of a DSL, for this reason when this method is called with
909
+ # an argument, it will set the corresponding instance variable. When
910
+ # called without, it will return the already set value, or the default.
911
+ #
912
+ # @overload default_request_format(format)
913
+ # Sets the given value
914
+ # @param format [#to_sym] the symbol format
915
+ # @raise [TypeError] if it cannot be coerced to a symbol
916
+ #
917
+ # @overload default_request_format
918
+ # Gets the value
919
+ # @return [Symbol]
920
+ #
921
+ # @since 0.5.0
922
+ #
923
+ # @see http://rdoc.info/gems/hanami-controller/Hanami/Controller/Configuration#default_request_format
924
+ #
925
+ # @example Getting the value
926
+ # require 'hanami'
927
+ #
928
+ # module Bookshelf
929
+ # class Application < Hanami::Application
930
+ # end
931
+ # end
932
+ #
933
+ # Bookshelf::Application.configuration.default_request_format # => :html
934
+ #
935
+ # @example Setting the value
936
+ # require 'hanami'
937
+ #
938
+ # module Bookshelf
939
+ # class Application < Hanami::Application
940
+ # configure do
941
+ # default_request_format :json
942
+ # end
943
+ # end
944
+ # end
945
+ #
946
+ # Bookshelf::Application.configuration.default_request_format # => :json
947
+ def default_request_format(format = nil)
948
+ if format
949
+ @default_request_format = Utils::Kernel.Symbol(format)
950
+ else
951
+ @default_request_format || :html
952
+ end
953
+ end
954
+
955
+ # Set a format to be used for all responses regardless of the request type.
956
+ #
957
+ # The given format must be coercible to a symbol, and be a valid mime type
958
+ # alias. If it isn't, at the runtime the framework will raise a
959
+ # `Hanami::Controller::UnknownFormatError`.
960
+ #
961
+ # By default this value is `:html`.
962
+ #
963
+ # This is part of a DSL, for this reason when this method is called with
964
+ # an argument, it will set the corresponding instance variable. When
965
+ # called without, it will return the already set value, or the default.
966
+ #
967
+ # @overload default_response_format(format)
968
+ # Sets the given value
969
+ # @param format [#to_sym] the symbol format
970
+ # @raise [TypeError] if it cannot be coerced to a symbol
971
+ #
972
+ # @overload default_response_format
973
+ # Gets the value
974
+ # @return [Symbol,nil]
975
+ #
976
+ # @since 0.5.0
977
+ #
978
+ # @see http://rdoc.info/gems/hanami-controller/Hanami/Controller/Configuration#default_response_format
979
+ #
980
+ # @example Getting the value
981
+ # require 'hanami'
982
+ #
983
+ # module Bookshelf
984
+ # class Application < Hanami::Application
985
+ # end
986
+ # end
987
+ #
988
+ # Bookshelf::Application.configuration.default_response_format # => :html
989
+ #
990
+ # @example Setting the value
991
+ # require 'hanami'
992
+ #
993
+ # module Bookshelf
994
+ # class Application < Hanami::Application
995
+ # configure do
996
+ # default_response_format :json
997
+ # end
998
+ # end
999
+ # end
1000
+ #
1001
+ # Bookshelf::Application.configuration.default_response_format # => :json
1002
+ def default_response_format(format = nil)
1003
+ if format
1004
+ @default_response_format = Utils::Kernel.Symbol(format)
1005
+ else
1006
+ @default_response_format
1007
+ end
1008
+ end
1009
+
1010
+ # Set a format as default fallback for all the requests without a strict
1011
+ # requirement for the mime type.
1012
+ #
1013
+ # @since 0.1.0
1014
+ #
1015
+ # @deprecated Use {#default_request_format} instead.
1016
+ def default_format(format = nil)
1017
+ Hanami::Utils::Deprecation.new('default_format is deprecated, please use default_request_format')
1018
+ default_request_format(format)
1019
+ end
1020
+
1021
+ # The URI scheme for this application.
1022
+ # This is used by the router helpers to generate absolute URLs.
1023
+ #
1024
+ # By default this value is `"http"`.
1025
+ #
1026
+ # This is part of a DSL, for this reason when this method is called with
1027
+ # an argument, it will set the corresponding instance variable. When
1028
+ # called without, it will return the already set value, or the default.
1029
+ #
1030
+ # @overload scheme(value)
1031
+ # Sets the given value
1032
+ # @param value [String] the URI scheme
1033
+ #
1034
+ # @overload scheme
1035
+ # Gets the value
1036
+ # @return [String]
1037
+ #
1038
+ # @since 0.1.0
1039
+ #
1040
+ # @see http://en.wikipedia.org/wiki/URI_scheme
1041
+ #
1042
+ # @example Getting the value
1043
+ # require 'hanami'
1044
+ #
1045
+ # module Bookshelf
1046
+ # class Application < Hanami::Application
1047
+ # end
1048
+ # end
1049
+ #
1050
+ # Bookshelf::Application.configuration.scheme # => "http"
1051
+ #
1052
+ # @example Setting the value
1053
+ # require 'hanami'
1054
+ #
1055
+ # module Bookshelf
1056
+ # class Application < Hanami::Application
1057
+ # configure do
1058
+ # scheme 'https'
1059
+ # end
1060
+ # end
1061
+ # end
1062
+ #
1063
+ # Bookshelf::Application.configuration.scheme # => "https"
1064
+ def scheme(value = nil)
1065
+ if value
1066
+ @scheme = value
1067
+ else
1068
+ @scheme ||= 'http'
1069
+ end
1070
+ end
1071
+
1072
+ # Check if the application uses SSL
1073
+ #
1074
+ # @return [FalseClass,TrueClass] the result of the check
1075
+ #
1076
+ # @since 0.2.0
1077
+ #
1078
+ # @see Hanami::Configuration#scheme
1079
+ def ssl?
1080
+ scheme == SSL_SCHEME
1081
+ end
1082
+
1083
+ # The URI host for this application.
1084
+ # This is used by the router helpers to generate absolute URLs.
1085
+ #
1086
+ # By default this value is `"localhost"`.
1087
+ #
1088
+ # This is part of a DSL, for this reason when this method is called with
1089
+ # an argument, it will set the corresponding instance variable. When
1090
+ # called without, it will return the already set value, or the default.
1091
+ #
1092
+ # @overload host(value)
1093
+ # Sets the given value
1094
+ # @param value [String] the URI host
1095
+ #
1096
+ # @overload scheme
1097
+ # Gets the value
1098
+ # @return [String]
1099
+ #
1100
+ # @since 0.1.0
1101
+ #
1102
+ # @see http://en.wikipedia.org/wiki/URI_scheme
1103
+ #
1104
+ # @example Getting the value
1105
+ # require 'hanami'
1106
+ #
1107
+ # module Bookshelf
1108
+ # class Application < Hanami::Application
1109
+ # end
1110
+ # end
1111
+ #
1112
+ # Bookshelf::Application.configuration.host # => "localhost"
1113
+ #
1114
+ # @example Setting the value
1115
+ # require 'hanami'
1116
+ #
1117
+ # module Bookshelf
1118
+ # class Application < Hanami::Application
1119
+ # configure do
1120
+ # host 'bookshelf.org'
1121
+ # end
1122
+ # end
1123
+ # end
1124
+ #
1125
+ # Bookshelf::Application.configuration.host # => "bookshelf.org"
1126
+ def host(value = nil)
1127
+ if value
1128
+ @host = value
1129
+ else
1130
+ @host ||= @env.host
1131
+ end
1132
+ end
1133
+
1134
+ # The URI port for this application.
1135
+ # This is used by the router helpers to generate absolute URLs.
1136
+ #
1137
+ # By default this value is `2300`.
1138
+ #
1139
+ # This is part of a DSL, for this reason when this method is called with
1140
+ # an argument, it will set the corresponding instance variable. When
1141
+ # called without, it will return the already set value, or the default.
1142
+ #
1143
+ # @overload port(value)
1144
+ # Sets the given value
1145
+ # @param value [#to_int] the URI port
1146
+ # @raise [TypeError] if the given value cannot be coerced to Integer
1147
+ #
1148
+ # @overload scheme
1149
+ # Gets the value
1150
+ # @return [String]
1151
+ #
1152
+ # @since 0.1.0
1153
+ #
1154
+ # @see http://en.wikipedia.org/wiki/URI_scheme
1155
+ #
1156
+ # @example Getting the value
1157
+ # require 'hanami'
1158
+ #
1159
+ # module Bookshelf
1160
+ # class Application < Hanami::Application
1161
+ # end
1162
+ # end
1163
+ #
1164
+ # Bookshelf::Application.configuration.port # => 2300
1165
+ #
1166
+ # @example Setting the value
1167
+ # require 'hanami'
1168
+ #
1169
+ # module Bookshelf
1170
+ # class Application < Hanami::Application
1171
+ # configure do
1172
+ # port 8080
1173
+ # end
1174
+ # end
1175
+ # end
1176
+ #
1177
+ # Bookshelf::Application.configuration.port # => 8080
1178
+ def port(value = nil)
1179
+ if value
1180
+ @port = Integer(value)
1181
+ else
1182
+ @port || @env.port
1183
+ end
1184
+ end
1185
+
1186
+ # Defines a relative pattern to find controllers.
1187
+ #
1188
+ # Hanami supports multiple architectures (aka application structures), this
1189
+ # setting helps to understand the namespace where to find applications'
1190
+ # controllers and actions.
1191
+ #
1192
+ # By default this equals to <tt>"Controllers::%{controller}::%{action}"</tt>
1193
+ # That means controllers must be structured like this:
1194
+ # <tt>Bookshelf::Controllers::Dashboard::Index</tt>, where <tt>Bookshelf</tt>
1195
+ # is the application module, <tt>Controllers</tt> is the first value
1196
+ # specified in the pattern, <tt>Dashboard</tt> the controller and
1197
+ # <tt>Index</tt> the action.
1198
+ #
1199
+ # This pattern MUST always contain <tt>"%{controller}"</tt> and <tt>%{action}</tt>.
1200
+ # This pattern SHOULD be used accordingly to <tt>#view_pattern</tt> value.
1201
+ #
1202
+ # This is part of a DSL, for this reason when this method is called with
1203
+ # an argument, it will set the corresponding instance variable. When
1204
+ # called without, it will return the already set value, or the default.
1205
+ #
1206
+ # @overload controller_pattern(value)
1207
+ # Sets the given value
1208
+ # @param value [String] the controller pattern
1209
+ #
1210
+ # @overload controller_pattern
1211
+ # Gets the value
1212
+ # @return [String]
1213
+ #
1214
+ # @since 0.1.0
1215
+ #
1216
+ # @see Hanami::Configuration#view_pattern
1217
+ #
1218
+ # @example Getting the value
1219
+ # require 'hanami'
1220
+ #
1221
+ # module Bookshelf
1222
+ # class Application < Hanami::Application
1223
+ # configure do
1224
+ # routes do
1225
+ # get '/', to: 'dashboard#index'
1226
+ # end
1227
+ # end
1228
+ # end
1229
+ #
1230
+ # module Controllers::Dashboard
1231
+ # class Index
1232
+ # include Bookshelf::Action
1233
+ #
1234
+ # def call(params)
1235
+ # # ...
1236
+ # end
1237
+ # end
1238
+ # end
1239
+ # end
1240
+ #
1241
+ # Bookshelf::Application.configuration.controller_pattern
1242
+ # # => "Controllers::%{controller}::%{action}"
1243
+ #
1244
+ # # All the controllers MUST live under Bookshelf::Controllers
1245
+ #
1246
+ # # GET '/' # => Bookshelf::Controllers::Dashboard::Index
1247
+ #
1248
+ # @example Setting the value
1249
+ # require 'hanami'
1250
+ #
1251
+ # module Bookshelf
1252
+ # class Application < Hanami::Application
1253
+ # configure do
1254
+ # controller_pattern "%{controller}Controller::%{action}"
1255
+ #
1256
+ # routes do
1257
+ # get '/', to: 'dashboard#index'
1258
+ # end
1259
+ # end
1260
+ # end
1261
+ #
1262
+ # module DashboardController
1263
+ # class Index
1264
+ # include Bookshelf::Action
1265
+ #
1266
+ # def call(params)
1267
+ # end
1268
+ # end
1269
+ # end
1270
+ # end
1271
+ #
1272
+ # Bookshelf::Application.configuration.controller_pattern
1273
+ # # => "%{controller}Controller::%{action}"
1274
+ #
1275
+ # # All the controllers are directly under the Bookshelf module
1276
+ #
1277
+ # # GET '/' # => Bookshelf::DashboardController::Index
1278
+ #
1279
+ # @example Setting the value for a top level name structure
1280
+ # require 'hanami'
1281
+ #
1282
+ # module Bookshelf
1283
+ # class Application < Hanami::Application
1284
+ # configure do
1285
+ # namespace Object
1286
+ # controller_pattern "%{controller}Controller::%{action}"
1287
+ #
1288
+ # routes do
1289
+ # get '/', to: 'dashboard#index'
1290
+ # end
1291
+ # end
1292
+ # end
1293
+ # end
1294
+ #
1295
+ # module DashboardController
1296
+ # class Index
1297
+ # incude Bookshelf::Action
1298
+ #
1299
+ # def call(params)
1300
+ # end
1301
+ # end
1302
+ # end
1303
+ #
1304
+ # Bookshelf::Application.configuration.controller_pattern
1305
+ # # => "%{controller}Controller::%{action}"
1306
+ #
1307
+ # # All the controllers are at the top level namespace
1308
+ #
1309
+ # # GET '/' # => DashboardController::Index
1310
+ def controller_pattern(value = nil)
1311
+ if value
1312
+ @controller_pattern = value
1313
+ else
1314
+ @controller_pattern ||= 'Controllers::%{controller}::%{action}'
1315
+ end
1316
+ end
1317
+
1318
+ # Defines a relative pattern to find views:.
1319
+ #
1320
+ # Hanami supports multiple architectures (aka application structures), this
1321
+ # setting helps to understand the namespace where to find applications'
1322
+ # views:.
1323
+ #
1324
+ # By default this equals to <tt>"Views::%{controller}::%{action}"</tt>
1325
+ # That means views must be structured like this:
1326
+ # <tt>Bookshelf::Views::Dashboard::Index</tt>, where <tt>Bookshelf</tt> is
1327
+ # the application module, <tt>Views</tt> is the first value specified in the
1328
+ # pattern, <tt>Dashboard</tt> a module corresponding to the controller name
1329
+ # and <tt>Index</tt> the view, corresponding to the action name.
1330
+ #
1331
+ # This pattern MUST always contain <tt>"%{controller}"</tt> and <tt>%{action}</tt>.
1332
+ # This pattern SHOULD be used accordingly to <tt>#controller_pattern</tt> value.
1333
+ #
1334
+ # This is part of a DSL, for this reason when this method is called with
1335
+ # an argument, it will set the corresponding instance variable. When
1336
+ # called without, it will return the already set value, or the default.
1337
+ #
1338
+ # @overload view_pattern(value)
1339
+ # Sets the given value
1340
+ # @param value [String] the view pattern
1341
+ #
1342
+ # @overload controller_pattern
1343
+ # Gets the value
1344
+ # @return [String]
1345
+ #
1346
+ # @since 0.1.0
1347
+ #
1348
+ # @see Hanami::Configuration#controller_pattern
1349
+ #
1350
+ # @example Getting the value
1351
+ # require 'hanami'
1352
+ #
1353
+ # module Bookshelf
1354
+ # class Application < Hanami::Application
1355
+ # configure do
1356
+ # routes do
1357
+ # get '/', to: 'dashboard#index'
1358
+ # end
1359
+ # end
1360
+ # end
1361
+ #
1362
+ # module Views
1363
+ # module Dashboard
1364
+ # class Index
1365
+ # include Bookshelf::View
1366
+ # end
1367
+ # end
1368
+ # end
1369
+ # end
1370
+ #
1371
+ # Bookshelf::Application.configuration.view_pattern
1372
+ # # => "Views::%{controller}::%{action}"
1373
+ #
1374
+ # # All the views MUST live under Bookshelf::Views
1375
+ #
1376
+ # # GET '/' # => Bookshelf::Views::Dashboard::Index
1377
+ #
1378
+ # @example Setting the value
1379
+ # require 'hanami'
1380
+ #
1381
+ # module Bookshelf
1382
+ # class Application < Hanami::Application
1383
+ # configure do
1384
+ # view_pattern "%{controller}::%{action}"
1385
+ #
1386
+ # routes do
1387
+ # get '/', to: 'dashboard#index'
1388
+ # end
1389
+ # end
1390
+ # end
1391
+ #
1392
+ # module Dashboard
1393
+ # class Index
1394
+ # include Bookshelf::View
1395
+ # end
1396
+ # end
1397
+ # end
1398
+ #
1399
+ # Bookshelf::Application.configuration.view_pattern
1400
+ # # => "%{controller}::%{action}"
1401
+ #
1402
+ # # All the views are directly under the Bookshelf module
1403
+ #
1404
+ # # GET '/' # => Bookshelf::Dashboard::Index
1405
+ #
1406
+ # @example Setting the value for a top level name structure
1407
+ # require 'hanami'
1408
+ #
1409
+ # module Bookshelf
1410
+ # class Application < Hanami::Application
1411
+ # configure do
1412
+ # namespace Object
1413
+ # view_pattern "%{controller}::%{action}"
1414
+ #
1415
+ # routes do
1416
+ # get '/', to: 'dashboard#index'
1417
+ # end
1418
+ # end
1419
+ # end
1420
+ # end
1421
+ #
1422
+ # module Dashboard
1423
+ # class Index
1424
+ # include Bookshelf::View
1425
+ # end
1426
+ # end
1427
+ #
1428
+ # Bookshelf::Application.configuration.view_pattern
1429
+ # # => "%{controller}::%{action}"
1430
+ #
1431
+ # # All the views: are at the top level namespace
1432
+ #
1433
+ # # GET '/' # => Dashboard::Index
1434
+ def view_pattern(value = nil)
1435
+ if value
1436
+ @view_pattern = value
1437
+ else
1438
+ @view_pattern ||= 'Views::%{controller}::%{action}'
1439
+ end
1440
+ end
1441
+
1442
+ # Decide if handle exceptions with an HTTP status or let them uncaught
1443
+ #
1444
+ # If this value is set to `true`, the configured exceptions will return
1445
+ # the specified HTTP status, the rest of them with `500`.
1446
+ #
1447
+ # If this value is set to `false`, the exceptions won't be caught.
1448
+ #
1449
+ # This is part of a DSL, for this reason when this method is called with
1450
+ # an argument, it will set the corresponding instance variable. When
1451
+ # called without, it will return the already set value, or the default.
1452
+ #
1453
+ # @overload handle_exceptions(value)
1454
+ # Sets the given value
1455
+ # @param value [TrueClass, FalseClass] true or false, default to true
1456
+ #
1457
+ # @overload handle_exceptions
1458
+ # Gets the value
1459
+ # @return [TrueClass, FalseClass]
1460
+ #
1461
+ # @since 0.2.0
1462
+ #
1463
+ # @see http://rdoc.info/gems/hanami-controller/Hanami/Controller/Configuration:handle_exceptions
1464
+ # @see http://httpstatus.es/500
1465
+ #
1466
+ # @example Enabled (default)
1467
+ # require 'hanami'
1468
+ #
1469
+ # module Bookshelf
1470
+ # class Application < Hanami::Application
1471
+ # configure do
1472
+ # routes do
1473
+ # get '/error', to: 'error#index'
1474
+ # end
1475
+ # end
1476
+ #
1477
+ # load!
1478
+ # end
1479
+ #
1480
+ # module Controllers::Error
1481
+ # class Index
1482
+ # include Bookshelf::Action
1483
+ #
1484
+ # def call(params)
1485
+ # raise ArgumentError
1486
+ # end
1487
+ # end
1488
+ # end
1489
+ # end
1490
+ #
1491
+ # # GET '/error' # => 500 - Internal Server Error
1492
+ #
1493
+ # @example Disabled
1494
+ # require 'hanami'
1495
+ #
1496
+ # module Bookshelf
1497
+ # class Application < Hanami::Application
1498
+ # configure do
1499
+ # handle_exceptions false
1500
+ #
1501
+ # routes do
1502
+ # get '/error', to: 'error#index'
1503
+ # end
1504
+ # end
1505
+ #
1506
+ # load!
1507
+ # end
1508
+ #
1509
+ # module Controllers::Error
1510
+ # class Index
1511
+ # include Bookshelf::Action
1512
+ #
1513
+ # def call(params)
1514
+ # raise ArgumentError
1515
+ # end
1516
+ # end
1517
+ # end
1518
+ # end
1519
+ #
1520
+ # # GET '/error' # => raises ArgumentError
1521
+ def handle_exceptions(value = nil)
1522
+ if value.nil?
1523
+ @handle_exceptions
1524
+ else
1525
+ @handle_exceptions = value
1526
+ end
1527
+ end
1528
+
1529
+ # It lazily collects all the low level settings for Hanami::Model's
1530
+ # configuration and applies them when the application is loaded.
1531
+ #
1532
+ # NOTE: This forwards all the configurations to Hanami::Model, without
1533
+ # checking them. Before to use this feature, please have a look at the
1534
+ # current Hanami::Model version installed.
1535
+ #
1536
+ # NOTE: This may override some configurations of your application.
1537
+ #
1538
+ # @return [Hanami::Config::FrameworkConfiguration] the configuration
1539
+ #
1540
+ # @since 0.2.0
1541
+ #
1542
+ # @see http://www.rubydoc.info/gems/hanami-model/Hanami/Model/Configuration
1543
+ #
1544
+ # @example Define a setting
1545
+ # require 'hanami'
1546
+ # require 'hanami/model'
1547
+ #
1548
+ # module Bookshelf
1549
+ # class Application < Hanami::Application
1550
+ # configure do
1551
+ # model.adapter type: :memory, uri: 'memory://localhost/database'
1552
+ # end
1553
+ # end
1554
+ # end
1555
+ #
1556
+ # @example Override a setting
1557
+ # require 'hanami'
1558
+ # require 'hanami/model'
1559
+ #
1560
+ # module Bookshelf
1561
+ # class Application < Hanami::Application
1562
+ # configure do
1563
+ # adapter type: :sql, uri: 'postgres://localhost/database'
1564
+ # model.adapter type: :memory, uri: 'memory://localhost/database'
1565
+ # end
1566
+ # end
1567
+ # end
1568
+ #
1569
+ # # The memory adapter will override the SQL one
1570
+ def model
1571
+ @model ||= Config::FrameworkConfiguration.new
1572
+ end
1573
+
1574
+ # It lazily collects all the low level settings for Hanami::Controller's
1575
+ # configuration and applies them when the application is loaded.
1576
+ #
1577
+ # NOTE: This forwards all the configurations to Hanami::Controller, without
1578
+ # checking them. Before to use this feature, please have a look at the
1579
+ # current Hanami::Controller version installed.
1580
+ #
1581
+ # NOTE: This may override some configurations of your application.
1582
+ #
1583
+ # @return [Hanami::Config::FrameworkConfiguration] the configuration
1584
+ #
1585
+ # @since 0.2.0
1586
+ #
1587
+ # @see http://www.rubydoc.info/gems/hanami-controller/Hanami/Controller/Configuration
1588
+ #
1589
+ # @example Define a setting
1590
+ # require 'hanami'
1591
+ #
1592
+ # module Bookshelf
1593
+ # class Application < Hanami::Application
1594
+ # configure do
1595
+ # controller.default_request_format :json
1596
+ # end
1597
+ # end
1598
+ # end
1599
+ #
1600
+ # @example Override a setting
1601
+ # require 'hanami'
1602
+ #
1603
+ # module Bookshelf
1604
+ # class Application < Hanami::Application
1605
+ # configure do
1606
+ # handle_exceptions false
1607
+ # controller.handle_exceptions true
1608
+ # end
1609
+ # end
1610
+ # end
1611
+ #
1612
+ # # Exceptions will be handled
1613
+ def controller
1614
+ @controller ||= Config::FrameworkConfiguration.new
1615
+ end
1616
+
1617
+ # It lazily collects all the low level settings for Hanami::View's
1618
+ # configuration and applies them when the application is loaded.
1619
+ #
1620
+ # NOTE: This forwards all the configurations to Hanami::View, without
1621
+ # checking them. Before to use this feature, please have a look at the
1622
+ # current Hanami::View version installed.
1623
+ #
1624
+ # NOTE: This may override some configurations of your application.
1625
+ #
1626
+ # @return [Hanami::Config::FrameworkConfiguration] the configuration
1627
+ #
1628
+ # @since 0.2.0
1629
+ #
1630
+ # @see http://www.rubydoc.info/gems/hanami-view/Hanami/View/Configuration
1631
+ #
1632
+ # @example Define a setting
1633
+ # require 'hanami'
1634
+ #
1635
+ # module Bookshelf
1636
+ # class Application < Hanami::Application
1637
+ # configure do
1638
+ # view.layout :application
1639
+ # end
1640
+ # end
1641
+ # end
1642
+ #
1643
+ # @example Override a setting
1644
+ # require 'hanami'
1645
+ #
1646
+ # module Bookshelf
1647
+ # class Application < Hanami::Application
1648
+ # configure do
1649
+ # layout :application
1650
+ # view.layout :backend
1651
+ # end
1652
+ # end
1653
+ # end
1654
+ #
1655
+ # # It will use `:backend` layout
1656
+ def view
1657
+ @view ||= Config::FrameworkConfiguration.new
1658
+ end
1659
+
1660
+ # Defines a logger instance to the configuration
1661
+ #
1662
+ # This logger instance will be used to set the logger available on application module
1663
+ #
1664
+ # If no logger instance is defined, a Hanami::Logger will be set by default
1665
+ #
1666
+ # @return [Logger, NilClass] logger instance
1667
+ #
1668
+ # @since 0.5.0
1669
+ #
1670
+ # @example Define a logger
1671
+ # require 'hanami'
1672
+ #
1673
+ # module Bookshelf
1674
+ # class Application < Hanami::Application
1675
+ # configure do
1676
+ # logger Logger.new(STDOUT)
1677
+ # end
1678
+ # load!
1679
+ # end
1680
+ #
1681
+ # module Controllers::Error
1682
+ # class Index
1683
+ # include Bookshelf::Action
1684
+ #
1685
+ # def call(params)
1686
+ # Bookshelf::Logger.info "Logging to STDOUT"
1687
+ # end
1688
+ # end
1689
+ # end
1690
+ # end
1691
+ #
1692
+ def logger(value = nil)
1693
+ if value.nil?
1694
+ @logger
1695
+ else
1696
+ @logger = value
1697
+ end
1698
+ end
1699
+
1700
+ # This options is used as a bridge between container and router application.
1701
+ #
1702
+ # @return [String, NilClass] path prefix for routes
1703
+ #
1704
+ # @since 0.4.0
1705
+ # @api private
1706
+ def path_prefix(value = nil)
1707
+ if value.nil?
1708
+ @path_prefix
1709
+ else
1710
+ @path_prefix = value
1711
+ end
1712
+ end
1713
+
1714
+ private
1715
+
1716
+ # @since 0.2.0
1717
+ # @api private
1718
+ def evaluate_configurations!
1719
+ configurations.each { |c| instance_eval(&c) }
1720
+ end
1721
+
1722
+ # @since 0.2.0
1723
+ # @api private
1724
+ def configurations
1725
+ [ @blk ] + @configurations[@env.environment]
1726
+ end
1727
+ end
1728
+ end