Sha256: b0583193187f7ad19d6017896da27c56f07fc3198078a8b0e9f5dc5a96428e0d

Contents?: true

Size: 1.45 KB

Versions: 4

Compression:

Stored size: 1.45 KB

Contents

module Fakeit
  module Openapi
    module Example
      def array_example(options)
        example_options = add_depth(options)
        if example_options[:use_static][type: 'array', property: example_options[:property]]
          generate_array_example(example_options, -> { min_array })
        else
          generate_array_example(example_options, -> { random_array_size(example_options) })
        end
      end

      private

      def generate_array_example(example_options, get_size)
        size = retries = get_size[]
        [].tap { generate_items(size, retries, example_options, _1) }
      end

      def random_array_size(example_options)
        uniqueItems ? min_array : Faker::Number.between(from: min_array, to: max_array(example_options[:depth]))
      end

      def generate_items(size, retries, example_options, result)
        loop do
          item = items.to_example(example_options)

          if need_retry?(item, result, retries)
            retries -= 1
          elsif (result << item).size >= size
            break
          end
        end
      end

      def add_depth(example_options)
        { **example_options, depth: example_options[:depth] + 1 }
      end

      def need_retry?(item, result, retries)
        uniqueItems && result.include?(item) && retries.positive?
      end

      def min_array
        minItems || 1
      end

      def max_array(depth)
        maxItems || min_array + (depth > 1 ? 2 : 9)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fakeit-0.5.3 lib/fakeit/openapi/example/array_example.rb
fakeit-0.5.2 lib/fakeit/openapi/example/array_example.rb
fakeit-0.5.1 lib/fakeit/openapi/example/array_example.rb
fakeit-0.5.0 lib/fakeit/openapi/example/array_example.rb