Class: Flag::Members

Inherits:
Struct
  • Object
show all
Defined in:
lib/flag.rb

Constant Summary collapse

USERS =
"users".freeze
GROUPS =
"groups".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



7
8
9
# File 'lib/flag.rb', line 7

def name
  @name
end

Instance Method Details

#<<(item) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/flag.rb', line 11

def <<(item)
  if item.to_s.end_with?("%")
    Flag.store.call("HSET", Flag::FEATURES, name, item[0...-1])
  else
    Flag.store.call("SADD", subkey(item), item)
  end
end

#activedObject



23
24
25
# File 'lib/flag.rb', line 23

def actived
  { percentage: percentage.to_i, users: users, groups: groups }
end

#groupsObject



27
# File 'lib/flag.rb', line 27

def groups; members_for(GROUPS).map(&:to_sym) end

#include?(item) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
# File 'lib/flag.rb', line 30

def include?(item)
  return percentage == item[0...-1].to_i if item.to_s.end_with?("%")
  return true if Zlib.crc32(item.to_s) % 100 < percentage

  Flag.store.call("SISMEMBER", subkey(item), item).to_i == 1
end

#keyObject



19
20
21
# File 'lib/flag.rb', line 19

def key
  "#{Flag::FEATURES}:#{name}"
end

#resetObject



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

def reset
  [USERS, GROUPS].each { |k| Flag.store.call("DEL", "#{key}:#{k}") }
end

#usersObject



28
# File 'lib/flag.rb', line 28

def users;  members_for(USERS) end