class Switches::Feature

Attributes

name[R]
percentage[R]

Public Class Methods

collection(instance) click to toggle source
# File lib/switches/feature.rb, line 7
def self.collection(instance)
  Collection.new(self, instance)
end
new(name, instance) click to toggle source
# File lib/switches/feature.rb, line 11
def initialize(name, instance)
  @name = name
  @instance = instance
  @percentage = Percentage(0)
  @cohorts = Set.new
end

Public Instance Methods

add(cohort_name) click to toggle source
# File lib/switches/feature.rb, line 37
def add(cohort_name)
  @cohorts.add(cohort_name.to_s)
  updated
end
as_json() click to toggle source
# File lib/switches/feature.rb, line 53
def as_json
  {
    name: name,
    percentage: percentage.to_i,
    cohorts: @cohorts.to_a
  }
end
cohorts() click to toggle source
# File lib/switches/feature.rb, line 61
def cohorts
  @cohorts.to_a
end
inspect() click to toggle source
# File lib/switches/feature.rb, line 47
def inspect
  output = "#<Feature #{@name}; #{@percentage}"
  output += "; #{@cohorts.to_a.join(", ")}" if @cohorts.any?
  output += ">"
end
key() click to toggle source
# File lib/switches/feature.rb, line 75
def key
  [type, name].join(":")
end
off() click to toggle source
# File lib/switches/feature.rb, line 32
def off
  @percentage = Percentage(0)
  updated
end
on(numeric = 100) click to toggle source
# File lib/switches/feature.rb, line 27
def on(numeric = 100)
  @percentage = Percentage(numeric)
  updated
end
on?(identifier) click to toggle source
# File lib/switches/feature.rb, line 65
def on?(identifier)
  return true if @percentage.max?

  in_cohort?(identifier) || in_percentage?(identifier)
end
reload() click to toggle source
# 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) click to toggle source
# File lib/switches/feature.rb, line 42
def remove(cohort_name)
  @cohorts.delete(cohort_name.to_s)
  updated
end
type() click to toggle source
# File lib/switches/feature.rb, line 71
def type
  "feature"
end

Private Instance Methods

in_cohort?(identifier) click to toggle source
# File lib/switches/feature.rb, line 81
def in_cohort?(identifier)
  @cohorts.any? do |cohort|
    @instance.cohort(cohort).include?(identifier)
  end
end
in_percentage?(identifier) click to toggle source
# File lib/switches/feature.rb, line 87
def in_percentage?(identifier)
  return false if @percentage.min?

  @percentage.include?(identifier)
end
updated() click to toggle source
# File lib/switches/feature.rb, line 93
def updated
  @instance.set(self)
  @instance.notify(self)
  self
end