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
@@ -1,202 +1,366 @@
1
- require 'thread'
2
- require 'concurrent'
3
- require 'hanami/application_name'
4
- require 'hanami/application_namespace'
5
- require 'hanami/application_configuration'
6
- require 'hanami/environment_application_configurations'
7
- require 'hanami/rendering_policy'
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/system/container"
4
+ require "hanami/configuration"
5
+ require "pathname"
6
+ require "rack"
7
+ require_relative "slice"
8
+ require_relative "application/autoloader/inflector_adapter"
9
+ require_relative "application/settings"
8
10
 
9
11
  module Hanami
10
- # A full stack Hanami application
11
- #
12
- # @since 0.1.0
13
- #
14
- # @example
15
- # require 'hanami'
12
+ # Hanami application class
16
13
  #
17
- # module Bookshelf
18
- # class Application < Hanami::Application
19
- # end
20
- # end
14
+ # @since 2.0.0
21
15
  class Application
22
- # Override Ruby's Class#inherited
23
- #
24
- # @since 0.2.0
25
- # @api private
26
- #
27
- # @see http://www.ruby-doc.org/core/Class.html#method-i-inherited
28
- def self.inherited(base)
29
- super
30
-
31
- base.extend(ClassMethods)
32
- base.namespace.module_eval do
33
- class << self
34
- # Routes for this application
35
- #
36
- # @return [Hanami::Routes] the routes for this Hanami application
37
- #
38
- # @since 0.9.0
39
- # @api public
40
- #
41
- # @example
42
- #
43
- # Web.routes
44
- # Admin.routes
45
- attr_accessor :routes
16
+ @_mutex = Mutex.new
17
+
18
+ class << self
19
+ def inherited(klass)
20
+ @_mutex.synchronize do
21
+ klass.class_eval do
22
+ @_mutex = Mutex.new
23
+ @_configuration = Hanami::Configuration.new(env: Hanami.env)
24
+
25
+ extend ClassMethods
26
+ include InstanceMethods
27
+ end
28
+
29
+ klass.send :prepare_base_load_path
30
+
31
+ Hanami.application = klass
46
32
  end
47
33
  end
48
34
  end
49
35
 
50
- # Class interface for Hanami applications
36
+ # Application class interface
51
37
  #
52
- # @since 0.9.0
53
- # @api private
38
+ # rubocop:disable Metrics/ModuleLength
54
39
  module ClassMethods
