Class: Blackbeard::Context

Inherits:
Object
  • Object
show all
Includes:
ConfigurationMethods
Defined in:
lib/blackbeard/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ConfigurationMethods

#config, #db, #guest_method, included, #tz

Constructor Details

#initialize(pirate, user, controller = nil) ⇒ Context

Returns a new instance of Context.



8
9
10
11
12
13
14
15
16
# File 'lib/blackbeard/context.rb', line 8

def initialize(pirate, user, controller = nil)
  @pirate = pirate
  @controller = controller
  @user = user

  if (@user == false) || (@user && guest_method && @user.send(guest_method) == false)
    @user = nil
  end
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



6
7
8
# File 'lib/blackbeard/context.rb', line 6

def controller
  @controller
end

#userObject (readonly)

Returns the value of attribute user.



6
7
8
# File 'lib/blackbeard/context.rb', line 6

def user
  @user
end

Instance Method Details

#ab_test(id, options = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/blackbeard/context.rb', line 28

def ab_test(id, options = nil)
  test = @pirate.test(id.to_s)
  if options.is_a? Hash
    test.add_variations(options.keys)
    variation = test.select_variation
    options[variation.to_sym]
  else
    variation = test.select_variation
    SelectedVariation.new(test, variation)
  end
end

#add_total(id, amount = 1) ⇒ Object



18
19
20
21
# File 'lib/blackbeard/context.rb', line 18

def add_total(id, amount = 1)
  @pirate.metric(:total, id.to_s).add(self, amount)
  self
end

#add_unique(id) ⇒ Object



23
24
25
26
# File 'lib/blackbeard/context.rb', line 23

def add_unique(id)
  @pirate.metric(:unique, id.to_s).add(self, 1)
  self
end

#feature_active?(id) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/blackbeard/context.rb', line 40

def feature_active?(id)
  @pirate.feature(id.to_s).reload.active_for?(self)
end

#unique_identifierObject



44
45
46
# File 'lib/blackbeard/context.rb', line 44

def unique_identifier
  @user.nil? ? "b#{visitor_id}" : "a#{@user.id}"
end

#user_idObject



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

def user_id
  @user.id
end

#visitor_idObject



52
53
54
# File 'lib/blackbeard/context.rb', line 52

def visitor_id
  controller.request.cookies[:bbd] ||= generate_visitor_id
end