hanami 1.3.1 → 2.0.0.alpha2

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 +110 -6
  3. data/FEATURES.md +9 -1
  4. data/LICENSE.md +1 -1
  5. data/README.md +4 -5
  6. data/hanami.gemspec +26 -30
  7. data/lib/hanami.rb +40 -243
  8. data/lib/hanami/application.rb +338 -174
  9. data/lib/hanami/application/autoloader/inflector_adapter.rb +22 -0
  10. data/lib/hanami/application/container/boot/inflector.rb +7 -0
  11. data/lib/hanami/application/container/boot/logger.rb +8 -0
  12. data/lib/hanami/application/container/boot/rack_logger.rb +19 -0
  13. data/lib/hanami/application/container/boot/rack_monitor.rb +12 -0
  14. data/lib/hanami/application/container/boot/settings.rb +7 -0
  15. data/lib/hanami/application/router.rb +59 -0
  16. data/lib/hanami/application/routing/middleware/stack.rb +89 -0
  17. data/lib/hanami/application/routing/resolver.rb +82 -0
  18. data/lib/hanami/application/routing/resolver/node.rb +50 -0
  19. data/lib/hanami/application/routing/resolver/trie.rb +59 -0
  20. data/lib/hanami/application/settings.rb +23 -0
  21. data/lib/hanami/application/settings/definition.rb +26 -0
  22. data/lib/hanami/application/settings/loader.rb +97 -0
  23. data/lib/hanami/application/settings/struct.rb +65 -0
  24. data/lib/hanami/boot.rb +5 -0
  25. data/lib/hanami/cli/application/cli.rb +40 -0
  26. data/lib/hanami/cli/application/command.rb +47 -0
  27. data/lib/hanami/cli/application/commands.rb +16 -0
  28. data/lib/hanami/cli/application/commands/console.rb +81 -0
  29. data/lib/hanami/cli/base_command.rb +48 -0
  30. data/lib/hanami/cli/commands.rb +6 -42
  31. data/lib/hanami/cli/commands/command.rb +20 -30
  32. data/lib/hanami/cli/commands/server.rb +63 -14
  33. data/lib/hanami/configuration.rb +248 -190
  34. data/lib/hanami/configuration/middleware.rb +8 -28
  35. data/lib/hanami/configuration/router.rb +50 -0
  36. data/lib/hanami/configuration/sessions.rb +50 -0
  37. data/lib/hanami/init.rb +5 -0
  38. data/lib/hanami/server.rb +5 -69
  39. data/lib/hanami/setup.rb +8 -2
  40. data/lib/hanami/slice.rb +138 -0
  41. data/lib/hanami/version.rb +3 -1
  42. data/lib/hanami/web/rack_logger.rb +96 -0
  43. metadata +84 -228
  44. data/bin/hanami +0 -6
  45. data/lib/hanami/action/csrf_protection.rb +0 -211
  46. data/lib/hanami/action/routing_helpers.rb +0 -40
  47. data/lib/hanami/app.rb +0 -96
  48. data/lib/hanami/application_configuration.rb +0 -1495
  49. data/lib/hanami/application_name.rb +0 -108
  50. data/lib/hanami/application_namespace.rb +0 -14
  51. data/lib/hanami/assets/asset.rb +0 -72
  52. data/lib/hanami/assets/static.rb +0 -102
  53. data/lib/hanami/cli/commands/assets.rb +0 -16
  54. data/lib/hanami/cli/commands/assets/precompile.rb +0 -42
  55. data/lib/hanami/cli/commands/console.rb +0 -95
  56. data/lib/hanami/cli/commands/db.rb +0 -32
  57. data/lib/hanami/cli/commands/db/apply.rb +0 -32
  58. data/lib/hanami/cli/commands/db/console.rb +0 -44
  59. data/lib/hanami/cli/commands/db/create.rb +0 -32
  60. data/lib/hanami/cli/commands/db/drop.rb +0 -32
  61. data/lib/hanami/cli/commands/db/migrate.rb +0 -39
  62. data/lib/hanami/cli/commands/db/prepare.rb +0 -32
  63. data/lib/hanami/cli/commands/db/rollback.rb +0 -59
  64. data/lib/hanami/cli/commands/db/version.rb +0 -32
  65. data/lib/hanami/cli/commands/destroy.rb +0 -26
  66. data/lib/hanami/cli/commands/destroy/action.rb +0 -137
  67. data/lib/hanami/cli/commands/destroy/app.rb +0 -159
  68. data/lib/hanami/cli/commands/destroy/mailer.rb +0 -74
  69. data/lib/hanami/cli/commands/destroy/migration.rb +0 -51
  70. data/lib/hanami/cli/commands/destroy/model.rb +0 -84
  71. data/lib/hanami/cli/commands/generate.rb +0 -28
  72. data/lib/hanami/cli/commands/generate/action.rb +0 -321
  73. data/lib/hanami/cli/commands/generate/action/action.erb +0 -7
  74. data/lib/hanami/cli/commands/generate/action/action_spec.minitest.erb +0 -11
  75. data/lib/hanami/cli/commands/generate/action/action_spec.rspec.erb +0 -9
  76. data/lib/hanami/cli/commands/generate/action/action_without_view.erb +0 -7
  77. data/lib/hanami/cli/commands/generate/action/view.erb +0 -7
  78. data/lib/hanami/cli/commands/generate/action/view_spec.minitest.erb +0 -12
  79. data/lib/hanami/cli/commands/generate/action/view_spec.rspec.erb +0 -10
  80. data/lib/hanami/cli/commands/generate/app.rb +0 -243
  81. data/lib/hanami/cli/commands/generate/app/application.erb +0 -313
  82. data/lib/hanami/cli/commands/generate/app/favicon.ico +0 -0
  83. data/lib/hanami/cli/commands/generate/app/gitkeep.erb +0 -0
  84. data/lib/hanami/cli/commands/generate/app/layout.erb +0 -7
  85. data/lib/hanami/cli/commands/generate/app/layout_spec.minitest.erb +0 -10
  86. data/lib/hanami/cli/commands/generate/app/layout_spec.rspec.erb +0 -10
  87. data/lib/hanami/cli/commands/generate/app/routes.erb +0 -5
  88. data/lib/hanami/cli/commands/generate/app/template.erb.erb +0 -10
  89. data/lib/hanami/cli/commands/generate/app/template.haml.erb +0 -7
  90. data/lib/hanami/cli/commands/generate/app/template.slim.erb +0 -8
  91. data/lib/hanami/cli/commands/generate/mailer.rb +0 -104
  92. data/lib/hanami/cli/commands/generate/mailer/mailer.erb +0 -9
  93. data/lib/hanami/cli/commands/generate/mailer/mailer_spec.minitest.erb +0 -7
  94. data/lib/hanami/cli/commands/generate/mailer/mailer_spec.rspec.erb +0 -5
  95. data/lib/hanami/cli/commands/generate/migration.rb +0 -41
  96. data/lib/hanami/cli/commands/generate/migration/migration.erb +0 -4
  97. data/lib/hanami/cli/commands/generate/model.rb +0 -125
  98. data/lib/hanami/cli/commands/generate/model/entity.erb +0 -2
  99. data/lib/hanami/cli/commands/generate/model/entity_spec.minitest.erb +0 -5
  100. data/lib/hanami/cli/commands/generate/model/entity_spec.rspec.erb +0 -3
  101. data/lib/hanami/cli/commands/generate/model/migration.erb +0 -10
  102. data/lib/hanami/cli/commands/generate/model/repository.erb +0 -5
  103. data/lib/hanami/cli/commands/generate/model/repository_spec.minitest.erb +0 -5
  104. data/lib/hanami/cli/commands/generate/model/repository_spec.rspec.erb +0 -3
  105. data/lib/hanami/cli/commands/generate/secret.rb +0 -48
  106. data/lib/hanami/cli/commands/new.rb +0 -578
  107. data/lib/hanami/cli/commands/new/.env.development.erb +0 -3
  108. data/lib/hanami/cli/commands/new/.env.test.erb +0 -3
  109. data/lib/hanami/cli/commands/new/.gitkeep.erb +0 -0
  110. data/lib/hanami/cli/commands/new/Gemfile.erb +0 -58
  111. data/lib/hanami/cli/commands/new/README.md.erb +0 -33
  112. data/lib/hanami/cli/commands/new/config.ru.erb +0 -3
  113. data/lib/hanami/cli/commands/new/config/boot.erb +0 -2
  114. data/lib/hanami/cli/commands/new/config/environment.erb +0 -49
  115. data/lib/hanami/cli/commands/new/gitignore.erb +0 -2
  116. data/lib/hanami/cli/commands/new/gitignore_with_sqlite.erb +0 -3
  117. data/lib/hanami/cli/commands/new/hanamirc.erb +0 -3
  118. data/lib/hanami/cli/commands/new/lib/project.erb +0 -2
  119. data/lib/hanami/cli/commands/new/minitest/Rakefile.erb +0 -12
  120. data/lib/hanami/cli/commands/new/minitest/features_helper.erb +0 -11
  121. data/lib/hanami/cli/commands/new/minitest/spec_helper.erb +0 -7
  122. data/lib/hanami/cli/commands/new/rspec/Rakefile.erb +0 -9
  123. data/lib/hanami/cli/commands/new/rspec/capybara.erb +0 -8
  124. data/lib/hanami/cli/commands/new/rspec/features_helper.erb +0 -12
  125. data/lib/hanami/cli/commands/new/rspec/rspec.erb +0 -2
  126. data/lib/hanami/cli/commands/new/rspec/spec_helper.erb +0 -103
  127. data/lib/hanami/cli/commands/new/schema.sql.erb +0 -0
  128. data/lib/hanami/cli/commands/project.rb +0 -421
  129. data/lib/hanami/cli/commands/routes.rb +0 -21
  130. data/lib/hanami/cli/commands/templates.rb +0 -31
  131. data/lib/hanami/cli/commands/version.rb +0 -19
  132. data/lib/hanami/common_logger.rb +0 -93
  133. data/lib/hanami/components.rb +0 -156
  134. data/lib/hanami/components/app/assets.rb +0 -59
  135. data/lib/hanami/components/app/controller.rb +0 -74
  136. data/lib/hanami/components/app/routes.rb +0 -59
  137. data/lib/hanami/components/app/view.rb +0 -44
  138. data/lib/hanami/components/component.rb +0 -182
  139. data/lib/hanami/components/components.rb +0 -479
  140. data/lib/hanami/components/routes_inspector.rb +0 -72
  141. data/lib/hanami/config/cookies.rb +0 -69
  142. data/lib/hanami/config/framework_configuration.rb +0 -43
  143. data/lib/hanami/config/load_paths.rb +0 -46
  144. data/lib/hanami/config/mapper.rb +0 -47
  145. data/lib/hanami/config/routes.rb +0 -20
  146. data/lib/hanami/config/security.rb +0 -110
  147. data/lib/hanami/config/sessions.rb +0 -103
  148. data/lib/hanami/configuration/app.rb +0 -18
  149. data/lib/hanami/early_hints.rb +0 -129
  150. data/lib/hanami/env.rb +0 -69
  151. data/lib/hanami/environment.rb +0 -513
  152. data/lib/hanami/environment_application_configurations.rb +0 -37
  153. data/lib/hanami/frameworks.rb +0 -22
  154. data/lib/hanami/hanamirc.rb +0 -169
  155. data/lib/hanami/mailer/glue.rb +0 -27
  156. data/lib/hanami/middleware_stack.rb +0 -172
  157. data/lib/hanami/rake_helper.rb +0 -78
  158. data/lib/hanami/rake_tasks.rb +0 -2
  159. data/lib/hanami/rendering_policy.rb +0 -95
  160. data/lib/hanami/routes.rb +0 -219
  161. data/lib/hanami/routing/default.rb +0 -32
  162. data/lib/hanami/static.rb +0 -63
  163. data/lib/hanami/templates/default.html.erb +0 -30
  164. data/lib/hanami/templates/welcome.html.erb +0 -52
  165. data/lib/hanami/views/default.rb +0 -37
  166. data/lib/hanami/views/default_template_finder.rb +0 -22
  167. data/lib/hanami/views/null_view.rb +0 -15
  168. data/lib/hanami/welcome.rb +0 -36
