Sha256: 2f372e56182b4d51d8b9360710a868809b4541be1017e5ae020514a5bcb89c1a

Contents?: true

Size: 1.39 KB

Versions: 9

Compression:

Stored size: 1.39 KB

Contents

module Fakeit
  module Openapi
    module Example
      DEFAULT_BITS = 32

      def integer_example(example_options)
        if example_options[:use_static][type: 'integer', property: example_options[:property]]
          static_integer_example
        else
          random_integer_example
        end
      end

      private

      def static_integer_example
        if enum
          enum.to_a.first
        else
          int_rand_end * int_multiple
        end
      end

      def random_integer_example
        if enum
          enum.to_a.sample
        else
          Faker::Number.between(from: int_rand_begin, to: int_rand_end) * int_multiple
        end
      end

      def int_rand_begin
        min_int / int_multiple + int_rand_begin_adjust
      end

      def int_rand_end
        max_int / int_multiple
      end

      def int_rand_begin_adjust
        (min_int % int_multiple).zero? ? 0 : 1
      end

      def int_multiple
        multipleOf || 1
      end

      def min_int
        if minimum
          exclusiveMinimum ? minimum + 1 : minimum
        else
          -2**(int_bits - 1)
        end
      end

      def max_int
        if maximum
          exclusiveMaximum ? maximum - 1 : maximum
        else
          2**(int_bits - 1) - 1
        end
      end

      def int_bits
        return DEFAULT_BITS unless format =~ /int\d+/

        format[/\d+/].to_i
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
fakeit-0.7.1 lib/fakeit/openapi/example/integer_example.rb
fakeit-0.7.0 lib/fakeit/openapi/example/integer_example.rb
fakeit-0.6.3 lib/fakeit/openapi/example/integer_example.rb
fakeit-0.6.2 lib/fakeit/openapi/example/integer_example.rb
fakeit-0.6.1 lib/fakeit/openapi/example/integer_example.rb
fakeit-0.6.0 lib/fakeit/openapi/example/integer_example.rb
fakeit-0.5.3 lib/fakeit/openapi/example/integer_example.rb
fakeit-0.5.2 lib/fakeit/openapi/example/integer_example.rb
fakeit-0.5.1 lib/fakeit/openapi/example/integer_example.rb