module Vanity

All the cool stuff happens in other places. @see Vanity::Helper @see Vanity::Rails @see Vanity::Playground @see Vanity::Metric @see Vanity::Experiment @see Vanity::Configuration @see Vanity::Connection

Run time configuration and helpers

Constants

VERSION

Attributes

configuration[W]

@since 2.0.0

connection[W]

@since 2.0.0

playground[W]

@since 2.0.0

Public Class Methods

configuration(set_if_needed=true) click to toggle source

Returns the current configuration.

@see Vanity::Configuration @since 2.0.0

# File lib/vanity/vanity.rb, line 11
def self.configuration(set_if_needed=true)
  if defined?(@configuration) && @configuration
    @configuration
  elsif set_if_needed
    configure!
  end
end
configure() { |configuration| ... } click to toggle source

This is the preferred way to configure Vanity.

@example

Vanity.configure do |config|
  config.use_js = true
end

@since 2.0.0

# File lib/vanity/vanity.rb, line 37
def self.configure
  yield(configuration)
end
configure!() click to toggle source

@since 2.0.0

# File lib/vanity/vanity.rb, line 20
def self.configure!
  @configuration = Configuration.new
end
connect!(spec_or_nil=nil) click to toggle source

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.

@since 2.0.0 @see Vanity::Connection

# File lib/vanity/vanity.rb, line 94
def self.connect!(spec_or_nil=nil)
  spec_or_nil ||= configuration.connection_params

  # Legacy special config variables permitted in connection spec
  update_configuration_from_connection_params(spec_or_nil)

  # Legacy redis.yml fallback
  if spec_or_nil.nil?
    redis_url = configuration.redis_url_from_file
    if redis_url
      spec_or_nil = redis_url
    end
  end

  # Legacy connection url fallback
  if configuration.connection_url
    spec_or_nil = configuration.connection_url
  end

  @connection = Connection.new(spec_or_nil)
end
connection(connect_if_needed=true) click to toggle source

Returns the current connection. Establishes new connection is necessary.

@since 2.0.0

# File lib/vanity/vanity.rb, line 80
def self.connection(connect_if_needed=true)
  if defined?(@connection) && @connection
    @connection
  elsif connect_if_needed
    connect!
  end
end
context() click to toggle source

Returns the Vanity context. For example, when using Rails this would be the current controller, which can be used to get/set the vanity identity.

# File lib/vanity/vanity.rb, line 48
def self.context
  Thread.current[:vanity_context]
end
context=(context) click to toggle source

Sets the Vanity context. For example, when using Rails this would be set by the set_vanity_context before filter (via Vanity::Rails#use_vanity).

# File lib/vanity/vanity.rb, line 54
def self.context=(context)
  Thread.current[:vanity_context] = context

  if context
    context.class.send(:define_method, :vanity_add_to_active_experiments) do |name, alternative|
      @_vanity_experiments ||= {}
      @_vanity_experiments[name] ||= alternative
      @_vanity_experiments[name].value
    end
    context.class.send(:alias_method, :vanity_store_experiment_for_js, :vanity_add_to_active_experiments)

    context.class.send(:define_method, :vanity_active_experiments) do
      @_vanity_experiments ||= {}
    end
  end

  context
end
disconnect!() click to toggle source

Destroys a connection

@since 2.0.0

# File lib/vanity/vanity.rb, line 119
def self.disconnect!
  if @connection
    @connection.disconnect!
    @connection = nil
  end
end
load!() click to toggle source

Loads all metrics and experiments. Called during initialization. In the case of Rails, use the Rails logger and look for templates at app/views/vanity.

@since 2.0.0

# File lib/vanity/vanity.rb, line 151
def self.load!
  @playground = Playground.new
end
logger() click to toggle source

@since 2.0.0

# File lib/vanity/vanity.rb, line 42
def self.logger
  configuration.logger
end
playground(load_if_needed=true) click to toggle source

The playground instance.

@see Vanity::Playground

# File lib/vanity/vanity.rb, line 138
def self.playground(load_if_needed=true)
  if @playground
    @playground
  elsif load_if_needed
    load!
  end
end
reconnect!() click to toggle source
# File lib/vanity/vanity.rb, line 126
def self.reconnect!
  disconnect!
  connect!
end
reload!() click to toggle source

Reloads all metrics and experiments. Rails calls this for each request in development mode.

@since 2.0.0

# File lib/vanity/vanity.rb, line 164
def self.reload!
  unload!
  load!
end
reset!() click to toggle source

@since 2.0.0

# File lib/vanity/vanity.rb, line 25
def self.reset!
  @configuration = nil
  configuration
end
template(name) click to toggle source
# File lib/vanity/templates.rb, line 36
def template(name)
  @templates ||= Templates.new
  @templates.path(name)
end
unload!() click to toggle source

@since 2.0.0

# File lib/vanity/vanity.rb, line 156
def self.unload!
  @playground = nil
end