55
- # Override Ruby's Class#extended
56
- #
57
- # @since 0.9.0
58
- # @api private
59
- #
60
- # @see http://www.ruby-doc.org/core/Class.html#method-i-extended
61
- def self.extended(base) # rubocop:disable Metrics/MethodLength
62
- super
63
-
64
- base.class_eval do
65
- @namespace = ApplicationNamespace.resolve(name)
66
- @configurations = EnvironmentApplicationConfigurations.new
67
- @_lock = Mutex.new
68
-
69
- class << self
70
- # @since 0.9.0
71
- # @api private
72
- attr_reader :namespace
73
-
74
- # @since 0.9.0
75
- # @api private
76
- attr_reader :configurations
77
-
78
- # @since 0.9.0
79
- # @api private
80
- attr_reader :configuration
40
+ def self.extended(klass)
41
+ klass.class_eval do
42
+ @inited = @booted = false
43
+ end
44
+ end
45
+
46
+ def configuration
47
+ @_configuration
48
+ end
49
+
50
+ alias config configuration
51
+
52
+ def init # rubocop:disable Metrics/MethodLength
53
+ return self if inited?
54
+
55
+ configuration.finalize
56
+
57
+ load_settings
58
+
59
+ @container = prepare_container
60
+ @deps_module = prepare_deps_module
61
+
62
+ load_slices
63
+ slices.values.each(&:init)
64
+ slices.freeze
65
+
66
+ if configuration.autoloader
67
+ configuration.autoloader.inflector = Autoloader::InflectorAdapter.new(inflector)
68
+ configuration.autoloader.setup
69
+ end
70
+
71
+ load_routes
72
+
73
+ @inited = true
74
+ self
75
+ end
76
+
77
+ def inited?
78
+ @inited
79
+ end
80
+
81
+ def container
82
+ raise "Application not init'ed" unless defined?(@container)
83
+
84
+ @container
85
+ end
86
+
87
+ def deps
88
+ raise "Application not init'ed" unless defined?(@deps_module)
89
+
90
+ @deps_module
91
+ end
92
+
93
+ def slices
94
+ @slices ||= {}
95
+ end
96
+
97
+ def register_slice(name, **slice_args)
98
+ raise "Slice +#{name}+ already registered" if slices.key?(name.to_sym)
99
+
100
+ slice = Slice.new(self, name: name, **slice_args)
101
+ slice.namespace.const_set :Slice, slice if slice.namespace # rubocop:disable Style/SafeNavigation
102
+ slices[name.to_sym] = slice
103
+ end
104
+
105
+ def register(*args, **opts, &block)
106
+ container.register(*args, **opts, &block)
107
+ end
108
+
109
+ def register_bootable(*args, **opts, &block)
110
+ container.boot(*args, **opts, &block)
111
+ end
112
+
113
+ def init_bootable(*args)
114
+ container.init(*args)
115
+ end
116
+
117
+ def start_bootable(*args)
118
+ container.start(*args)
119
+ end
120
+
121
+ def key?(*args)
122
+ container.key?(*args)
123
+ end
124
+
125
+ def keys
126
+ container.keys
127
+ end
128
+
129
+ def [](*args)
130
+ container[*args]
131
+ end
132
+
133
+ def resolve(*args)
134
+ container.resolve(*args)
135
+ end
136
+
137
+ def boot(&block)
138
+ return self if booted?
139
+
140
+ init
141
+
142
+ container.finalize!(&block)
143
+
144
+ slices.values.each(&:boot)
145
+
146
+ @booted = true
147
+ self
148
+ end
149
+
150
+ def booted?
151
+ @booted
152
+ end
153
+
154
+ def settings(&block) # rubocop:disable Metrics/MethodLength
155
+ if block
156
+ @_settings = Application::Settings.build(
157
+ configuration.settings_loader,
158
+ configuration.settings_loader_options,
159
+ &block
160
+ )
161
+ elsif instance_variable_defined?(:@_settings)
162
+ @_settings
163
+ else
164
+ # Load settings lazily so they can be used to configure the
165
+ # Hanami::Application subclass (before the application has inited)
166
+ load_settings
167
+ @_settings ||= nil
168
+ end
169
+ end
170
+
171
+ def routes(&block)
172
+ @_mutex.synchronize do
173
+ if block.nil?
174
+ raise "Hanami.application.routes not configured" unless defined?(@_routes)
175
+
176
+ @_routes
177
+ else
178
+ @_routes = block
81
179
  end
82
180
  end
83
181
  end
84
182
 
85
- # Hanami application name
86
- #
87
- # @return [String] the Hanami application name
88
- #
89
- # @since 0.9.0
90
- # @api private
91
- #
92
- # @example
93
- # require 'hanami'
94
- #
95
- # module Web
96
- # class Application < Hanami::Application
97
- # end
98
- # end
99
- #
100
- # Web::Application.app_name # => "web"
101
- def app_name
102
- ApplicationName.new(name).to_s
103
- end
104
-
105
- # Set configuration
106
- #
107
- # @param configuration [Hanami::ApplicationConfiguration] the application configuration
108
- #
109
- # @raise [RuntimeError] if the configuration is assigned more than once
110
- #
111
- # @since 0.1.0
183
+ MODULE_DELIMITER = "::"
184
+ private_constant :MODULE_DELIMITER
185
+
186
+ def namespace
187
+ inflector.constantize(name.split(MODULE_DELIMITER)[0..-2].join(MODULE_DELIMITER))
188
+ end
189
+
190
+ def namespace_path
191
+ inflector.underscore(namespace)
192
+ end
193
+
194
+ def application_name
195
+ inflector.underscore(namespace).to_sym
196
+ end
197
+
198
+ def root
199
+ configuration.root
200
+ end
201
+
202
+ def inflector
203
+ configuration.inflector
204
+ end
205
+
112
206
  # @api private
