Sha256: 14a6cd2fdfcacf91a7beb68267a3a6d3bb0b709c6b9158505dccb8b0afdd9692

Contents?: true

Size: 1.2 KB

Versions: 9

Compression:

Stored size: 1.2 KB

Contents

module Finitio
  class Generation
    class Heuristic
      class Random < Heuristic

        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
          }
        }

        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

      end # class Random
    end # class Heuristic
  end # class Generation
end # module Finition

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
finitio-0.12.0 lib/finitio/generation/heuristic/random.rb
finitio-0.11.4 lib/finitio/generation/heuristic/random.rb
finitio-0.11.3 lib/finitio/generation/heuristic/random.rb
finitio-0.11.2 lib/finitio/generation/heuristic/random.rb
finitio-0.11.1 lib/finitio/generation/heuristic/random.rb
finitio-0.10.0 lib/finitio/generation/heuristic/random.rb
finitio-0.9.1 lib/finitio/generation/heuristic/random.rb
finitio-0.9.0 lib/finitio/generation/heuristic/random.rb
finitio-0.8.0 lib/finitio/generation/heuristic/random.rb