class JsonTestData::Number
Attributes
factor[RW]
maximum[RW]
minimum[RW]
type[RW]
value[RW]
Public Class Methods
create(schema)
click to toggle source
# File lib/json_test_data/data_structures/number.rb, line 8 def create(schema) return schema.fetch(:enum).sample if schema.fetch(:enum, nil) factor = schema.fetch(:multipleOf, nil) minimum, maximum = schema.fetch(:minimum, nil), schema.fetch(:maximum, nil) num = new(min: minimum, max: maximum, factor: factor, type: schema.fetch(:type, "number").to_sym) num.adjust! end
new(min: nil, max: nil, factor: nil, value: nil, type: nil)
click to toggle source
# File lib/json_test_data/data_structures/number.rb, line 21 def initialize(min: nil, max: nil, factor: nil, value: nil, type: nil) @factor, @minimum, @maximum = factor, min, max @value = value || @factor || rand(1000) @type = type || :number end
Public Instance Methods
adjust!()
click to toggle source
# File lib/json_test_data/data_structures/number.rb, line 67 def adjust! while !value_divisible_by_factor? || value_too_low? || value_too_high? || should_be_int_but_isnt? adjust_for_divisibility! adjust_by!(step_size) end @value ||= 1 @type == :number ? @value : @value.to_i end
adjust_by!(step_size)
click to toggle source
# File lib/json_test_data/data_structures/number.rb, line 62 def adjust_by!(step_size) @value -= step_size if value_too_high? @value += step_size if value_too_low? end
adjust_for_divisibility!()
click to toggle source
# File lib/json_test_data/data_structures/number.rb, line 34 def adjust_for_divisibility! return if value_divisible_by_factor? @value *= factor end
is_int?()
click to toggle source
# File lib/json_test_data/data_structures/number.rb, line 39 def is_int? type == :integer end
should_be_int_but_isnt?()
click to toggle source
# File lib/json_test_data/data_structures/number.rb, line 58 def should_be_int_but_isnt? type == :integer && !@value.is_a?(Integer) end
step_size()
click to toggle source
# File lib/json_test_data/data_structures/number.rb, line 27 def step_size return @step_size ||= is_int? ? 1 : 0.5 unless minimum && maximum @step_size ||= Number.between(min: minimum, max: maximum, integer: type == :integer) / 3 is_int? ? @step_size.to_i : @step_size.round(2) end
value_divisible_by_factor?()
click to toggle source
# File lib/json_test_data/data_structures/number.rb, line 53 def value_divisible_by_factor? return true unless factor @value % factor == 0 end
value_too_high?()
click to toggle source
# File lib/json_test_data/data_structures/number.rb, line 48 def value_too_high? return false unless maximum @value >= maximum end
value_too_low?()
click to toggle source
# File lib/json_test_data/data_structures/number.rb, line 43 def value_too_low? return false unless minimum @value <= minimum end