Module: Genny::Hash

Defined in:
lib/genny/json_schema.rb

Constant Summary collapse

@@additional_properties =
1

Class Method Summary collapse

Class Method Details

.additional_properties=(chance) ⇒ Object

Raises:

  • (ArgumentError)


63
64
65
66
# File 'lib/genny/json_schema.rb', line 63

def self.additional_properties=(chance)
  raise ArgumentError, "chance must be a probability between 0 and 1" if chance < 0 || chance > 1
  @@additional_properties = chance
end

.genny(opts = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/genny/json_schema.rb', line 68

def self.genny(opts = {})
  opts = Genny.symbolize(opts)
  ::Hash[(opts[:properties] || {}).map { |key, schema_object|
    next nil if !(opts[:required] || []).include?(key) && Random.rand > @@additional_properties
    [
      key,
      JSONSchema.new(
        schema_object.merge(
          hint: [key, opts[:hint]].compact.join(" ")
        ),
        definitions: opts[:definitions],
        validate: false
      ).genny
    ]
  }.compact]
end