module Fakeit::Openapi::Schema

Public Instance Methods

to_example(example_options) click to toggle source
# File lib/fakeit/openapi/schema.rb, line 13
def to_example(example_options)
  return example if example_options[:use_example] && example

  return one_of_example(example_options) if one_of
  return all_of_example(example_options) if all_of
  return any_of_example(example_options) if any_of

  type_based_example(example_options)
end

Private Instance Methods

all_of_example(example_options) click to toggle source
# File lib/fakeit/openapi/schema.rb, line 33
def all_of_example(example_options)
  all_of
    .select { _1.type == 'object' }
    .map { _1.to_example(example_options) }
    .reduce(&:merge)
end
any_of_example(example_options) click to toggle source
# File lib/fakeit/openapi/schema.rb, line 40
def any_of_example(example_options)
  any_of_options(example_options)
    .map { _1.to_example(example_options) }
    .reduce(&:merge)
end
any_of_options(example_options) click to toggle source
# File lib/fakeit/openapi/schema.rb, line 46
def any_of_options(example_options)
  any_of
    .select { _1.type == 'object' }
    .then do |options|
      if example_options[:use_static][property: example_options[:property]]
        options
      else
        options.sample(Faker::Number.between(from: 1, to: any_of.size))
      end
    end
end
one_of_example(example_options) click to toggle source
# File lib/fakeit/openapi/schema.rb, line 25
def one_of_example(example_options)
  if example_options[:use_static][property: example_options[:property]]
    one_of.first.to_example(example_options)
  else
    one_of.sample.to_example(example_options)
  end
end
type_based_example(example_options) click to toggle source
# File lib/fakeit/openapi/schema.rb, line 58
def type_based_example(example_options)
  send("#{type}_example", example_options) if %w[string integer number boolean array object].include?(type)
end