Class: JsonTestData::Number
- Inherits:
-
Object
- Object
- JsonTestData::Number
- Extended by:
- NumberHelper
- Defined in:
- lib/json_test_data/data_structures/number.rb
Instance Attribute Summary collapse
-
#factor ⇒ Object
Returns the value of attribute factor.
-
#maximum ⇒ Object
Returns the value of attribute maximum.
-
#minimum ⇒ Object
Returns the value of attribute minimum.
-
#type ⇒ Object
Returns the value of attribute type.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #adjust! ⇒ Object
- #adjust_for_divisibility! ⇒ Object
-
#initialize(min: nil, max: nil, factor: nil, value: nil, type: nil) ⇒ Number
constructor
A new instance of Number.
- #is_int? ⇒ Boolean
- #should_be_int_but_isnt? ⇒ Boolean
- #step_size ⇒ Object
- #value_divisible_by_factor? ⇒ Boolean
- #value_too_high? ⇒ Boolean
- #value_too_low? ⇒ Boolean
Methods included from NumberHelper
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
#factor ⇒ Object
Returns the value of attribute factor.
17 18 19 |
# File 'lib/json_test_data/data_structures/number.rb', line 17 def factor @factor end |
#maximum ⇒ Object
Returns the value of attribute maximum.
17 18 19 |
# File 'lib/json_test_data/data_structures/number.rb', line 17 def maximum @maximum end |
#minimum ⇒ Object
Returns the value of attribute minimum.
17 18 19 |
# File 'lib/json_test_data/data_structures/number.rb', line 17 def minimum @minimum end |
#type ⇒ Object
Returns the value of attribute type.
17 18 19 |
# File 'lib/json_test_data/data_structures/number.rb', line 17 def type @type end |
#value ⇒ Object
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_size ⇒ Object
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 |