Class: Switches::Feature

Inherits:
Object
  • Object
show all
Includes:
JSONSerializer
Defined in:
lib/switches/feature.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) ⇒ Feature

Returns a new instance of Feature.



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

def initialize(name, instance)
  @name = name
  @instance = instance
  @percentage = Percentage(0)
  @cohorts = Set.new
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#percentageObject (readonly)

Returns the value of attribute percentage.



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

def percentage
  @percentage
end

Class Method Details

.collection(instance) ⇒ Object



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

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

Instance Method Details

#add(cohort_name) ⇒ Object



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

def add(cohort_name)
  @cohorts.add(cohort_name.to_s)
  updated
end

#as_jsonObject



53
54
55
56
57
58
59
# File 'lib/switches/feature.rb', line 53

def as_json
  {
    name: name,
    percentage: percentage.to_i,
    cohorts: @cohorts.to_a
  }
end

#cohortsObject



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

def cohorts
  @cohorts.to_a
end

#inspectObject



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

def inspect
  output = "#<Feature #{@name}; #{@percentage}"
  output += "; #{@cohorts.to_a.join(", ")}" if @cohorts.any?
  output += ">"
end

#keyObject



75
76
77
# File 'lib/switches/feature.rb', line 75

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

#offObject



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

def off
  @percentage = Percentage(0)
  updated
end

#on(numeric = 100) ⇒ Object



27
28
29
30
# File 'lib/switches/feature.rb', line 27

def on(numeric = 100)
  @percentage = Percentage(numeric)
  updated
end

#on?(identifier) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
# File 'lib/switches/feature.rb', line 65

def on?(identifier)
  return true if @percentage.max?

  in_cohort?(identifier) || in_percentage?(identifier)
end

#reloadObject



18
19
20
21
22
23
24
25
# File 'lib/switches/feature.rb', line 18

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

  self
end

#remove(cohort_name) ⇒ Object



42
43
44
45
# File 'lib/switches/feature.rb', line 42

def remove(cohort_name)
  @cohorts.delete(cohort_name.to_s)
  updated
end

#typeObject



71
72
73
# File 'lib/switches/feature.rb', line 71

def type
  "feature"
end