class FairRandom::Box

Public Class Methods

from_poro(poro) click to toggle source
# File lib/fair_random.rb, line 28
def self.from_poro(poro)
  instance = new(poro['type_count'], poro['box_capacity'])
  instance.instance_eval do
    @contents = poro['contents']
  end
  instance
end
new(element_type_count, box_capacity) click to toggle source
# File lib/fair_random.rb, line 5
def initialize(element_type_count, box_capacity)
  raise ArgumentError if box_capacity % element_type_count != 0
  @element_type_count = element_type_count
  @box_capacity = box_capacity
  reset_contents
end

Public Instance Methods

next() click to toggle source
# File lib/fair_random.rb, line 12
def next
  reset_contents if @index >= @contents.size
  c = @contents[@index]
  @index += 1
  c
end
to_poro() click to toggle source

Convert to PORO(Plain Old Ruby Object) for serialize.

# File lib/fair_random.rb, line 20
def to_poro
  {
    'type_count' => @element_type_count,
    'box_capacity' => @box_capacity,
    'contents' => @contents,
  }
end

Private Instance Methods

reset_contents() click to toggle source
# File lib/fair_random.rb, line 37
def reset_contents
  @contents = []
  @index = 0
  # type_index => count
  @element_type_count.times do|type|
    @contents.concat([type] * (@box_capacity / @element_type_count))
  end
  @contents = @contents.shuffle
end