class SiteHub::Collection::SplitRouteCollection

Constants

FIXNUM_ERR_MSG
INVALID_SPILTS_MSG
MAX
MIN
SPLIT_ERR_MSG

Public Class Methods

new(hash = {}) click to toggle source
# File lib/sitehub/collection/split_route_collection.rb, line 15
def initialize(hash = {})
  hash.each do |value, percentage|
    add(value.id, value, percentage)
  end
end

Public Instance Methods

[](key) click to toggle source
Calls superclass method
# File lib/sitehub/collection/split_route_collection.rb, line 39
def [](key)
  key?(key) ? super(key).value : nil
end
add(id, value, percentage) click to toggle source
# File lib/sitehub/collection/split_route_collection.rb, line 21
def add(id, value, percentage)
  raise InvalidSplitException, FIXNUM_ERR_MSG unless percentage.is_a?(Fixnum)
  upper_bound = lower_bound + percentage

  raise InvalidSplitException, SPLIT_ERR_MSG if upper_bound > MAX
  self[id] = Split.new(lower_bound, upper_bound, value)
end
resolve(*args) click to toggle source
# File lib/sitehub/collection/split_route_collection.rb, line 29
def resolve(*args)
  random = rand(MAX)
  result = values.find { |split| random >= split.lower && random < split.upper }
  result ? result.value.resolve(*args) : nil
end
transform(&block) click to toggle source
# File lib/sitehub/collection/split_route_collection.rb, line 35
def transform(&block)
  values.each { |split| split.update_value(&block) }
end
valid?() click to toggle source
# File lib/sitehub/collection/split_route_collection.rb, line 43
def valid?
  last = values.last
  return true if last && last.upper == MAX

  warn(INVALID_SPILTS_MSG)
  false
end

Private Instance Methods

lower_bound() click to toggle source
# File lib/sitehub/collection/split_route_collection.rb, line 53
def lower_bound
  values.empty? ? MIN : values.last.upper
end