Module: Blackbeard::FeatureRollout

Included in:
Feature
Defined in:
lib/blackbeard/feature_rollout.rb

Instance Method Summary collapse

Instance Method Details

#active_segment?(context) ⇒ Boolean

Returns:

  • (Boolean)


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

def active_segment?(context)
  return false unless group_segments
  #TODO: speed this up. memoize group on the feature. store segment in user session
  group_segments.each_pair do |group_id, segments|
    next if segments.nil? || segments.empty?
    group = Group.find(group_id) or next
    user_segment = group.segment_for(context) or next
    return true if segments.include?(user_segment)
  end
  false
end

#active_user?(context) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
# File 'lib/blackbeard/feature_rollout.rb', line 19

def active_user?(context)
  return false if (users_rate.zero? || !context.user)
  return true if users_rate == 100

  user_id = id_to_int(context.user_id)
  (user_id % 100).between?(1,users_rate)
end

#active_visitor?(context) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/blackbeard/feature_rollout.rb', line 27

def active_visitor?(context)
  return false if visitors_rate.zero?
  return true if visitors_rate == 100
  (context.visitor_id % 100).between?(1,visitors_rate)
end

#id_to_int(id) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/blackbeard/feature_rollout.rb', line 33

def id_to_int(id)
  if id.kind_of?(Integer)
    id
  elsif id.kind_of?(String)
    bytes = id.bytes
    if bytes.count > 8
      bytes = bytes[-8..-1]
    end
    bytes.inject { |sum, n| sum * n }
  else
    raise UserIdNotDivisable
  end
end

#rollout?(context) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/blackbeard/feature_rollout.rb', line 3

def rollout?(context)
  active_user?(context) || active_visitor?(context) || active_segment?(context)
end