Sha256: 87ff7ef4b5bd9bc598c60c4268b8c897b72d2dd0e4d8a3b966cf8a432eab6da3

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module RubyOnAcid

class Factory

  def initialize(*args)
    @minimums = {}
    @maximums = {}
  end

  #Calls get_unit with key to get value between 0.0 and 1.0, then converts that value to be between given minimum and maximum.
  def within(key, minimum, maximum)
    get_unit(key) * (maximum - minimum) + minimum
  end
  
  #Calls get_unit with key to get value between 0.0 and 1.0, then converts that value to be between given minimum and maximum.
  def get(key, options = {})
    @minimums[key] = (options[:min] || @minimums[key] || 0.0)
    @maximums[key] = (options[:max] || @maximums[key] || (@minimums[key] > 1.0 ? @minimums[key] + 1.0 : 1.0))
    get_unit(key) * (@maximums[key] - @minimums[key]) + @minimums[key]
  end
  
  #Returns true if get_unit(key) returns greater than 0.5.
  def boolean(key)
    get_unit(key) >= 0.5
  end
  
  #Calls get_unit with key to get value between 0.0 and 1.0, then converts that value to an index within the given list of choices.
  #choices can be an array or an argument list of arbitrary size.
  def choose(key, *choices)
    all_choices = choices.flatten
    index = (get_unit(key) * all_choices.length).floor
    index = all_choices.length - 1 if index > all_choices.length - 1
    all_choices[index]
  end
  
end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubyonacid-0.2.0 lib/rubyonacid/factory.rb