Class: JsonTestData::Number

Inherits:
Object
  • Object
show all
Extended by:
NumberHelper
Defined in:
lib/json_test_data/data_structures/number.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NumberHelper

between

Constructor Details

#initialize(min: nil, max: nil, factor: nil, value: nil, type: nil) ⇒ Number



19
20
21
22
23
# File 'lib/json_test_data/data_structures/number.rb', line 19

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

Instance Attribute Details

#factorObject

Returns the value of attribute factor.



17
18
19
# File 'lib/json_test_data/data_structures/number.rb', line 17

def factor
  @factor
end

#maximumObject

Returns the value of attribute maximum.



17
18
19
# File 'lib/json_test_data/data_structures/number.rb', line 17

def maximum
  @maximum
end

#minimumObject

Returns the value of attribute minimum.



17
18
19
# File 'lib/json_test_data/data_structures/number.rb', line 17

def minimum
  @minimum
end

#typeObject

Returns the value of attribute type.



17
18
19
# File 'lib/json_test_data/data_structures/number.rb', line 17

def type
  @type
end

#valueObject

Returns the value of attribute value.



17
18
19
# File 'lib/json_test_data/data_structures/number.rb', line 17

def value
  @value
end

Class Method Details

.create(schema) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/json_test_data/data_structures/number.rb', line 8

def create(schema)
  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

Instance Method Details

#adjust!Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/json_test_data/data_structures/number.rb', line 60

def adjust!
  while !value_divisible_by_factor? || value_too_low? || value_too_high? || should_be_int_but_isnt?
    adjust_for_divisibility!

    @value -= step_size if value_too_high?
    @value += step_size if value_too_low?
  end

  @value ||= 1
  @type == :number ? @value : @value.to_i
end

#adjust_for_divisibility!Object



32
33
34
35
# File 'lib/json_test_data/data_structures/number.rb', line 32

def adjust_for_divisibility!
  return if value_divisible_by_factor?
  @value *= factor
end

#is_int?Boolean



37
38
39
# File 'lib/json_test_data/data_structures/number.rb', line 37

def is_int?
  type == :integer
end

#should_be_int_but_isnt?Boolean



56
57
58
# File 'lib/json_test_data/data_structures/number.rb', line 56

def should_be_int_but_isnt?
  type == :integer && !@value.is_a?(Integer)
end

#step_sizeObject



25
26
27
28
29
30
# File 'lib/json_test_data/data_structures/number.rb', line 25

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?Boolean



51
52
53
54
# File 'lib/json_test_data/data_structures/number.rb', line 51

def value_divisible_by_factor?
  return true unless factor
  @value % factor == 0
end

#value_too_high?Boolean



46
47
48
49
# File 'lib/json_test_data/data_structures/number.rb', line 46

def value_too_high?
  return false unless maximum
  @value >= maximum
end

#value_too_low?Boolean



41
42
43
44
# File 'lib/json_test_data/data_structures/number.rb', line 41

def value_too_low?
  return false unless minimum
  @value <= minimum
end