Module: Vanity

Extended by:
Helpers
Defined in:
lib/vanity.rb,
lib/vanity/vanity.rb,
lib/vanity/helpers.rb,
lib/vanity/version.rb,
lib/vanity/adapters.rb,
lib/vanity/safe_yaml.rb,
lib/vanity/templates.rb,
lib/vanity/connection.rb,
lib/vanity/playground.rb,
lib/vanity/autoconnect.rb,
lib/vanity/metric/base.rb,
lib/vanity/commands/list.rb,
lib/vanity/configuration.rb,
lib/vanity/metric/remote.rb,
lib/vanity/commands/report.rb,
lib/vanity/experiment/base.rb,
lib/vanity/frameworks/rails.rb,
lib/vanity/experiment/ab_test.rb,
lib/vanity/metric/active_record.rb,
lib/vanity/adapters/mock_adapter.rb,
lib/vanity/experiment/definition.rb,
lib/vanity/adapters/redis_adapter.rb,
lib/vanity/experiment/alternative.rb,
lib/vanity/metric/google_analytics.rb,
lib/vanity/adapters/mongodb_adapter.rb,
lib/vanity/adapters/abstract_adapter.rb,
lib/vanity/adapters/active_record_adapter.rb,
lib/vanity/experiment/bayesian_bandit_score.rb

Overview

Run time configuration and helpers

Defined Under Namespace

Modules: Adapters, Autoconnect, Commands, Experiment, Helpers, Rails, Render, SafeYAML, Version Classes: Configuration, Connection, Metric, NoExperimentError, Playground, Templates, ViewsGenerator

Constant Summary collapse

VERSION =
"4.0.4"

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Helpers

ab_test, track!

Class Attribute Details

.configuration(set_if_needed = true) ⇒ Object

Returns the current configuration.

See Also:

Since:

  • 2.0.0



11
12
13
14
15
16
17
# 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

.connection(connect_if_needed = true) ⇒ Object

Returns the current connection. Establishes new connection is necessary.

Since:

  • 2.0.0



80
81
82
83
84
85
86
# 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

.playground(load_if_needed = true) ⇒ Object

The playground instance.

See Also:



134
135
136
137
138
139
140
# File 'lib/vanity/vanity.rb', line 134

def self.playground(load_if_needed = true)
  if @playground
    @playground
  elsif load_if_needed
    load!
  end
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

This is the preferred way to configure Vanity.

Examples:

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

Yields:

Since:

  • 2.0.0



37
38
39
# File 'lib/vanity/vanity.rb', line 37

def self.configure
  yield(configuration)
end

.configure!Object

Since:

  • 2.0.0



20
21
22
# File 'lib/vanity/vanity.rb', line 20

def self.configure!
  @configuration = Configuration.new
end

.connect!(spec_or_nil = nil) ⇒ Object

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.

See Also:

Since:

  • 2.0.0



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# 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
    spec_or_nil = redis_url if redis_url
  end

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

  @connection = Connection.new(spec_or_nil)
end

.contextObject

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.



48
49
50
# File 'lib/vanity/vanity.rb', line 48

def self.context
  Thread.current[:vanity_context]
end

.context=(context) ⇒ Object

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).



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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!Object

Destroys a connection

Since:

  • 2.0.0



115
116
117
118
119
120
# File 'lib/vanity/vanity.rb', line 115

def self.disconnect!
  if @connection # rubocop:todo Style/GuardClause
    @connection.disconnect!
    @connection = nil
  end
end

.load!Object

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



147
148
149
# File 'lib/vanity/vanity.rb', line 147

def self.load!
  @playground = Playground.new
end

.loggerObject

Since:

  • 2.0.0



42
43
44
# File 'lib/vanity/vanity.rb', line 42

def self.logger
  configuration.logger
end

.reconnect!Object



122
123
124
125
# File 'lib/vanity/vanity.rb', line 122

def self.reconnect!
  disconnect!
  connect!
end

.reload!Object

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

Since:

  • 2.0.0



160
161
162
163
# File 'lib/vanity/vanity.rb', line 160

def self.reload!
  unload!
  load!
end

.reset!Object

Since:

  • 2.0.0



25
26
27
28
# File 'lib/vanity/vanity.rb', line 25

def self.reset!
  @configuration = nil
  configuration
end

.template(name) ⇒ Object



36
37
38
39
# File 'lib/vanity/templates.rb', line 36

def template(name)
  @templates ||= Templates.new
  @templates.path(name)
end

.unload!Object

Since:

  • 2.0.0



152
153
154
# File 'lib/vanity/vanity.rb', line 152

def self.unload!
  @playground = nil
end