Skip to content
This repository has been archived by the owner on Jan 1, 2024. It is now read-only.

Commit

Permalink
Add Autoconnect singleton and determine Playground connection with it.
Browse files Browse the repository at this point in the history
* Refactor Playground#initialize to use #autoconnect.
* Closes #81.
  • Loading branch information
phillbaker committed Oct 10, 2013
1 parent 817a21e commit 2a37b07
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 15 deletions.
1 change: 1 addition & 0 deletions lib/vanity.rb
Expand Up @@ -31,6 +31,7 @@ module Vanity
require "vanity/adapters/mock_adapter"
# Playground.
require "vanity/playground"
require "vanity/autoconnect"
require "vanity/helpers"
# Integration with various frameworks.
require "vanity/frameworks"
61 changes: 61 additions & 0 deletions lib/vanity/autoconnect.rb
@@ -0,0 +1,61 @@
module Vanity
# A singleton responsible for determining if the playground should connect
# to the datastore.
module Autoconnect

BLACKLISTED_RAILS_RAKE_TASKS = [
'about',
'assets:clean',
'assets:clobber',
'assets:environment',
'assets:precompile',
'assets:precompile:all',
'db:create',
'db:drop',
'db:fixtures:load',
'db:migrate',
'db:migrate:status',
'db:rollback',
'db:schema:cache:clear',
'db:schema:cache:dump',
'db:schema:dump',
'db:schema:load',
'db:seed',
'db:setup',
'db:structure:dump',
'db:version',
'doc:app',
'log:clear',
'middleware',
'notes',
'notes:custom',
'rails:template',
'rails:update',
'routes',
'secret',
'stats',
'time:zones:all',
'tmp:clear',
'tmp:create'
]
ENVIRONMENT_VANITY_DISABLED_FLAG = "VANITY_DISABLED"

def self.playground_should_autoconnect?
!environment_disabled? && !in_blacklisted_rake_task?
end

def self.environment_disabled?
!!ENV[ENVIRONMENT_VANITY_DISABLED_FLAG]
end

def self.in_blacklisted_rake_task?
!(current_rake_tasks & BLACKLISTED_RAILS_RAKE_TASKS).empty?
end

def self.current_rake_tasks
::Rake.application.top_level_tasks
rescue => e
[]
end
end
end
39 changes: 24 additions & 15 deletions lib/vanity/playground.rb
Expand Up @@ -21,6 +21,7 @@ class Playground
# - namespace -- Namespace to use
# - load_path -- Path to load experiments/metrics from
# - logger -- Logger to use
# - redis -- A Redis object that will be used for the connection
def initialize(*args)
options = Hash === args.last ? args.pop : {}
# In the case of Rails, use the Rails logger and collect only for
Expand All @@ -39,31 +40,22 @@ def initialize(*args)
end

@options = defaults.merge(config).merge(options)
if @options[:host] == 'redis' && @options.values_at(:host, :port, :db).any?
warn "Deprecated: please specify Redis connection as URL (\"redis://host:port/db\")"
establish_connection :adapter=>"redis", :host=>@options[:host], :port=>@options[:port], :database=>@options[:db] || @options[:database]
elsif @options[:redis]
@adapter = RedisAdapter.new(:redis=>@options[:redis])
else
connection_spec = args.shift || @options[:connection]
if connection_spec
connection_spec = "redis://" + connection_spec unless connection_spec[/^\w+:/]
establish_connection connection_spec
end
end

warn "Deprecated: namespace option no longer supported directly" if @options[:namespace]
@load_path = @options[:load_path] || DEFAULTS[:load_path]
unless @logger = @options[:logger]
@logger = Logger.new(STDOUT)
@logger.level = Logger::ERROR
end

autoconnect(@options, args) if Vanity::Autoconnect.playground_should_autoconnect?

@loading = []
@use_js = false
self.add_participant_path = DEFAULT_ADD_PARTICIPANT_PATH
@collecting = !!@options[:collecting]
end

# Deprecated. Use redis.server instead.
attr_accessor :host, :port, :db, :password, :namespace

Expand Down Expand Up @@ -219,7 +211,7 @@ def track!(id, count = 1)


# -- Connection management --

# This is the preferred way to programmatically create a new connection (or
# switch to a new connection). If no connection was established, the
# playground will create a new one by calling this method with no arguments.
Expand All @@ -242,7 +234,7 @@ def track!(id, count = 1)
# Vanity.playground.establish_connection :adapter=>:redis,
# :host=>"redis.local"
#
# @since 1.4.0
# @since 1.4.0
def establish_connection(spec = nil)
@spec = spec
disconnect! if @adapter
Expand Down Expand Up @@ -343,6 +335,23 @@ def redis
connection
end

protected

def autoconnect(options, arguments)
if options[:host] == 'redis' && options.values_at(:host, :port, :db).any?
warn "Deprecated: please specify Redis connection as URL (\"redis://host:port/db\")"
establish_connection :adapter=>"redis", :host=>options[:host], :port=>options[:port], :database=>options[:db] || options[:database]
elsif options[:redis]
@adapter = RedisAdapter.new(:redis=>options[:redis])
else
connection_spec = arguments.shift || options[:connection]
if connection_spec
connection_spec = "redis://" + connection_spec unless connection_spec[/^\w+:/]
establish_connection connection_spec
end
end
end

end

# In the case of Rails, use the Rails logger and collect only for
Expand Down
25 changes: 25 additions & 0 deletions test/autoconnect_test.rb
@@ -0,0 +1,25 @@
require "test/test_helper"

context ".playground_should_autoconnect?" do

setup do
end

test "returns true by default" do
autoconnect = Vanity::Autoconnect.playground_should_autoconnect?
assert autoconnect == true
end

test "returns false if environment flag is set" do
ENV['VANITY_DISABLED'] = '1'
autoconnect = Vanity::Autoconnect.playground_should_autoconnect?
assert autoconnect == false
ENV['VANITY_DISABLED'] = nil
end

test "returns false if in assets:precompile rake task" do
Rake.expects(:application).returns(stub(:top_level_tasks => ['assets:precompile']))
autoconnect = Vanity::Autoconnect.playground_should_autoconnect?
assert autoconnect == false
end
end
11 changes: 11 additions & 0 deletions test/playground_test.rb
Expand Up @@ -23,4 +23,15 @@ def test_reconnects_with_existing_connection
assert_equal Vanity.playground.connection.to_s, "mock:/"
end

def test_autoconnect_establishes_connection_by_default
instance = Vanity.playground
assert instance.connected?
end

def test_autoconnect_can_skip_connection
Vanity::Autoconnect.stubs(:playground_should_autoconnect?).returns(false)
instance = Vanity::Playground.new
assert !instance.connected?
end

end

0 comments on commit 2a37b07

Please sign in to comment.