Class: Finitio::Generation::Heuristic::Random

Inherits:
Finitio::Generation::Heuristic show all
Defined in:
lib/finitio/generation/heuristic/random.rb

Constant Summary collapse

RANDOMERS =
{
  NilClass => nil,

  TrueClass => true,

  FalseClass => false,

  Integer => ->(_,g,_) {
    g.flip_one_out_of(1_000_000)
  },

  Float => ->(_,g,_) {
    g.flip_one_out_of(1_000_000)            
  },

  String => ->(_,g,_) {
    (1..3).map{ SecureRandom.hex(6) }.join(" ")
  },

  Date => ->(_,g,_) {
    Time.at(rand * Time.now.to_i).to_date
  },

  Time => ->(_,g,_) {
    Time.at(rand * Time.now.to_i)
  },

  DateTime => ->(_,g,_) {
    Time.at(rand * Time.now.to_i).to_datetime
  }
}

Instance Method Summary collapse

Instance Method Details

#call(ruby_type, generator, world = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/finitio/generation/heuristic/random.rb', line 38

def call(ruby_type, generator, world = nil)
  r = RANDOMERS.fetch(ruby_type) do
    pair = RANDOMERS.find do |clazz, value|
      clazz >= ruby_type
    end
    throw :unfound unless pair
    pair.last
  end
  r.is_a?(Proc) ? r.call(ruby_type, generator, world) : r
end