113
- def configuration=(configuration)
114
- @_lock.synchronize do
115
- # raise "Can't assign configuration more than once (#{app_name})" unless @configuration.nil?
116
- @configuration = configuration
207
+ def component_provider(component)
208
+ raise "Hanami.application must be inited before detecting providers" unless inited?
209
+
210
+ # [Admin, Main, MyApp] or [MyApp::Admin, MyApp::Main, MyApp]
211
+ providers = slices.values + [self]
212
+
213
+ component_class = component.is_a?(Class) ? component : component.class
214
+ component_name = component_class.name
215
+
216
+ return unless component_name
217
+
218
+ providers.detect { |provider| component_name.include?(provider.namespace.to_s) }
219
+ end
220
+
221
+ private
222
+
223
+ def prepare_base_load_path
224
+ base_path = File.join(root, "lib")
225
+ $LOAD_PATH.unshift base_path unless $LOAD_PATH.include?(base_path)
226
+ end
227
+
228
+ def prepare_container
229
+ define_container.tap do |container|
230
+ configure_container container
117
231
  end
118
232
  end
119
233
 
120
- # Configure the application.
121
- # It yields the given block in the context of the configuration
122
- #
123
- # @param environment [Symbol,nil] the configuration environment name
124
- # @param blk [Proc] the configuration block
125
- #
126
- # @since 0.1.0
127
- #
128
- # @see Hanami::ApplicationConfiguration
129
- #
130
- # @example
131
- # require 'hanami'
132
- #
133
- # module Bookshelf
134
- # Application < Hanami::Application
135
- # configure do
136
- # # ...
137
- # end
138
- # end
139
- # end
140
- def configure(environment = nil, &blk)
141
- configurations.add(environment, &blk)
234
+ def prepare_deps_module
235
+ define_deps_module
142
236
  end
143
- end
144
237
 
145
- # Initialize and load a new instance of the application
146
- #
147
- # @return [Hanami::Application] a new instance of the application
148
- #
149
- # @since 0.1.0
150
- # @api private
151
- def initialize
152
- @renderer = RenderingPolicy.new(configuration)
153
- @middleware = configuration.middleware
154
- end
238
+ def define_container
239
+ require "#{application_name}/container"
240
+ namespace.const_get :Container
241
+ rescue LoadError, NameError
242
+ namespace.const_set :Container, Class.new(Dry::System::Container)
243
+ end
155
244
 
156
- # Process a request.
157
- # This method makes Hanami applications compatible with the Rack protocol.
158
- #
159
- # @param env [Hash] a Rack env
160
- #
161
- # @return [Array] a serialized Rack response
162
- #
163
- # @since 0.1.0
164
- #
165
- # @see http://rack.github.io
166
- # @see Hanami::RenderingPolicy#render
167
- # @see Hanami::Application#middleware
168
- def call(env)
169
- renderer.render(env, middleware.call(env))
170
- end
245
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
246
+ def configure_container(container)
247
+ container.use :env, inferrer: -> { Hanami.env }
248
+ container.use :notifications
171
249
 
172
- private
250
+ container.configure do |config|
251
+ config.inflector = configuration.inflector
173
252
 
