Skip to content

Commit

Permalink
Project initializers
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Nov 11, 2016
1 parent 45a244d commit d87ae78
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 11 deletions.
6 changes: 0 additions & 6 deletions lib/hanami/commands/server.rb
Expand Up @@ -9,12 +9,6 @@ module Commands
class Server < Command
requires 'code_reloading'

# Message text when Shotgun enabled but interpreter does not support `fork`
#
# @since 0.8.0
# @api private
WARNING_MESSAGE = 'Your platform doesn\'t support code reloading.'.freeze

def initialize(options)
super(options)

Expand Down
26 changes: 25 additions & 1 deletion lib/hanami/components/components.rb
@@ -1,3 +1,5 @@
require 'hanami/utils'

module Hanami
# Registered components
#
Expand All @@ -11,7 +13,7 @@ module Components # rubocop:disable Metrics/ModuleLength
# @since x.x.x
# @api private
register 'all' do
requires 'model', 'apps'
requires 'model', 'apps', 'finalizers'

resolve { true }
end
Expand Down Expand Up @@ -201,6 +203,28 @@ module Components # rubocop:disable Metrics/ModuleLength
end
end

# Finalizers for the project
#
# @since x.x.x
# @api private
register 'finalizers' do
requires 'finalizers.initializers'

resolve { true }
end

# Load project initializers
#
# @since x.x.x
# @api private
register 'finalizers.initializers' do
run do
Hanami::Utils.require!(
Hanami.root.join('config', 'initializers')
)
end
end

# Configure, load and finalize a Hanami application in the project
#
# @since x.x.x
Expand Down
8 changes: 4 additions & 4 deletions lib/hanami/server.rb
Expand Up @@ -46,14 +46,14 @@ def start

def setup
if code_reloading?
@app = Shotgun::Loader.new(rackup, &model)
@app = Shotgun::Loader.new(rackup, &reloadable)
else
model.call
reloadable.call
end
end

def model
->(*) { Hanami::Components.resolve('model') }
def reloadable
->(*) { Hanami::Components.resolve('model', 'finalizers') }
end

def environment
Expand Down
38 changes: 38 additions & 0 deletions spec/integration/project_initializers_spec.rb
@@ -0,0 +1,38 @@
RSpec.describe "Project initializers", type: :cli do
it "mounts Rack middleware" do
with_project("project_initializers", gems: ['i18n']) do
write "config/locales/en.yml", <<-EOF
en:
greeting: "Welcome stranger"
EOF

write "config/initializers/i18n.rb", <<-EOF
require 'i18n'
I18n.load_path = Dir['config/locales/*.yml']
I18n.backend.load_translations
EOF

generate "action web home#index --url=/"
rewrite "apps/web/views/home/index.rb", <<-EOF
module Web::Views::Home
class Index
include Web::View
def greeting
I18n.t(:greeting)
end
end
end
EOF

rewrite "apps/web/templates/home/index.html.erb", <<-EOF
<h1><%= greeting%></h1>
EOF
server do
get '/'

expect(last_response.body).to include("Welcome stranger")
end
end
end
end
2 changes: 2 additions & 0 deletions spec/isolation/components/all/component_spec.rb
Expand Up @@ -14,6 +14,8 @@

expect(Hanami::Components['web']).to_not be(nil)
expect(Hanami::Components['admin']).to_not be(nil)

expect(Hanami::Components['finalizers']).to_not be(nil)
end
end
end
10 changes: 10 additions & 0 deletions spec/isolation/components/finalizers/component_spec.rb
@@ -0,0 +1,10 @@
RSpec.describe "Components: finalizers", type: :cli do
it "finalizes project" do
with_project do
require Pathname.new(Dir.pwd).join("config", "environment")
Hanami::Components.resolve('finalizers')

expect(Hanami::Components['finalizers']).to be(true)
end
end
end
32 changes: 32 additions & 0 deletions spec/isolation/components/finalizers/ensure_once_spec.rb
@@ -0,0 +1,32 @@
RSpec.describe "Components: finalizers", type: :cli do
it "ensures to load components once" do
with_project do
write "config/initializers/counter.rb", <<-EOF
class Counter
@counter = 0
def self.counter
@counter
end
def self.increment!
@counter += 1
end
end
Counter.increment!
EOF

require Pathname.new(Dir.pwd).join("config", "environment")
Hanami::Components.resolve('finalizers')

counter = Counter.counter

# Simulate accidental double trigger
Hanami::Components.resolve('finalizers')

# counter shouldn't have been changed
expect(Counter.counter).to eq(counter)
end
end
end

0 comments on commit d87ae78

Please sign in to comment.