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_by!(step_size) ⇒ 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
21 22 23 24 25 |
# 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 |
Instance Attribute Details
#factor ⇒ Object
Returns the value of attribute factor.
19 20 21 |
# File 'lib/json_test_data/data_structures/number.rb', line 19 def factor @factor end |
#maximum ⇒ Object
Returns the value of attribute maximum.
19 20 21 |
# File 'lib/json_test_data/data_structures/number.rb', line 19 def maximum @maximum end |
#minimum ⇒ Object
Returns the value of attribute minimum.
19 20 21 |
# File 'lib/json_test_data/data_structures/number.rb', line 19 def minimum @minimum end |
#type ⇒ Object
Returns the value of attribute type.
19 20 21 |
# File 'lib/json_test_data/data_structures/number.rb', line 19 def type @type end |
#value ⇒ Object
Returns the value of attribute value.
19 20 21 |
# File 'lib/json_test_data/data_structures/number.rb', line 19 def value @value end |
Class Method Details
.create(schema) ⇒ Object
8 9 10 11 12 13 14 15 16 |
# 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 |
Instance Method Details
#adjust! ⇒ Object
67 68 69 70 71 72 73 74 75 |
# 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) ⇒ Object
62 63 64 65 |
# 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! ⇒ Object
34 35 36 37 |
# 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? ⇒ Boolean
39 40 41 |
# File 'lib/json_test_data/data_structures/number.rb', line 39 def is_int? type == :integer end |
#should_be_int_but_isnt? ⇒ Boolean
58 59 60 |
# 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 ⇒ Object
27 28 29 30 31 32 |
# 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? ⇒ Boolean
53 54 55 56 |
# 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? ⇒ Boolean
48 49 50 51 |
# 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? ⇒ Boolean
43 44 45 46 |
# File 'lib/json_test_data/data_structures/number.rb', line 43 def value_too_low? return false unless minimum @value <= minimum end |