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
17
# File 'lib/blackbeard/context.rb', line 8

def initialize(pirate, user, controller = nil)
  # TODO: remove pirate. access cache via separate cache class
  @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



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

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_to_cohort(id, timestamp = nil, force = false) ⇒ Object



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

def add_to_cohort(id, timestamp = nil, force = false)
  @pirate.cohort(id.to_s).add(self, timestamp, force)
end

#add_to_cohort!(id, timestamp = nil) ⇒ Object



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

def add_to_cohort!(id, timestamp = nil)
  add_to_cohort(id, timestamp, true)
end

#add_total(id, amount = 1) ⇒ Object



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

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

#add_unique(id) ⇒ Object



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

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

#feature_active?(id) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#unique_identifierObject



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

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

#user_idObject



57
58
59
# File 'lib/blackbeard/context.rb', line 57

def user_id
  @user.id
end

#visitor_idObject



61
62
63
# File 'lib/blackbeard/context.rb', line 61

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