Class: Synthra::Types::RepeatingElement

Inherits:
Base
  • Object
show all
Defined in:
lib/synthra/types/repeating_element.rb

Overview

Repeating element type generator (stub implementation)

Instance Method Summary collapse

Instance Method Details

#clamp_count(count) ⇒ Object (private)

Bound the repeat count to the same ceiling as the other array-producing types (schema_ref, array). Without this, a tenant-supplied count: allocates an unbounded Array/String — a denial-of-service vector. Floors at 0.



34
35
36
37
# File 'lib/synthra/types/repeating_element.rb', line 34

def clamp_count(count)
  max = Synthra.configuration&.limits&.max_array_size || 1000
  [[count.to_i, 0].max, max].min
end

#generate_edge(rng, context, args) ⇒ Object



21
22
23
# File 'lib/synthra/types/repeating_element.rb', line 21

def generate_edge(rng, context, args)
  rng.sample(["", "-", "---"])
end

#generate_from_type(rng, context, type_name, count) ⇒ Object (private)



39
40
41
42
43
44
45
# File 'lib/synthra/types/repeating_element.rb', line 39

def generate_from_type(rng, context, type_name, count)
  type = Types::Registry.lookup(type_name)
  Array.new(count) { type.generate_random(rng, context, {}) }

rescue StandardError
  []
end

#generate_invalid(rng, context, args) ⇒ Object



25
26
27
# File 'lib/synthra/types/repeating_element.rb', line 25

def generate_invalid(rng, context, args)
  rng.sample([nil, 123, [], {}])
end

#generate_random(rng, context, args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/synthra/types/repeating_element.rb', line 7

def generate_random(rng, context, args)
  value = args[:value]
  count = clamp_count(args[:count] || 1)
  type_name = args[:type]

  if type_name
    generate_from_type(rng, context, type_name, count)
  elsif value
    value.to_s * count
  else
    ""
  end
end