Class: Switches::Cohort

Inherits:
Object
  • Object
show all
Includes:
JSONSerializer
Defined in:
lib/switches/cohort.rb

Constant Summary

Constants included from JSONSerializer

JSONSerializer::ParserError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JSONSerializer

deserialize, serialize, #to_json

Constructor Details

#initialize(name, instance) ⇒ Cohort

Returns a new instance of Cohort.



11
12
13
14
15
# File 'lib/switches/cohort.rb', line 11

def initialize(name, instance)
  @name = name
  @instance = instance
  @members = Set.new
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/switches/cohort.rb', line 5

def name
  @name
end

Class Method Details

.collection(instance) ⇒ Object



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

def self.collection(instance)
  Collection.new(self, instance)
end

Instance Method Details

#add(identifier) ⇒ Object



29
30
31
32
# File 'lib/switches/cohort.rb', line 29

def add(identifier)
  @members.add(identifier.to_s)
  updated
end

#as_jsonObject



46
47
48
49
50
51
# File 'lib/switches/cohort.rb', line 46

def as_json
  {
    name: name,
    members: members
  }
end

#include?(identifier) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/switches/cohort.rb', line 25

def include?(identifier)
  @members.include?(identifier.to_s)
end

#inspectObject



39
40
41
42
43
44
# File 'lib/switches/cohort.rb', line 39

def inspect
  output = "#<Cohort #{@name}"
  output += "; #{@members.count} member"
  output += "s" unless @members.count == 1
  output += ">"
end

#keyObject



61
62
63
# File 'lib/switches/cohort.rb', line 61

def key
  [type, name].join(":")
end

#membersObject



53
54
55
# File 'lib/switches/cohort.rb', line 53

def members
  @members.to_a
end

#reloadObject



17
18
19
20
21
22
23
# File 'lib/switches/cohort.rb', line 17

def reload
  if attributes = @instance.get(self)
    @members = attributes["members"].to_set
  end

  self
end

#remove(identifier) ⇒ Object



34
35
36
37
# File 'lib/switches/cohort.rb', line 34

def remove(identifier)
  @members.delete(identifier.to_s)
  updated
end

#typeObject



57
58
59
# File 'lib/switches/cohort.rb', line 57

def type
  "cohort"
end