Skip to content

Commit

Permalink
Merge pull request #11 from airbnb/add-reset_current-method
Browse files Browse the repository at this point in the history
Adds Trebuchet.reset_current!, consolidate controller trebuchets
  • Loading branch information
spencerdemars committed Aug 5, 2014
2 parents fd119b6 + ea2acb4 commit 4241b02
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
7 changes: 6 additions & 1 deletion lib/trebuchet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class << self
attr_accessor :admin_view, :admin_edit
attr_accessor :time_zone
attr_accessor :exception_handler
attr_accessor :current_block

def backend
self.backend = :memory unless @backend
Expand Down Expand Up @@ -40,10 +41,14 @@ def log(feature_name, result)
attr_accessor :current

def current
@current = @current.call if @current.is_a?(Proc)
@current ||= @current_block.call if @current_block.respond_to?(:call)
@current || new(nil) # return an blank Trebuchet instance if @current is not set
end

def reset_current!
@current = nil
end

end

def self.aim(feature_name, *args)
Expand Down
2 changes: 1 addition & 1 deletion lib/trebuchet/action_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def self.included(base)
end

def trebuchet
@trebuchet ||= Trebuchet.new(current_user, request)
Trebuchet.current
end

end
13 changes: 8 additions & 5 deletions lib/trebuchet/action_controller_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ class Trebuchet::ActionControllerFilter

def self.before(controller)
Trebuchet.initialize_logs

if defined?(Trebuchet::Backend::RedisCached) && Trebuchet.backend.is_a?(Trebuchet::Backend::RedisCached) && Trebuchet.backend.respond_to?(:clear_cached_strategies)
if Time.now > Trebuchet.backend.cache_cleared_at + 60.seconds
Trebuchet.backend.clear_cached_strategies
Trebuchet.backend.clear_cached_strategies
end
end

Trebuchet.current = Proc.new { Trebuchet.new controller.send(:current_user), controller.request }

Trebuchet.current_block = Proc.new {
Trebuchet.new(controller.send(:current_user), controller.request)
}
end

def self.after(controller)
Trebuchet.current = nil # very important
Trebuchet.current_block = nil
Trebuchet.reset_current! # very important
end

end
6 changes: 3 additions & 3 deletions lib/trebuchet/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Trebuchet
VERSION = "0.6.6"

VERSION = "0.7.0"

end

0 comments on commit 4241b02

Please sign in to comment.