Class: Rollout::Feature

Inherits:
Object
  • Object
show all
Defined in:
lib/rollout/feature.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, rollout:, state: nil, options: {}) ⇒ Feature

Returns a new instance of Feature.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rollout/feature.rb', line 8

def initialize(name, rollout:, state: nil, options: {})
  @name = name
  @rollout = rollout
  @options = options

  if state
    raw_percentage, raw_users, raw_groups, raw_data = state.split('|', 4)
    @percentage = raw_percentage.to_f
    @users = users_from_string(raw_users)
    @groups = groups_from_string(raw_groups)
    @data = raw_data.nil? || raw_data.strip.empty? ? {} : JSON.parse(raw_data)
  else
    clear
  end
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/rollout/feature.rb', line 5

def data
  @data
end

#groupsObject

Returns the value of attribute groups.



5
6
7
# File 'lib/rollout/feature.rb', line 5

def groups
  @groups
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/rollout/feature.rb', line 6

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/rollout/feature.rb', line 6

def options
  @options
end

#percentageObject

Returns the value of attribute percentage.



5
6
7
# File 'lib/rollout/feature.rb', line 5

def percentage
  @percentage
end

#usersObject

Returns the value of attribute users.



5
6
7
# File 'lib/rollout/feature.rb', line 5

def users
  @users
end

Instance Method Details

#active?(user) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
# File 'lib/rollout/feature.rb', line 52

def active?(user)
  if user
    id = user_id(user)
    user_in_percentage?(id) ||
      user_in_active_users?(id) ||
      user_in_active_group?(user)
  else
    @percentage == 100
  end
end

#add_group(group) ⇒ Object



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

def add_group(group)
  @groups << group.to_sym unless @groups.include?(group.to_sym)
end

#add_user(user) ⇒ Object



28
29
30
31
# File 'lib/rollout/feature.rb', line 28

def add_user(user)
  id = user_id(user)
  @users << id unless @users.include?(id)
end

#clearObject



45
46
47
48
49
50
# File 'lib/rollout/feature.rb', line 45

def clear
  @groups = groups_from_string('')
  @users = users_from_string('')
  @percentage = 0
  @data = {}
end

#deep_cloneObject



76
77
78
79
80
81
82
# File 'lib/rollout/feature.rb', line 76

def deep_clone
  c = self.clone
  c.instance_variable_set('@rollout', nil)
  c = Marshal.load(Marshal.dump(c))
  c.instance_variable_set('@rollot', @rollout)
  c
end

#remove_group(group) ⇒ Object



41
42
43
# File 'lib/rollout/feature.rb', line 41

def remove_group(group)
  @groups.delete(group.to_sym)
end

#remove_user(user) ⇒ Object



33
34
35
# File 'lib/rollout/feature.rb', line 33

def remove_user(user)
  @users.delete(user_id(user))
end

#serializeObject



24
25
26
# File 'lib/rollout/feature.rb', line 24

def serialize
  "#{@percentage}|#{@users.to_a.join(',')}|#{@groups.to_a.join(',')}|#{serialize_data}"
end

#to_hashObject



67
68
69
70
71
72
73
74
# File 'lib/rollout/feature.rb', line 67

def to_hash
  {
    percentage: @percentage,
    groups: @groups,
    users: @users,
    data: @data,
  }
end

#user_in_active_users?(user) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/rollout/feature.rb', line 63

def user_in_active_users?(user)
  @users.include?(user_id(user))
end