Module: Trebuchet::Strategy::PercentableDeprecated

Included in:
PercentDeprecated, VisitorPercentDeprecated
Defined in:
lib/trebuchet/strategy.rb

Overview

This module is deprecated because the implementation is such that it’s not possible to trust per-feature analysis if multiple features are using the PercentableDeprecated based strategies because the same visitors will tend to get the same features (even with the offset).

Instance Method Summary collapse

Instance Method Details

#exportObject



224
225
226
227
228
229
230
# File 'lib/trebuchet/strategy.rb', line 224

def export
  if @style == :percentage
    super :percentage => @to
  else
    super :from => @from, :to => @to
  end
end

#initialize(options) ⇒ Object



153
154
155
# File 'lib/trebuchet/strategy.rb', line 153

def initialize(options)
  set_range_from_options(options)
end

#offsetObject



177
178
179
180
181
182
183
# File 'lib/trebuchet/strategy.rb', line 177

def offset
  if @style == :percentage
    feature_id % 100
  else
    0
  end
end

#offset_fromObject



201
202
203
# File 'lib/trebuchet/strategy.rb', line 201

def offset_from
  (@from + offset) % 100
end

#offset_toObject



205
206
207
# File 'lib/trebuchet/strategy.rb', line 205

def offset_to
  (@to + offset) % 100
end

#percentageObject



185
186
187
188
189
# File 'lib/trebuchet/strategy.rb', line 185

def percentage
  return 0 unless @to.is_a?(Integer) && @from.is_a?(Integer)
  return 0 if @to < 0
  ((@to - @from) + 100) % 100 + 1
end

#set_range_from_options(options) ⇒ Object

must be called from initialize



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/trebuchet/strategy.rb', line 158

def set_range_from_options(options)
  if options == nil || options.is_a?(Numeric)
    @from = 0
    @to = options.to_i - 1
    @style = :percentage
  elsif options.is_a?(Hash) && (p = options['percentage'] || options[:percentage])
    @from = 0
    @to = p.to_i - 1
    @style = :percentage
  elsif options.is_a?(Hash)
    @from = options['from'] || options[:from]
    @to = options['to'] || options[:to]
    @style = :range
  else
    @from = 0
    @to = -1
  end
end

#to_sObject



209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/trebuchet/strategy.rb', line 209

def to_s
  kind = self.name == :visitor_percent ? "visitors" : "users"
  percentage_str = "#{percentage}% of #{kind}"
  range_str = if @to < 0
    "nobody"
  else
    str = ''
    str << "user id ending with " if kind != "visitors"
    str << "#{offset_from.to_s.rjust(2, '0')}"
    str << " to #{offset_to.to_s.rjust(2, '0')}" if @to != @from
    str
  end
  @style == :range ? "#{range_str} (#{percentage_str})" : "#{percentage_str} (#{range_str})"
end

#value_in_range?(value) ⇒ Boolean

call from launch_at? and pass in user id or another integer

Returns:

  • (Boolean)


192
193
194
195
196
197
198
199
# File 'lib/trebuchet/strategy.rb', line 192

def value_in_range?(value)
  return false unless @from && @to
  return false if @from.to_i < 0 || @to.to_i < 0
  return false if value == nil || !value.is_a?(Numeric)
  cutoff = percentage
  value = ((value - @from) + 200 - offset) % 100
  !!(value < cutoff)
end