data/bin/hanami DELETED
@@ -1,6 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'bundler'
3
- require 'hanami/cli/commands'
4
-
5
- Bundler.require(:plugins) if File.exist?(ENV["BUNDLE_GEMFILE"] || "Gemfile")
6
- Hanami::CLI.new(Hanami::CLI::Commands).call
@@ -1,211 +0,0 @@
1
- require 'securerandom'
2
-
3
- module Hanami
4
- # @api private
5
- module Action
6
- # Invalid CSRF Token
7
- #
8
- # @since 0.4.0
9
- class InvalidCSRFTokenError < ::StandardError
10
- end
11
-
12
- # CSRF Protection
13
- #
14
- # This security mechanism is enabled automatically if sessions are turned on.
15
- #
16
- # It stores a "challenge" token in session. For each "state changing request"
17
- # (eg. <tt>POST</tt>, <tt>PATCH</tt> etc..), we should send a special param:
18
- # <tt>_csrf_token</tt>.
19
- #
20
- # If the param matches with the challenge token, the flow can continue.
21
- # Otherwise the application detects an attack attempt, it reset the session
22
- # and <tt>Hanami::Action::InvalidCSRFTokenError</tt> is raised.
23
- #
24
- # We can specify a custom handling strategy, by overriding <tt>#handle_invalid_csrf_token</tt>.
25
- #
26
- # Form helper (<tt>#form_for</tt>) automatically sets a hidden field with the
27
- # correct token. A special view method (<tt>#csrf_token</tt>) is available in
28
- # case the form markup is manually crafted.
29
- #
30
- # We can disable this check on action basis, by overriding <tt>#verify_csrf_token?</tt>.
31
- #
32
- # @since 0.4.0
33
- #
34
- # @see https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29
35
- # @see https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet
36
- #
37
- # @example Custom Handling
38
- # module Web::Controllers::Books
39
- # class Create
40
- # include Web::Action
41
- #
42
- # def call(params)
43
- # # ...
44
- # end
45
- #
46
- # private
47
- #
48
- # def handle_invalid_csrf_token
49
- # Web::Logger.warn "CSRF attack: expected #{ session[:_csrf_token] }, was #{ params[:_csrf_token] }"
50
- # # manual handling
51
- # end
52
- # end
53
- # end
54
- #
55
- # @example Bypass Security Check
56
- # module Web::Controllers::Books
57
- # class Create
58
- # include Web::Action
59
- #
60
- # def call(params)
61
- # # ...
62
- # end
63
- #
64
- # private
65
- #
66
- # def verify_csrf_token?
67
- # false
68
- # end
69
- # end
70
- # end
71
- module CSRFProtection
72
- # Session and params key for CSRF token.
73
- #
74
- # This key is shared with <tt>hanami-controller</tt> and <tt>hanami-helpers</tt>
75
- #
76
- # @since 0.4.0
77
- # @api private
78
- CSRF_TOKEN = :_csrf_token
79
-
80
- # Idempotent HTTP methods
81
- #
82
- # By default, the check isn't performed if the request method is included
83
- # in this list.
84
- #
85
- # @since 0.4.0
86
- # @api private
87
- IDEMPOTENT_HTTP_METHODS = Hash[
88
- 'GET' => true,
89
- 'HEAD' => true,
90
- 'TRACE' => true,
91
- 'OPTIONS' => true
92
- ].freeze
93
-
94
- # @since 0.4.0
95
- # @api private
96
- def self.included(action)
97
- action.class_eval do
98
- before :set_csrf_token, :verify_csrf_token
99
- end unless Hanami.env?(:test)
100
- end
101
-
102
- private
103
- # Set CSRF Token in session
104
- #
105
- # @since 0.4.0
106
- # @api private
107
- def set_csrf_token
108
- session[CSRF_TOKEN] ||= generate_csrf_token
109
- end
110
-
111
- # Verify if CSRF token from params, matches the one stored in session.
112
- # If not, it raises an error.
113
- #
114
- # Don't override this method.
115
- #
116
- # To bypass the security check, please override <tt>#verify_csrf_token?</tt>.
117
- # For custom handling of an attack, please override <tt>#handle_invalid_csrf_token</tt>.
118
- #
119
- # @since 0.4.0
120
- # @api private
121
- def verify_csrf_token
122
- handle_invalid_csrf_token if invalid_csrf_token?
123
- end
124
-
125
- # Verify if CSRF token from params, matches the one stored in session.
126
- #
127
- # Don't override this method.
128
- #
129
- # @since 0.4.0
130
- # @api private
131
- def invalid_csrf_token?
132
- return false unless verify_csrf_token?
133
-
134
- missing_csrf_token? ||
135
- !::Rack::Utils.secure_compare(session[CSRF_TOKEN], params[CSRF_TOKEN])
136
- end
137
-
138
- # Verify the CSRF token was passed in params.
139
- #
140
- # @api private
141
- def missing_csrf_token?
142
- Hanami::Utils::Blank.blank?(params[CSRF_TOKEN])
143
- end
144
-
145
- # Generates a random CSRF Token
146
- #
147
- # @since 0.4.0
148
- # @api private
149
- def generate_csrf_token
150
- SecureRandom.hex(32)
151
- end
152
-
153
- # Decide if perform the check or not.
154
- #
155
- # Override and return <tt>false</tt> if you want to bypass security check.
156
- #
157
- # @since 0.4.0
158
- #
159
- # @example
160
- # module Web::Controllers::Books
161
- # class Create
162
- # include Web::Action
163
- #
164
- # def call(params)
165
- # # ...
166
- # end
167
- #
168
- # private
169
- #
170
- # def verify_csrf_token?
171
- # false
172
- # end
173
- # end
174
- # end
175
- def verify_csrf_token?
176
- !IDEMPOTENT_HTTP_METHODS[request_method]
177
- end
178
-
179
- # Handle CSRF attack.
180
- #
181
- # The default policy resets the session and raises an exception.
182
- #
183
- # Override this method, for custom handling.
184
- #
185
- # @raise [Hanami::Action::InvalidCSRFTokenError]
186
- #
187
- # @since 0.4.0
188
- #
189
- # @example
190
- # module Web::Controllers::Books
191
- # class Create
192
- # include Web::Action
193
- #
194
- # def call(params)
195
- # # ...
196
- # end
197
- #
198
- # private
199
- #
200
- # def handle_invalid_csrf_token
201
- # # custom invalid CSRF management goes here
202
- # end
203
- # end
204
- # end
205
- def handle_invalid_csrf_token
206
- session.clear
207
- raise InvalidCSRFTokenError.new
208
- end
209
- end
210
- end
211
- end
@@ -1,40 +0,0 @@
1
- require 'hanami/utils/string'
2
-
3
- module Hanami
4
- module Action
5
- # Routing helper for full stack Hanami web applications.
6
- #
7
- # For a given application called <tt>Web::Application</tt>, at runtime
8
- # Hanami creates a routes factory called <tt>Web::Routes</tt>.
9
- #
10
- # Included by default in every controller.
11
- #
12
- # @since 0.3.2
13
- #
14
- # @example Usage in controller
15
- # require 'hanami'
16
- #
17
- # module Web::Controllers::Protected
18
- # class Index
19
- # include Web::Action
20
- #
21
- # def call(params)
22
- # redirect_to routes.root_path
23
- # end
24
- # end
25
- # end
26
- module RoutingHelpers
27
- def self.included(base)
28
- factory = "#{Utils::String.namespace(base)}.routes"
29
-
30
- base.class_eval <<-END_EVAL, __FILE__, __LINE__
31
- private
32
-
33
- def routes
34
- #{factory}
35
- end
36
- END_EVAL
37
- end
38
- end
39
- end
40
- end
data/lib/hanami/app.rb DELETED
@@ -1,96 +0,0 @@
1
- require 'rack'
2
- require 'rack/builder'
3
- require 'hanami/router'
4
- require 'hanami/components'
5
- require 'hanami/common_logger'
6
-
7
- module Hanami
8
- # Main application that mounts many Rack and/or Hanami applications.
9
- #
10
- # @see Hanami.app
11
- #
12
- # @since 0.9.0
13
- # @api private
14
- class App
15
- # Initialize a new instance
16
- #
17
- # @param configuration [Hanami::Configuration] general configuration
18
- # @param environment [Hanami::Environment] current environment
19
- #
20
- # @since 0.9.0
21
- # @api private
22
- def initialize(configuration, environment)
23
- Components.resolve('apps')
24
-
25
- @builder = Rack::Builder.new
26
- @routes = Hanami::Router.new
27
-
28
- mount(configuration)
29
- middleware(configuration, environment)
30
- builder.run(routes)
31
-
32
- @app = builder.to_app
33
- end
34
-
35
- # Implements Rack SPEC
36
- #
37
- # @param env [Hash] a Rack env
38
- #
39
- # @return [Array] a serialized Rack response
40
- #
41
- # @since 0.9.0
42
- # @api private
43
- def call(env)
44
- app.call(env)
45
- end
46
-
47
- private
48
-
49
- # @since 1.2.0
50
- # @api private
51
- attr_reader :app
52
-
53
- # @since 0.9.0
54
- # @api private
55
- attr_reader :builder
56
-
57
- # @since 0.9.0
58
- # @api private
59
- attr_reader :routes
60
-
61
- # @since 0.9.0
62
- # @api private
63
- def mount(configuration)
64
- configuration.mounted.each do |klass, app|
65
- routes.mount(klass, at: app.path_prefix)
66
- end
67
- end
68
-
69
- # @since 0.9.0
70
- # @api private
71
- #
72
- # rubocop:disable Metrics/AbcSize
73
- # rubocop:disable Metrics/MethodLength
74
- def middleware(configuration, environment)
75
- builder.use Hanami::CommonLogger, Hanami.logger unless Hanami.logger.nil?
76
- builder.use Rack::ContentLength
77
-
78
- configuration.middleware.each do |m, args, blk|
79
- builder.use(m, *args, &blk)
80
- end
81
-
82
- if configuration.early_hints
83
- require 'hanami/early_hints'
84
- builder.use Hanami::EarlyHints
85
- end
86
-
87
- if middleware = environment.static_assets_middleware # rubocop:disable Lint/AssignmentInCondition
88
- builder.use middleware
89
- end
90
-
91
- builder.use Rack::MethodOverride
92
- end
93
- # rubocop:enable Metrics/MethodLength
94
- # rubocop:enable Metrics/AbcSize
95
- end
96
- end
@@ -1,1495 +0,0 @@
1
- require 'hanami/utils/kernel'
2
- require 'hanami/environment'
3
- require 'hanami/middleware_stack'
4
- require 'hanami/config/cookies'
5
- require 'hanami/config/framework_configuration'
6
- require 'hanami/config/load_paths'
7
- require 'hanami/config/routes'
8
- require 'hanami/config/security'
9
- require 'hanami/config/sessions'
10
-
11
- module Hanami
12
- # Configuration for a Hanami application
13
- #
14
- # @since 0.1.0
15
- class ApplicationConfiguration
16
- # @since 0.2.0
17
- # @api private
18
- #
19
- # @see Hanami::Configuration#ssl?
20
- SSL_SCHEME = 'https'.freeze
21
-
22
- # @since 1.0.0
23
- # @api private
24
- DEFAULT_SSL_PORT = 443
25
-
26
- # @since 0.1.0
27
- # @api private
28
- attr_reader :namespace
29
-
30
- # @since 0.4.0
31
- # @api private
32
- attr_reader :path_prefix
33
-
34
- # Initialize a new configuration instance
35
- #
36
- # @param namespace [Module]
37
- # @param configurations [Hanami::EnvironmentApplicationConfigurations]
38
- # @return [Hanami::Configuration]
39
- #
40
- # @since 0.1.0
41
- # @api private
42
- def initialize(namespace, configurations, path_prefix, env: Environment.new)
43
- @namespace = namespace
44
- @configurations = configurations
45
- @path_prefix = path_prefix
46
- @env = env
47
- @handle_exceptions = true
48
-
49
- evaluate_configurations!
50
- end
51
-
52
- # Returns the security policy
53
- #
54
- # @return [Hanami::Config::Security]
55
- #
56
- # @since 0.3.0
57
- #
58
- # @see Hanami::Config::Security
59
- #
60
- # @example Getting values
61
- # require 'hanami'
62
- #
63
- # module Bookshelf
64
- # class Application < Hanami::Application
65
- # configure do
66
- # security.x_frame_options "ALLOW ALL"
67
- # security.content_security_policy "script-src 'self' https://apis.example.com"
68
- # end
69
- # end
70
- # end
71
- #
72
- # Bookshelf::Application.configuration.security.x_frame_options # => "ALLOW ALL"
73
- # Bookshelf::Application.configuration.security.content_security_policy # => "script-src 'self' https://apis.example.com"
74
- #
75
- # @example Setting values
76
- # require 'hanami'
77
- #
78
- # module Bookshelf
79
- # class Application < Hanami::Application
80
- # configure do
81
- # security.x_frame_options "ALLOW ALL"
82
- # security.content_security_policy "script-src 'self' https://apis.example.com"
83
- # end
84
- # end
85
- # end
86
- def security
87
- @security ||= Config::Security.new
88
- end
89
-
90
- # Force ssl redirection if http scheme is set
91
- #
92
- # @return [Boolean]
93
- #
94
- # @since 0.4.0
95
- #
96
- # @see Hanami::Routing::ForceSsl
97
- def force_ssl(value = nil)
98
- if value
99
- @force_ssl = value
100
- else
101
- @force_ssl || false
102
- end
103
- end
104
-
105
- # The root of the application
106
- #
107
- # By default it returns the current directory, for this reason, **all the
108
- # commands must be executed from the top level directory of the project**.
109
- #
110
- # If for some reason, that constraint above cannot be satisfied, please
111
- # configure the root directory, so that commands can be executed from
112
- # everywhere.
113
- #
114
- # This is part of a DSL, for this reason when this method is called with
115
- # an argument, it will set the corresponding instance variable. When
116
- # called without, it will return the already set value, or the default.
117
- #
118
- # @overload root(value)
119
- # Sets the given value
120
- # @param value [String,Pathname,#to_pathname] The root directory of the app
121
- #
122
- # @overload root
123
- # Gets the value
124
- # @return [Pathname]
125
- # @raise [Errno::ENOENT] if the path cannot be found
126
- #
127
- # @since 0.1.0
128
- #
129
- # @see http://www.ruby-doc.org/core/Dir.html#method-c-pwd
130
- #
131
- # @example Getting the value
132
- # require 'hanami'
133
- #
134
- # module Bookshelf
135
- # class Application < Hanami::Application
136
- # end
137
- # end
138
- #
139
- # Bookshelf::Application.configuration.root # => #<Pathname:/path/to/root>
140
- #
141
- # @example Setting the value
142
- # require 'hanami'
143
- #
144
- # module Bookshelf
145
- # class Application < Hanami::Application
146
- # configure do
147
- # root '/path/to/another/root'
148
- # end
149
- # end
150
- # end
151
- def root(value = nil)
152
- if value
153
- @root = value
154
- else
155
- Utils::Kernel.Pathname(@root || Dir.pwd).realpath
156
- end
157
- end
158
-
159
- # A Hanami::Layout for this application
160
- #
161
- # By default it's `nil`.
162
- #
163
- # It accepts a Symbol as layout name. When the application is loaded, it
164
- # will lookup for the corresponding class.
165
- #
166
- # All the views will use this layout, unless otherwise specified.
167
- #
168
- # This is part of a DSL, for this reason when this method is called with
169
- # an argument, it will set the corresponding instance variable. When
170
- # called without, it will return the already set value, or the default.
171
- #
172
- # @overload layout(value)
173
- # Sets the given value
174
- # @param value [Symbol] the layout name
175
- #
176
- # @overload layout
177
- # Gets the value
178
- # @return [Symbol,nil] the layout name
179
- #
180
- # @since 0.1.0
181
- #
182
- # @see http://rdoc.info/gems/hanami-view/Hanami/Layout
183
- # @see http://rdoc.info/gems/hanami-view/Hanami/View/Configuration:layout
184
- #
185
- # @example Getting the value
186
- # require 'hanami'
187
- #
188
- # module Bookshelf
189
- # class Application < Hanami::Application
190
- # end
191
- # end
192
- #
193
- # Bookshelf::Application.configuration.layout # => nil
194
- #
195
- # # All the views will render without a layout
196
- #
197
- # @example Setting the value
198
- # require 'hanami'
199
- #
200
- # module Bookshelf
201
- # class Application < Hanami::Application
202
- # configure do
203
- # layout :application
204
- # end
205
- # end
206
- #
207
- # module Views
208
- # module Dashboard
209
- # class Index
210
- # include Bookshelf::Views
211
- # end
212
- #
213
- # class JsonIndex < Index
214
- # layout nil
215
- # end
216
- # end
217
- # end
218
- # end
219
- #
220
- # Bookshelf::Application.configuration.namespace layout => :application
221
- #
222
- # # All the views will use Bookshelf::Views::ApplicationLayout, unless
223
- # # they set a different value.
224
- #
225
- # Bookshelf::Views::Dashboard::Index.layout
226
- # # => Bookshelf::Views::ApplicationLayout
227
- #
228
- # Bookshelf::Views::Dashboard::JsonIndex.layout
229
- # # => Hanami::View::Rendering::NullLayout
230
- def layout(value = nil)
231
- if value
232
- @layout = value
233
- else
234
- @layout
235
- end
236
- end
237
-
238
- # Templates root.
239
- # The application will recursively look for templates under this path.
240
- #
241
- # By default it's equal to the application `root`.
242
- #
243
- # Otherwise, you can specify a different relative path under `root`.
244
- #
245
- # This is part of a DSL, for this reason when this method is called with
246
- # an argument, it will set the corresponding instance variable. When
247
- # called without, it will return the already set value, or the default.
248
- #
249
- # @overload templates(value)
250
- # Sets the given value
251
- # @param value [String] the relative path to the templates root.
252
- #
253
- # @overload templates
254
- # Gets the value
255
- # @return [Pathname] templates root
256
- #
257
- # @since 0.1.0
258
- #
259
- # @see Hanami::Configuration#root
260
- # @see http://rdoc.info/gems/hanami-view/Hanami/View/Configuration:root
261
- #
262
- # @example Getting the value
263
- # require 'hanami'
264
- #
265
- # module Bookshelf
266
- # class Application < Hanami::Application
267
- # end
268
- # end
269
- #
270
- # Bookshelf::Application.configuration.templates
271
- # # => #<Pathname:/root/path>
272
- #
273
- # @example Setting the value
274
- # require 'hanami'
275
- #
276
- # module Bookshelf
277
- # class Application < Hanami::Application
278
- # configure do
279
- # templates 'app/templates'
280
- # end
281
- # end
282
- # end
283
- #
284
- # Bookshelf::Application.configuration.templates
285
- # # => #<Pathname:/root/path/app/templates>
286
- def templates(value = nil)
287
- if value
288
- @templates = value
289
- else
290
- root.join @templates.to_s
291
- end
292
- end
293
-
294
- # The application will serve the static assets under these directories.
295
- #
296
- # By default it's equal to the `public/` directory under the application
297
- # `root`.
298
- #
299
- # Otherwise, you can add different relatives paths under `root`.
300
- #
301
- # @overload assets
302
- # Gets the value
303
- # @return [Hanami::Config::Assets] assets root
304
- #
305
- # @since 0.1.0
306
- #
307
- # @example Getting the value
308
- # require 'hanami'
309
- #
310
- # module Bookshelf
311
- # class Application < Hanami::Application
312
- # end
313
- # end
314
- #
315
- # Bookshelf::Application.configuration.assets
316
- # # => #<Pathname:/root/path/public>
317
- #
318
- # @example Adding new assets paths
319
- # require 'hanami'
320
- #
321
- # module Bookshelf
322
- # class Application < Hanami::Application
323
- # configure do
324
- # assets do
325
- # sources << [
326
- # 'vendor/assets'
327
- # ]
328
- # end
329
- # end
330
- # end
331
- # end
332
- #
333
- # Bookshelf::Application.configuration.assets
334
- # # => #<Hanami::Config::Assets @root=#<Pathname:/root/path/assets>, @paths=["public"]>
335
- def assets(&blk)
336
- if @assets
337
- @assets.__add(&blk)
338
- else
339
- @assets ||= Config::FrameworkConfiguration.new(&blk)
340
- end
341
- end
342
-
343
- # Configure cookies
344
- # Enable cookies (disabled by default).
345
- #
346
- # This is part of a DSL, for this reason when this method is called with
347
- # an argument, it will set the corresponding instance variable. When
348
- # called without, it will return the already set value, or the default.
349
- #
350
- # @overload cookies(options)
351
- # Sets the given value with their options.
352
- # @param options [Hash, TrueClass, FalseClass]
353
- #
354
- # @overload cookies
355
- # Gets the value.
356
- # @return [Hanami::Config::Cookies]
357
- #
358
- # @example Getting the value
359
- # require 'hanami'
360
- #
361
- # module Bookshelf
362
- # class Application < Hanami::Application
363
- # end
364
- # end
365
- #
366
- # Bookshelf::Application.configuration.cookies
367
- # # => #<Hanami::Config::Cookies:0x0000000329f880 @options={}, @default_options={:httponly=>true, :secure=>false}>
368
- #
369
- # @example Setting the value
370
- # require 'hanami'
371
- #
372
- # module Bookshelf
373
- # class Application < Hanami::Application
374
- # configure do
375
- # cookies domain: 'hanamirb.org'
376
- # end
377
- # end
378
- # end
379
- #
380
- # Bookshelf::Application.configuration.cookies
381
- # # => #<Hanami::Config::Cookies:0x0000000329f880 @options={:domain=>'hanamirb.org'}, @default_options={:domain=>'hanamirb.org', :httponly=>true, :secure=>false}>
382
- def cookies(options = nil)
383
- if options.nil?
384
- @cookies ||= Config::Cookies.new(self, options)
385
- else
386
- @cookies = Config::Cookies.new(self, options)
387
- end
388
- end
389
-
390
- # Configure sessions
391
- # Enable sessions (disabled by default).
392
- #
393
- # This is part of a DSL, for this reason when this method is called with
394
- # an argument, it will set the corresponding instance variable. When
395
- # called without, it will return the already set value, or the default.
396
- #
397
- # Given Class as adapter it will be used as sessions middleware.
398
- # Given String as adapter it will be resolved as class name and used as
399
- # sessions middleware.
400
- # Given Symbol as adapter it is assumed it's name of the class under
401
- # Rack::Session namespace that will be used as sessions middleware
402
- # (e.g. :cookie for Rack::Session::Cookie).
403
- #
404
- # By default options include domain inferred from host configuration, and
405
- # secure flag inferred from scheme configuration.
406
- #
407
- # @overload sessions(adapter, options)
408
- # Sets the given value.
409
- # @param adapter [Class, String, Symbol] Rack middleware for sessions management
410
- # @param options [Hash] options to pass to sessions middleware
411
- #
412
- # @overload sessions(false)
413
- # Disables sessions
414
- #
415
- # @overload sessions
416
- # Gets the value.
417
- # @return [Hanami::Config::Sessions] sessions configuration
418
- #
419
- # @since 0.2.0
420
- #
421
- # @see Hanami::Configuration#host
422
- # @see Hanami::Configuration#scheme
423
- #
424
- # @example Getting the value
425
- # require 'hanami'
426
- #
427
- # module Bookshelf
428
- # class Application < Hanami::Application
429
- # end
430
- # end
431
- #
432
- # Bookshelf::Application.configuration.sessions
433
- # # => #<Hanami::Config::Sessions:0x00000001ca0c28 @enabled=false>
434
- #
435
- # @example Setting the value with symbol
436
- # require 'hanami'
437
- #
438
- # module Bookshelf
439
- # class Application < Hanami::Application
440
- # configure do
441
- # sessions :cookie, secret: 'abc123'
442
- # end
443
- # end
444
- # end
445
- #
446
- # Bookshelf::Application.configuration.sessions
447
- # # => #<Hanami::Config::Sessions:0x00000001589458 @enabled=true, @adapter=:cookie, @options={:domain=>"localhost", :secure=>false}>
448
- #
449
- # @example Disabling previously enabled sessions
450
- # require 'hanami'
451
- #
452
- # module Bookshelf
453
- # class Application < Hanami::Application
454
- # configure do
455
- # sessions :cookie
456
- # sessions false
457
- # end
458
- # end
459
- # end
460
- #
461
- # Bookshelf::Application.configuration.sessions
462
- # # => #<Hanami::Config::Sessions:0x00000002460d78 @enabled=false>
463
- #
464
- def sessions(adapter = nil, options = {})
465
- if adapter.nil?
466
- @sessions ||= Config::Sessions.new
467
- else
468
- @sessions = Config::Sessions.new(adapter, options, self)
469
- end
470
- end
471
-
472
- # Application load paths
473
- # The application will recursively load all the Ruby files under these paths.
474
- #
475
- # By default it's empty in order to allow developers to decide their own
476
- # app structure.
477
- #
478
- # @return [Hanami::Config::LoadPaths] a set of load paths
479
- #
480
- # @since 0.1.0
481
- #
482
- # @see Hanami::Configuration#root
483
- #
484
- # @example Getting the value
485
- # require 'hanami'
486
- #
487
- # module Bookshelf
488
- # class Application < Hanami::Application
489
- # end
490
- # end
491
- #
492
- # Bookshelf::Application.configuration.load_paths
493
- # # => #<Hanami::Config::LoadPaths:0x007ff4fa212310 @paths=[]>
494
- #
495
- # @example Setting the value
496
- # require 'hanami'
497
- #
498
- # module Bookshelf
499
- # class Application < Hanami::Application
500
- # configure do
501
- # load_paths << [
502
- # 'app/controllers',
503
- # 'app/views
504
- # ]
505
- # end
506
- # end
507
- # end
508
- #
509
- # Bookshelf::Application.configuration.assets
510
- # # => #<Hanami::Config::LoadPaths:0x007fe3a20b18e0 @paths=[["app/controllers", "app/views"]]>
511
- def load_paths
512
- @load_paths ||= Config::LoadPaths.new(root)
513
- end
514
-
515
- # Application routes.
516
- #
517
- # Specify a set of routes for the application, by passing a block, or a
518
- # relative path where to find the file that describes them.
519
- #
520
- # By default it's `nil`.
521
- #
522
- # This is part of a DSL, for this reason when this method is called with
523
- # an argument, it will set the corresponding instance variable. When
524
- # called without, it will return the already set value, or the default.
525
- #
526
- # @overload routes(blk)
527
- # Specify a set of routes in the given block
528
- # @param blk [Proc] the routes definitions
529
- #
530
- # @overload routes(path)
531
- # Specify a relative path where to find the routes file
532
- # @param path [String] the relative path
533
- #
534
- # @overload routes
535
- # Gets the value
536
- # @return [Hanami::Config::Routes] the set of routes
537
- #
538
- # @since 0.1.0
539
- #
540
- # @see http://rdoc.info/gems/hanami-router/Hanami/Router
541
- #
542
- # @example Getting the value
543
- # require 'hanami'
544
- #
545
- # module Bookshelf
546
- # class Application < Hanami::Application
547
- # end
548
- # end
549
- #
550
- # Bookshelf::Application.configuration.routes
551
- # # => nil
552
- #
553
- # @example Setting the value, by passing a block
554
- # require 'hanami'
555
- #
556
- # module Bookshelf
557
- # class Application < Hanami::Application
558
- # configure do
559
- # routes do
560
- # get '/', to: 'dashboard#index'
561
- # resources :books
562
- # end
563
- # end
564
- # end
565
- # end
566
- #
567
- # Bookshelf::Application.configuration.routes
568
- # # => #<Hanami::Config::Routes:0x007ff50a991388 @blk=#<Proc:0x007ff50a991338@(irb):4>, @path=#<Pathname:.>>
569
- #
570
- # @example Setting the value, by passing a relative path
571
- # require 'hanami'
572
- #
573
- # module Bookshelf
574
- # class Application < Hanami::Application
575
- # configure do
576
- # routes 'config/routes'
577
- # end
578
- # end
579
- # end
580
- #
581
- # Bookshelf::Application.configuration.routes
582
- # # => #<Hanami::Config::Routes:0x007ff50a991388 @blk=nil, @path=#<Pathname:config/routes.rb>>
583
- def routes(path = nil, &blk)
584
- if path or block_given?
585
- @routes = Config::Routes.new(root, path, &blk)
586
- else
587
- @routes
588
- end
589
- end
590
-
591
- # Body parsing configuration.
592
- #
593
- # Specify a set of parsers for specific mime types that your application will use. This method will
594
- # return the application's parsers which you can use to add existing and new custom parsers for your
595
- # application to use.
596
- #
597
- # By default it's an empty `Array`
598
- #
599
- # This is part of a DSL, for this reason when this method is called with
600
- # an argument, it will set the corresponding instance variable. When
601
- # called without, it will return the already set value, or the default.
602
- #
603
- # @overload body_parsers(parsers)
604
- # Specify a set of body parsers.
605
- # @param parsers [Array] the body parser definitions
606
- #
607
- # @overload body_parsers
608
- # Gets the value
609
- # @return [Array] the set of parsers
610
- #
611
- # @since 0.2.0
612
- #
613
- # @example Getting the value
614
- # require 'hanami'
615
- #
616
- # module Bookshelf
617
- # class Application < Hanami::Application
618
- # end
619
- # end
620
- #
621
- # Bookshelf::Application.configuration.body_parsers
622
- # # => []
623
- #
624
- # @example Setting the value
625
- # require 'hanami'
626
- #
627
- # module Bookshelf
628
- # class Application < Hanami::Application
629
- # configure do
630
- # body_parsers :json, XmlParser.new
631
- # end
632
- # end
633
- # end
634
- #
635
- # Bookshelf::Application.configuration.body_parsers
636
- # # => [:json, XmlParser.new]
637
- #
638
- # @example Setting a new value after one is set.
639
- # require 'hanami'
640
- #
641
- # module Bookshelf
642
- # class Application < Hanami::Application
643
- # configure do
644
- # body_parsers :json
645
- # body_parsers XmlParser.new
646
- # end
647
- # end
648
- # end
649
- #
650
- # Bookshelf::Application.configuration.body_parsers
651
- # # => [XmlParser.new]
652
- #
653
- def body_parsers(*parsers)
654
- if parsers.empty?
655
- @body_parsers ||= []
656
- else
657
- @body_parsers = parsers
658
- end
659
- end
660
-
661
- # Application middleware.
662
- #
663
- # Specify middleware that your application will use. This method will return
664
- # the application's underlying Middleware stack which you can use to add new
665
- # middleware for your application to use. By default, the middleware stack
666
- # will contain only `Rack::Static` and `Rack::MethodOverride`. However, if
667
- # `assets false` was specified # in the configuration block, the default
668
- # `Rack::Static` will be removed.
669
- #
670
- # @since 0.2.0
671
- #
672
- # @see http://rdoc.info/gems/rack/Rack/Static
673
- # @see Hanami::MiddlewareStack#use
674
- #
675
- # @example
676
- # require 'hanami'
677
- #
678
- # module Bookshelf
679
- # class Application < Hanami::Application
680
- # configure do
681
- # middleware.use Rack::MethodOverride, nil, 'max-age=0, private, must-revalidate'
682
- # middleware.use Rack::ETag
683
- # end
684
- # end
685
- # end
686
- def middleware
687
- @middleware ||= Hanami::MiddlewareStack.new(self)
688
- end
689
-
690
- # Adapter configuration.
691
- # The application will instantiate adapter instance based on this configuration.
692
- #
693
- # The given options must have key pairs :type and :uri
694
- # If it isn't, at the runtime the framework will raise a
695
- # `ArgumentError`.
696
- #
697
- # This is part of a DSL, for this reason when this method is called with
698
- # an argument, it will set the corresponding instance variable. When
699
- # called without, it will return the already set value, or the default.
700
- #
701
- # @overload adapter(options)
702
- # Sets the given type and uri
703
- # @param options [Hash] a set of options for adapter
704
- #
705
- # @overload adapter
706
- # Gets the value
707
- # @return [Hash] adapter options
708
- #
709
- # @since 0.2.0
710
- #
711
- # @see Hanami::Configuration#adapter
712
- # @see http://rdoc.info/gems/hanami-model/Hanami/Model/Configuration:adapter
713
- #
714
- # @example
715
- # require 'hanami'
716
- #
717
- # module Bookshelf
718
- # class Application < Hanami::Application
719
- # configure do
720
- # adapter :sql, 'sqlite3://uri'
721
- # end
722
- # end
723
- # end
724
- #
725
- # Bookshelf::Application.configuration.adapter
726
- # # => [:sql, 'sqlite3://uri']
727
- def adapter(*options)
728
- if !options.empty?
729
- @adapter = options
730
- else
731
- @adapter
732
- end
733
- end
734
-
735
- # Set a format as default fallback for all the requests without a strict
736
- # requirement for the mime type.
737
- #
738
- # The given format must be coercible to a symbol, and be a valid mime type
739
- # alias. If it isn't, at the runtime the framework will raise a
740
- # `Hanami::Controller::UnknownFormatError`.
741
- #
742
- # By default this value is `:html`.
743
- #
744
- # This is part of a DSL, for this reason when this method is called with
745
- # an argument, it will set the corresponding instance variable. When
746
- # called without, it will return the already set value, or the default.
747
- #
748
- # @overload default_request_format(format)
749
- # Sets the given value
750
- # @param format [#to_sym] the symbol format
751
- # @raise [TypeError] if it cannot be coerced to a symbol
752
- #
753
- # @overload default_request_format
754
- # Gets the value
755
- # @return [Symbol]
756
- #
757
- # @since 0.5.0
758
- #
759
- # @see http://rdoc.info/gems/hanami-controller/Hanami/Controller/Configuration#default_request_format
760
- #
761
- # @example Getting the value
762
- # require 'hanami'
763
- #
764
- # module Bookshelf
765
- # class Application < Hanami::Application
766
- # end
767
- # end
768
- #
769
- # Bookshelf::Application.configuration.default_request_format # => :html
770
- #
771
- # @example Setting the value
772
- # require 'hanami'
773
- #
774
- # module Bookshelf
775
- # class Application < Hanami::Application
776
- # configure do
777
- # default_request_format :json
778
- # end
779
- # end
780
- # end
781
- #
782
- # Bookshelf::Application.configuration.default_request_format # => :json
783
- def default_request_format(format = nil)
784
- if format
785
- @default_request_format = Utils::Kernel.Symbol(format)
786
- else
787
- @default_request_format || :html
788
- end
789
- end
790
-
791
- # Set a format to be used for all responses regardless of the request type.
792
- #
793
- # The given format must be coercible to a symbol, and be a valid mime type
794
- # alias. If it isn't, at the runtime the framework will raise a
795
- # `Hanami::Controller::UnknownFormatError`.
796
- #
797
- # By default this value is `:html`.
798
- #
799
- # This is part of a DSL, for this reason when this method is called with
800
- # an argument, it will set the corresponding instance variable. When
801
- # called without, it will return the already set value, or the default.
802
- #
803
- # @overload default_response_format(format)
804
- # Sets the given value
805
- # @param format [#to_sym] the symbol format
806
- # @raise [TypeError] if it cannot be coerced to a symbol
807
- #
808
- # @overload default_response_format
809
- # Gets the value
810
- # @return [Symbol,nil]
811
- #
812
- # @since 0.5.0
813
- #
814
- # @see http://rdoc.info/gems/hanami-controller/Hanami/Controller/Configuration#default_response_format
815
- #
816
- # @example Getting the value
817
- # require 'hanami'
818
- #
819
- # module Bookshelf
820
- # class Application < Hanami::Application
821
- # end
822
- # end
823
- #
824
- # Bookshelf::Application.configuration.default_response_format # => :html
825
- #
826
- # @example Setting the value
827
- # require 'hanami'
828
- #
829
- # module Bookshelf
830
- # class Application < Hanami::Application
831
- # configure do
832
- # default_response_format :json
833
- # end
834
- # end
835
- # end
836
- #
837
- # Bookshelf::Application.configuration.default_response_format # => :json
838
- def default_response_format(format = nil)
839
- if format
840
- @default_response_format = Utils::Kernel.Symbol(format)
841
- else
842
- @default_response_format
843
- end
844
- end
845
-
846
- # The URI scheme for this application.
847
- # This is used by the router helpers to generate absolute URLs.
848
- #
849
- # By default this value is `"http"`.
850
- #
851
- # This is part of a DSL, for this reason when this method is called with
852
- # an argument, it will set the corresponding instance variable. When
853
- # called without, it will return the already set value, or the default.
854
- #
855
- # @overload scheme(value)
856
- # Sets the given value
857
- # @param value [String] the URI scheme
858
- #
859
- # @overload scheme
860
- # Gets the value
861
- # @return [String]
862
- #
863
- # @since 0.1.0
864
- #
865
- # @see http://en.wikipedia.org/wiki/URI_scheme
866
- #
867
- # @example Getting the value
868
- # require 'hanami'
869
- #
870
- # module Bookshelf
871
- # class Application < Hanami::Application
872
- # end
873
- # end
874
- #
875
- # Bookshelf::Application.configuration.scheme # => "http"
876
- #
877
- # @example Setting the value
878
- # require 'hanami'
879
- #
880
- # module Bookshelf
881
- # class Application < Hanami::Application
882
- # configure do
883
- # scheme 'https'
884
- # end
885
- # end
886
- # end
887
- #
888
- # Bookshelf::Application.configuration.scheme # => "https"
889
- def scheme(value = nil)
890
- if value
891
- @scheme = value
892
- else
893
- @scheme ||= 'http'
894
- end
895
- end
896
-
897
- # Check if the application uses SSL
898
- #
899
- # @return [FalseClass,TrueClass] the result of the check
900
- #
901
- # @since 0.2.0
902
- #
903
- # @see Hanami::Configuration#scheme
904
- def ssl?
905
- scheme == SSL_SCHEME
906
- end
907
-
908
- # The URI host for this application.
909
- # This is used by the router helpers to generate absolute URLs.
910
- #
911
- # By default this value is `"localhost"`.
912
- #
913
- # This is part of a DSL, for this reason when this method is called with
914
- # an argument, it will set the corresponding instance variable. When
915
- # called without, it will return the already set value, or the default.
916
- #
917
- # @overload host(value)
918
- # Sets the given value
919
- # @param value [String] the URI host
920
- #
921
- # @overload scheme
922
- # Gets the value
923
- # @return [String]
924
- #
925
- # @since 0.1.0
926
- #
927
- # @see http://en.wikipedia.org/wiki/URI_scheme
928
- #
929
- # @example Getting the value
930
- # require 'hanami'
931
- #
932
- # module Bookshelf
933
- # class Application < Hanami::Application
934
- # end
935
- # end
936
- #
937
- # Bookshelf::Application.configuration.host # => "localhost"
938
- #
939
- # @example Setting the value
940
- # require 'hanami'
941
- #
942
- # module Bookshelf
943
- # class Application < Hanami::Application
944
- # configure do
945
- # host 'bookshelf.org'
946
- # end
947
- # end
948
- # end
949
- #
950
- # Bookshelf::Application.configuration.host # => "bookshelf.org"
951
- def host(value = nil)
952
- if value
953
- @host = value
954
- else
955
- @host ||= @env.host
956
- end
957
- end
958
-
959
- # The URI port for this application.
960
- # This is used by the router helpers to generate absolute URLs.
961
- #
962
- # By default this value is `2300`.
963
- #
964
- # This is part of a DSL, for this reason when this method is called with
965
- # an argument, it will set the corresponding instance variable. When
966
- # called without, it will return the already set value, or the default.
967
- #
968
- # @overload port(value)
969
- # Sets the given value
970
- # @param value [#to_int] the URI port
971
- # @raise [TypeError] if the given value cannot be coerced to Integer
972
- #
973
- # @overload scheme
974
- # Gets the value
975
- # @return [String]
976
- #
977
- # @since 0.1.0
978
- #
979
- # @see http://en.wikipedia.org/wiki/URI_scheme
980
- #
981
- # @example Getting the value
982
- # require 'hanami'
983
- #
984
- # module Bookshelf
985
- # class Application < Hanami::Application
986
- # end
987
- # end
988
- #
989
- # Bookshelf::Application.configuration.port # => 2300
990
- #
991
- # @example Setting the value
992
- # require 'hanami'
993
- #
994
- # module Bookshelf
995
- # class Application < Hanami::Application
996
- # configure do
997
- # port 8080
998
- # end
999
- # end
1000
- # end
1001
- #
1002
- # Bookshelf::Application.configuration.port # => 8080
1003
- def port(value = nil)
1004
- if value
1005
- @port = Integer(value)
1006
- else
1007
- return @port if defined?(@port)
1008
- return @env.port unless @env.default_port?
1009
- return DEFAULT_SSL_PORT if force_ssl
1010
- @env.port
1011
- end
1012
- end
1013
-
1014
- # Defines a relative pattern to find controllers.
1015
- #
1016
- # By default this equals to <tt>"Controllers::%{controller}::%{action}"</tt>
1017
- # That means controllers must be structured like this:
1018
- # <tt>Bookshelf::Controllers::Dashboard::Index</tt>, where <tt>Bookshelf</tt>
1019
- # is the application module, <tt>Controllers</tt> is the first value
1020
- # specified in the pattern, <tt>Dashboard</tt> the controller and
1021
- # <tt>Index</tt> the action.
1022
- #
1023
- # This pattern MUST always contain <tt>"%{controller}"</tt> and <tt>%{action}</tt>.
1024
- # This pattern SHOULD be used accordingly to <tt>#view_pattern</tt> value.
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 controller_pattern(value)
1031
- # Sets the given value
1032
- # @param value [String] the controller pattern
1033
- #
1034
- # @overload controller_pattern
1035
- # Gets the value
1036
- # @return [String]
1037
- #
1038
- # @since 0.1.0
1039
- #
1040
- # @see Hanami::Configuration#view_pattern
1041
- #
1042
- # @example Getting the value
1043
- # require 'hanami'
1044
- #
1045
- # module Bookshelf
1046
- # class Application < Hanami::Application
1047
- # configure do
1048
- # routes do
1049
- # get '/', to: 'dashboard#index'
1050
- # end
1051
- # end
1052
- # end
1053
- #
1054
- # module Controllers::Dashboard
1055
- # class Index
1056
- # include Bookshelf::Action
1057
- #
1058
- # def call(params)
1059
- # # ...
1060
- # end
1061
- # end
1062
- # end
1063
- # end
1064
- #
1065
- # Bookshelf::Application.configuration.controller_pattern
1066
- # # => "Controllers::%{controller}::%{action}"
1067
- #
1068
- # # All the controllers MUST live under Bookshelf::Controllers
1069
- #
1070
- # # GET '/' # => Bookshelf::Controllers::Dashboard::Index
1071
- #
1072
- # @example Setting the value
1073
- # require 'hanami'
1074
- #
1075
- # module Bookshelf
1076
- # class Application < Hanami::Application
1077
- # configure do
1078
- # controller_pattern "%{controller}Controller::%{action}"
1079
- #
1080
- # routes do
1081
- # get '/', to: 'dashboard#index'
1082
- # end
1083
- # end
1084
- # end
1085
- #
1086
- # module DashboardController
1087
- # class Index
1088
- # include Bookshelf::Action
1089
- #
1090
- # def call(params)
1091
- # end
1092
- # end
1093
- # end
1094
- # end
1095
- #
1096
- # Bookshelf::Application.configuration.controller_pattern
1097
- # # => "%{controller}Controller::%{action}"
1098
- #
1099
- # # All the controllers are directly under the Bookshelf module
1100
- #
1101
- # # GET '/' # => Bookshelf::DashboardController::Index
1102
- #
1103
- # @example Setting the value for a top level name structure
1104
- # require 'hanami'
1105
- #
1106
- # module Bookshelf
1107
- # class Application < Hanami::Application
1108
- # configure do
1109
- # controller_pattern "%{controller}Controller::%{action}"
1110
- #
1111
- # routes do
1112
- # get '/', to: 'dashboard#index'
1113
- # end
1114
- # end
1115
- # end
1116
- # end
1117
- #
1118
- # module DashboardController
1119
- # class Index
1120
- # include Bookshelf::Action
1121
- #
1122
- # def call(params)
1123
- # end
1124
- # end
1125
- # end
1126
- #
1127
- # Bookshelf::Application.configuration.controller_pattern
1128
- # # => "%{controller}Controller::%{action}"
1129
- #
1130
- # # All the controllers are at the top level namespace
1131
- #
1132
- # # GET '/' # => DashboardController::Index
1133
- def controller_pattern(value = nil)
1134
- if value
1135
- @controller_pattern = value
1136
- else
1137
- @controller_pattern ||= 'Controllers::%{controller}::%{action}'
1138
- end
1139
- end
1140
-
1141
- # Defines a relative pattern to find views:.
1142
- #
1143
- # By default this equals to <tt>"Views::%{controller}::%{action}"</tt>
1144
- # That means views must be structured like this:
1145
- # <tt>Bookshelf::Views::Dashboard::Index</tt>, where <tt>Bookshelf</tt> is
1146
- # the application module, <tt>Views</tt> is the first value specified in the
1147
- # pattern, <tt>Dashboard</tt> a module corresponding to the controller name
1148
- # and <tt>Index</tt> the view, corresponding to the action name.
1149
- #
1150
- # This pattern MUST always contain <tt>"%{controller}"</tt> and <tt>%{action}</tt>.
1151
- # This pattern SHOULD be used accordingly to <tt>#controller_pattern</tt> value.
1152
- #
1153
- # This is part of a DSL, for this reason when this method is called with
1154
- # an argument, it will set the corresponding instance variable. When
1155
- # called without, it will return the already set value, or the default.
1156
- #
1157
- # @overload view_pattern(value)
1158
- # Sets the given value
1159
- # @param value [String] the view pattern
1160
- #
1161
- # @overload controller_pattern
1162
- # Gets the value
1163
- # @return [String]
1164
- #
1165
- # @since 0.1.0
1166
- #
1167
- # @see Hanami::Configuration#controller_pattern
1168
- #
1169
- # @example Getting the value
1170
- # require 'hanami'
1171
- #
1172
- # module Bookshelf
1173
- # class Application < Hanami::Application
1174
- # configure do
1175
- # routes do
1176
- # get '/', to: 'dashboard#index'
1177
- # end
1178
- # end
1179
- # end
1180
- #
1181
- # module Views
1182
- # module Dashboard
1183
- # class Index
1184
- # include Bookshelf::View
1185
- # end
1186
- # end
1187
- # end
1188
- # end
1189
- #
1190
- # Bookshelf::Application.configuration.view_pattern
1191
- # # => "Views::%{controller}::%{action}"
1192
- #
1193
- # # All the views MUST live under Bookshelf::Views
1194
- #
1195
- # # GET '/' # => Bookshelf::Views::Dashboard::Index
1196
- #
1197
- # @example Setting the value
1198
- # require 'hanami'
1199
- #
1200
- # module Bookshelf
1201
- # class Application < Hanami::Application
1202
- # configure do
1203
- # view_pattern "%{controller}::%{action}"
1204
- #
1205
- # routes do
1206
- # get '/', to: 'dashboard#index'
1207
- # end
1208
- # end
1209
- # end
1210
- #
1211
- # module Dashboard
1212
- # class Index
1213
- # include Bookshelf::View
1214
- # end
1215
- # end
1216
- # end
1217
- #
1218
- # Bookshelf::Application.configuration.view_pattern
1219
- # # => "%{controller}::%{action}"
1220
- #
1221
- # # All the views are directly under the Bookshelf module
1222
- #
1223
- # # GET '/' # => Bookshelf::Dashboard::Index
1224
- #
1225
- # @example Setting the value for a top level name structure
1226
- # require 'hanami'
1227
- #
1228
- # module Bookshelf
1229
- # class Application < Hanami::Application
1230
- # configure do
1231
- # view_pattern "%{controller}::%{action}"
1232
- #
1233
- # routes do
1234
- # get '/', to: 'dashboard#index'
1235
- # end
1236
- # end
1237
- # end
1238
- # end
1239
- #
1240
- # module Dashboard
1241
- # class Index
1242
- # include Bookshelf::View
1243
- # end
1244
- # end
1245
- #
1246
- # Bookshelf::Application.configuration.view_pattern
1247
- # # => "%{controller}::%{action}"
1248
- #
1249
- # # All the views: are at the top level namespace
1250
- #
1251
- # # GET '/' # => Dashboard::Index
1252
- def view_pattern(value = nil)
1253
- if value
1254
- @view_pattern = value
1255
- else
1256
- @view_pattern ||= 'Views::%{controller}::%{action}'
1257
- end
1258
- end
1259
-
1260
- # Decide if handle exceptions with an HTTP status or let them uncaught
1261
- #
1262
- # If this value is set to `true`, the configured exceptions will return
1263
- # the specified HTTP status, the rest of them with `500`.
1264
- #
1265
- # If this value is set to `false`, the exceptions won't be caught.
1266
- #
1267
- # This is part of a DSL, for this reason when this method is called with
1268
- # an argument, it will set the corresponding instance variable. When
1269
- # called without, it will return the already set value, or the default.
1270
- #
1271
- # @overload handle_exceptions(value)
1272
- # Sets the given value
1273
- # @param value [TrueClass, FalseClass] true or false, default to true
1274
- #
1275
- # @overload handle_exceptions
1276
- # Gets the value
1277
- # @return [TrueClass, FalseClass]
1278
- #
1279
- # @since 0.2.0
1280
- #
1281
- # @see http://rdoc.info/gems/hanami-controller/Hanami/Controller/Configuration:handle_exceptions
1282
- # @see http://httpstatus.es/500
1283
- #
1284
- # @example Enabled (default)
1285
- # require 'hanami'
1286
- #
1287
- # module Bookshelf
1288
- # class Application < Hanami::Application
1289
- # configure do
1290
- # routes do
1291
- # get '/error', to: 'error#index'
1292
- # end
1293
- # end
1294
- #
1295
- # load!
1296
- # end
1297
- #
1298
- # module Controllers::Error
1299
- # class Index
1300
- # include Bookshelf::Action
1301
- #
1302
- # def call(params)
1303
- # raise ArgumentError
1304
- # end
1305
- # end
1306
- # end
1307
- # end
1308
- #
1309
- # # GET '/error' # => 500 - Internal Server Error
1310
- #
1311
- # @example Disabled
1312
- # require 'hanami'
1313
- #
1314
- # module Bookshelf
1315
- # class Application < Hanami::Application
1316
- # configure do
1317
- # handle_exceptions false
1318
- #
1319
- # routes do
1320
- # get '/error', to: 'error#index'
1321
- # end
1322
- # end
1323
- #
1324
- # load!
1325
- # end
1326
- #
1327
- # module Controllers::Error
1328
- # class Index
1329
- # include Bookshelf::Action
1330
- #
1331
- # def call(params)
1332
- # raise ArgumentError
1333
- # end
1334
- # end
1335
- # end
1336
- # end
1337
- #
1338
- # # GET '/error' # => raises ArgumentError
1339
- def handle_exceptions(value = nil)
1340
- if value.nil?
1341
- @handle_exceptions
1342
- else
1343
- @handle_exceptions = value
1344
- end
1345
- end
1346
-
1347
- # It lazily collects all the low level settings for Hanami::Model's
1348
- # configuration and applies them when the application is loaded.
1349
- #
1350
- # NOTE: This forwards all the configurations to Hanami::Model, without
1351
- # checking them. Before to use this feature, please have a look at the
1352
- # current Hanami::Model version installed.
1353
- #
1354
- # NOTE: This may override some configurations of your application.
1355
- #
1356
- # @return [Hanami::Config::FrameworkConfiguration] the configuration
1357
- #
1358
- # @since 0.2.0
1359
- #
1360
- # @see http://www.rubydoc.info/gems/hanami-model/Hanami/Model/Configuration
1361
- #
1362
- # @example Define a setting
1363
- # require 'hanami'
1364
- # require 'hanami/model'
1365
- #
1366
- # module Bookshelf
1367
- # class Application < Hanami::Application
1368
- # configure do
1369
- # model.adapter :sql, 'sqlite://db/bookshelf_development'
1370
- # end
1371
- # end
1372
- # end
1373
- #
1374
- # @example Override a setting
1375
- # require 'hanami'
1376
- # require 'hanami/model'
1377
- #
1378
- # module Bookshelf
1379
- # class Application < Hanami::Application
1380
- # configure do
1381
- # adapter :sql, 'postgresql://localhost/database'
1382
- # model.adapter :sql, 'sqlite://db/bookshelf_development'
1383
- # end
1384
- # end
1385
- # end
1386
- #
1387
- # # The sqlite adapter will override the SQL one
1388
- def model
1389
- @model ||= Config::FrameworkConfiguration.new
1390
- end
1391
-
1392
- # It lazily collects all the low level settings for Hanami::Controller's
1393
- # configuration and applies them when the application is loaded.
1394
- #
1395
- # NOTE: This forwards all the configurations to Hanami::Controller, without
1396
- # checking them. Before to use this feature, please have a look at the
1397
- # current Hanami::Controller version installed.
1398
- #
1399
- # NOTE: This may override some configurations of your application.
1400
- #
1401
- # @return [Hanami::Config::FrameworkConfiguration] the configuration
1402
- #
1403
- # @since 0.2.0
1404
- #
1405
- # @see http://www.rubydoc.info/gems/hanami-controller/Hanami/Controller/Configuration
1406
- #
1407
- # @example Define a setting
1408
- # require 'hanami'
1409
- #
1410
- # module Bookshelf
1411
- # class Application < Hanami::Application
1412
- # configure do
1413
- # controller.default_request_format :json
1414
- # end
1415
- # end
1416
- # end
1417
- #
1418
- # @example Override a setting
1419
- # require 'hanami'
1420
- #
1421
- # module Bookshelf
1422
- # class Application < Hanami::Application
1423
- # configure do
1424
- # handle_exceptions false
1425
- # controller.handle_exceptions true
1426
- # end
1427
- # end
1428
- # end
1429
- #
1430
- # # Exceptions will be handled
1431
- def controller
1432
- @controller ||= Config::FrameworkConfiguration.new
1433
- end
1434
-
1435
- # It lazily collects all the low level settings for Hanami::View's
1436
- # configuration and applies them when the application is loaded.
1437
- #
1438
- # NOTE: This forwards all the configurations to Hanami::View, without
1439
- # checking them. Before to use this feature, please have a look at the
1440
- # current Hanami::View version installed.
1441
- #
1442
- # NOTE: This may override some configurations of your application.
1443
- #
1444
- # @return [Hanami::Config::FrameworkConfiguration] the configuration
1445
- #
1446
- # @since 0.2.0
1447
- #
1448
- # @see http://www.rubydoc.info/gems/hanami-view/Hanami/View/Configuration
1449
- #
1450
- # @example Define a setting
1451
- # require 'hanami'
1452
- #
1453
- # module Bookshelf
1454
- # class Application < Hanami::Application
1455
- # configure do
1456
- # view.layout :application
1457
- # end
1458
- # end
1459
- # end
1460
- #
1461
- # @example Override a setting
1462
- # require 'hanami'
1463
- #
1464
- # module Bookshelf
1465
- # class Application < Hanami::Application
1466
- # configure do
1467
- # layout :application
1468
- # view.layout :backend
1469
- # end
1470
- # end
1471
- # end
1472
- #
1473
- # # It will use `:backend` layout
1474
- def view
1475
- @view ||= Config::FrameworkConfiguration.new
1476
- end
1477
-
1478
- # @since 0.9.0
1479
- # @api private
1480
- def app_name
1481
- ApplicationName.new(namespace)
1482
- end
1483
-
1484
- private
1485
-
1486
- # @api private
1487
- attr_reader :configurations
1488
-
1489
- # @since 0.2.0
1490
- # @api private
1491
- def evaluate_configurations!
1492
- configurations.each(@env.environment) { |c| instance_eval(&c) }
1493
- end
1494
- end
1495
- end