174
- # Return the configuration for this application
175
- #
176
- # @since 0.1.0
177
- # @api private
178
- #
179
- # @see Hanami::Application.configuration
180
- def configuration
181
- self.class.configuration
253
+ config.root = configuration.root
254
+ config.bootable_dirs = [
255
+ "config/boot",
256
+ Pathname(__dir__).join("application/container/boot").realpath,
257
+ ]
258
+
259
+ if configuration.autoloader
260
+ require "dry/system/loader/autoloading"
261
+ config.component_dirs.loader = Dry::System::Loader::Autoloading
262
+ config.component_dirs.add_to_load_path = false
263
+ end
264
+
265
+ if root.join("lib").directory?
266
+ config.component_dirs.add "lib" do |dir|
267
+ dir.default_namespace = application_name.to_s
268
+ end
269
+
270
+ configuration.autoloader&.push_dir(root.join("lib"))
271
+ end
272
+ end
273
+
274
+ container
275
+ end
276
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
277
+
278
+ def define_deps_module
279
+ require "#{application_name}/deps"
280
+ namespace.const_get :Deps
281
+ rescue LoadError, NameError
282
+ namespace.const_set :Deps, container.injector
283
+ end
284
+
285
+ def load_slices
286
+ Dir[File.join(slices_path, "*")]
287
+ .select(&File.method(:directory?))
288
+ .each(&method(:load_slice))
289
+ end
290
+
291
+ def slices_path
292
+ File.join(root, config.slices_dir)
293
+ end
294
+
295
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
296
+ def load_slice(slice_path)
297
+ slice_path = Pathname(slice_path)
298
+
299
+ slice_name = slice_path.relative_path_from(Pathname(slices_path)).to_s
300
+ slice_const_name = inflector.camelize(slice_name)
301
+
302
+ if config.slices_namespace.const_defined?(slice_const_name)
303
+ slice_module = config.slices_namespace.const_get(slice_const_name)
304
+
305
+ raise "Cannot use slice +#{slice_const_name}+ since it is not a module" unless slice_module.is_a?(Module)
306
+ else
307
+ slice_module = Module.new
308
+ config.slices_namespace.const_set inflector.camelize(slice_name), slice_module
309
+ end
310
+
311
+ register_slice(
312
+ slice_name,
313
+ namespace: slice_module,
314
+ root: slice_path.realpath
315
+ )
316
+ end
317
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
318
+
319
+ def load_routes
320
+ require File.join(configuration.root, configuration.router.routes)
321
+ rescue LoadError # rubocop:disable Lint/SuppressedException
322
+ end
323
+
324
+ def load_settings
325
+ prepare_base_load_path
326
+ require File.join(configuration.root, configuration.settings_path)
327
+ rescue LoadError # rubocop:disable Lint/SuppressedException
328
+ end
182
329
  end
330
+ # rubocop:enable Metrics/ModuleLength
183
331
 
184
- # Rendering policy
185
- #
186
- # @since 0.2.0
187
- # @api private
188
- #
189
- # @see Hanami::RenderingPolicy
190
- attr_reader :renderer
332
+ # Application instance interface
333
+ module InstanceMethods
334
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
335
+ def initialize(application = self.class)
336
+ require_relative "application/router"
191
337
 
192
- # Rack middleware stack
193
- #
194
- # @return [Hanami::MiddlewareStack] the middleware stack
195
- #
196
- # @since 0.1.0
197
- # @api private
198
- #
199
- # @see Hanami::MiddlewareStack
200
- attr_reader :middleware
338
+ application.boot
339
+
340
+ resolver = application.config.router.resolver.new(
341
+ slices: application.slices,
342
+ inflector: application.inflector
343
+ )
344
+
345
+ router = Application::Router.new(
346
+ routes: application.routes,
347
+ resolver: resolver,
348
+ **application.configuration.router.options,
349
+ ) do
350
+ use application[:rack_monitor]
351
+
352
+ application.config.for_each_middleware do |m, *args, &block|
353
+ use(m, *args, &block)
354
+ end
355
+ end
356
+
357
+ @app = router.to_rack_app
358
+ end
359
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
360
+
361
+ def call(env)
362
+ @app.call(env)
363
+ end
364
+ end
201
365
  end
202
366
  end