Skip to content

Commit

Permalink
Merge pull request #28 from phallguy/phallguy/fix-scorpion-coordinati…
Browse files Browse the repository at this point in the history
…on-rack-controllers

Fixes coordination of rack & controller management of request scorpion
  • Loading branch information
phallguy authored Dec 7, 2018
2 parents 8abaeb1 + 60a8c44 commit 0bc9c11
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
scorpion-ioc (1.0.3)
scorpion-ioc (1.0.4)
rails (>= 4.0)

GEM
Expand Down Expand Up @@ -228,4 +228,4 @@ DEPENDENCIES
yard

BUNDLED WITH
1.16.1
1.16.3
16 changes: 13 additions & 3 deletions lib/scorpion/rack/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,23 @@ class Middleware

def initialize( app, nest = nil )
@app = app
@nest = nest || Scorpion.instance.build_nest
@nest = nest
end

def call( env )
env[ENV_KEY] = prepare_scorpion( nest.conceive, env )
# If we don't have a nest yet, build one from the configured global
# scorpion.
@nest ||= Scorpion.instance.build_nest

conceived = false
env[ENV_KEY] ||=
begin
conceived = true
prepare_scorpion( nest.conceive, env )
end

@app.call(env).tap do
free_scorpion( env )
free_scorpion( env ) if conceived
end
end

Expand Down
3 changes: 2 additions & 1 deletion lib/scorpion/rails/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module Controller

ENV_KEY = "scorpion.instance".freeze


# Fetch an object from the controller's {#scorpion}.
# @see Scorpion#fetch
def fetch( *args, &block )
Expand Down Expand Up @@ -71,6 +70,8 @@ def assign_scorpion( scorpion )
end

def free_scorpion
return unless conceived_scorpion?

scorpion.try( :destroy )
request.env.delete ENV_KEY
end
Expand Down
5 changes: 5 additions & 0 deletions lib/scorpion/rails/nest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,14 @@ def with_scorpion( &block )
end

def conceive_scorpion
@conceived_scorpion = true
nest.conceive
end

def conceived_scorpion?
!!@conceived_scorpion
end

def append_instance_hunts( scorpion )
scorpion.prepare do |hunter|
self.class.instance_hunts.each do |method, args, block|
Expand Down
2 changes: 1 addition & 1 deletion lib/scorpion/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Scorpion
VERSION_NUMBER = "1.0.3".freeze
VERSION_NUMBER = "1.0.4".freeze
VERSION_SUFFIX = "".freeze
VERSION = "#{ VERSION_NUMBER }#{ VERSION_SUFFIX }".freeze
end

0 comments on commit 0bc9c11

Please sign in to comment.