Module: Trebuchet::Strategy::PerDenominationable

Included in:
PerDenomination, Percentable
Defined in:
lib/trebuchet/strategy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#denominatorObject (readonly)

Returns the value of attribute denominator.



81
82
83
# File 'lib/trebuchet/strategy.rb', line 81

def denominator
  @denominator
end

#numeratorObject (readonly)

Returns the value of attribute numerator.



81
82
83
# File 'lib/trebuchet/strategy.rb', line 81

def numerator
  @numerator
end

Instance Method Details

#exportObject



111
112
113
# File 'lib/trebuchet/strategy.rb', line 111

def export
  super({ numerator: numerator, denominator: denominator })
end

#initialize(options) ⇒ Object



83
84
85
# File 'lib/trebuchet/strategy.rb', line 83

def initialize(options)
  set_range_from_options(options)
end

#set_range_from_options(options) ⇒ Object

must be called from initialize

Raises:

  • (ArgumentError)


88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/trebuchet/strategy.rb', line 88

def set_range_from_options(options)
  raise ArgumentError, "Missing required input numerator" unless options[:numerator]
  raise ArgumentError, "Missing required input denominator" unless options[:denominator]

  @numerator = options[:numerator].to_i
  @denominator = options[:denominator].to_i

  raise ArgumentError, "Invalid denominator #{@denominator}" if @denominator <= 0
  if @numerator > @denominator
    raise ArgumentError, "Invalid numerator #{@numerator} > denominator #{@denominator}"
  end
end

#to_sObject



107
108
109
# File 'lib/trebuchet/strategy.rb', line 107

def to_s
  "#{numerator} / #{denominator} of users"
end

#value_in_range?(value) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
104
105
# File 'lib/trebuchet/strategy.rb', line 101

def value_in_range?(value)
  bucket = Trebuchet::SHA1.hexdigest("#{@feature.name}|#{value}").to_i(16) % denominator

  bucket < numerator
end