Sha256: bb92d79084d32f7676f024291829d57f93971f11d194ec9bea9cda0dc3463ac8

Contents?: true

Size: 1.04 KB

Versions: 10

Compression:

Stored size: 1.04 KB

Contents

module Fakeit
  module Openapi
    module Example
      MIN_NUM = -2**31
      MAX_NUM = 2**31 - 1

      def number_example(example_options)
        if example_options[:use_static][type: 'number', property: example_options[:property]]
          static_number_example
        else
          random_number_example
        end
      end

      private

      def static_number_example
        (num_rand_end * num_multiple)
          .then { multipleOf ? _1 : _1.round(2) }
      end

      def random_number_example
        (Faker::Number.between(from: num_rand_begin, to: num_rand_end) * num_multiple)
          .then { multipleOf ? _1 : _1.round(2) }
      end

      def num_rand_begin
        multipleOf ? (min_num / multipleOf).ceil : min_num
      end

      def num_rand_end
        multipleOf ? (max_num / multipleOf).floor : max_num
      end

      def num_multiple
        multipleOf || 1
      end

      def min_num
        (minimum || MIN_NUM).to_f.ceil(2)
      end

      def max_num
        (maximum || MAX_NUM).to_f.floor(2)
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

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