Sha256: 9bc62817395bf0837b514cd23b47d297d6402b41483f205f6412ef7db18c4a5d

Contents?: true

Size: 1.32 KB

Versions: 9

Compression:

Stored size: 1.32 KB

Contents

require_relative "./int_gen"
require_relative "./float_gen"
require_relative "./var_word_gen"
require_relative "./array_gen"
require_relative "./keyword_gen"

module VGen
  class HashGen
    def initialize(max_depth: 3)
      @max_depth = max_depth
    end
    
    def call(
          only: [
            IntGen.new,
            FloatGen.new,
            VarWordGen.new,
            ArrayGen.new,
            self.class.new,
            KeywordGen.new
          ],
          except: [],
          min: 4,
          max: 8
        )
      val_gens = [] + only
      val_gens.delete_if {|e| except.include? e.class}
      if @max_depth == 0
        val_gens.delete_if {|e| e.class == ArrayGen}
        val_gens.delete_if {|e| e.class == self.class}
      end
      if @max_depth > 0
        val_gens.map do |e|
          next e unless (e.class == ArrayGen || e.class == self.class)
          if e.class == ArrayGen
            next ArrayGen.new(max_depth: @max_depth - 1)
          end
          if e.class == self.class
            next self.class.new(max_depth: @max_depth - 1)
          end
        end
      end
      key_gens = [IntGen.new, VarWordGen.new, KeywordGen.new]
      hash = Hash[Array.new(Random.new.rand(min..max)) { [key_gens.sample.call, val_gens.sample.call] }]
    end
  end
end

Version data entries

9 entries across 7 versions & 2 rubygems

Version Path
bsielski_control_flow-1.0.0 vendor/bundle/ruby/2.5.0/gems/bsielski_value_generator-1.4.0/lib/v_gen/old_hash_gen.rb
bsielski_control_flow-1.0.0 vendor/bundle/ruby/2.5.0/gems/bsielski_value_generator-1.3.0/lib/v_gen/old_hash_gen.rb
bsielski_control_flow-1.0.0 vendor/bundle/ruby/2.5.0/gems/bsielski_value_generator-1.2.0/lib/bsielski_v_gen/old_hash_gen.rb
bsielski_value_generator-1.4.0 lib/v_gen/old_hash_gen.rb
bsielski_value_generator-1.3.0 lib/v_gen/old_hash_gen.rb
bsielski_value_generator-1.2.0 lib/bsielski_v_gen/old_hash_gen.rb
bsielski_value_generator-1.1.0 lib/bsielski_v_gen/old_hash_gen.rb
bsielski_value_generator-1.0.1 lib/bsielski_v_gen/old_hash_gen.rb
bsielski_value_generator-1.0.0 lib/bsielski_v_gen/old_hash_gen.rb