Module: DirtySeed::Assigners::MinMaxHelper

Included in:
Float, Integer, String
Defined in:
lib/dirty_seed/assigners/min_max_helper.rb

Overview

Helps with min and max validations

Instance Method Summary collapse

Instance Method Details

#acceptable_maxInteger, Float

Returns a value representing the maximal acceptable value

Returns:



85
86
87
88
89
# File 'lib/dirty_seed/assigners/min_max_helper.rb', line 85

def acceptable_max
  return unless min_max_validator

  min_max_validator.options[:in]&.max || type_related_acceptable_max
end

#acceptable_minInteger, Float

Returns a value representing the minimal acceptable value

Returns:



65
66
67
68
69
# File 'lib/dirty_seed/assigners/min_max_helper.rb', line 65

def acceptable_min
  return unless min_max_validator

  min_max_validator.options[:in]&.min || type_related_acceptable_min
end

#ceilingInteger, Float

Returns default max depending on type

Returns:



42
43
44
45
46
47
48
# File 'lib/dirty_seed/assigners/min_max_helper.rb', line 42

def ceiling
  case type
  when :string then 50
  when :integer then 42
  when :float then 42.0
  end
end

#define_min_and_maxvoid

This method returns an undefined value.

Defines min and max depending on validator



21
22
23
24
25
26
27
28
# File 'lib/dirty_seed/assigners/min_max_helper.rb', line 21

def define_min_and_max
  @min = acceptable_min
  @max = acceptable_max

  # If necessary, adjust a value depending on the other
  @min ||= floor
  @max ||= @min + ceiling || ceiling # rubocop:disable Naming/MemoizedInstanceVariableName
end

#floorInteger, Float

Returns default min depending on type

Returns:



32
33
34
35
36
37
38
# File 'lib/dirty_seed/assigners/min_max_helper.rb', line 32

def floor # rubocop:disable Metrics/CyclomaticComplexity
  case type
  when :string then 1
  when :integer then @max&.negative? ? @max * 2 : 0
  when :float then @max&.negative? ? @max * 2 : 0.0
  end
end

#gapInteger, Float

Defines the gap to add to min when “greater_than” or to substract to max when “less_than”

For example if type is :float and value should be greater_than 0, then the min is 0.01
  and if the value should be lesser_than 1, then the max is 0.99

Returns:



107
108
109
110
111
112
# File 'lib/dirty_seed/assigners/min_max_helper.rb', line 107

def gap
  case type
  when :integer then 1
  when :float then 0.01
  end
end

#maxInteger, Float

Returns the maximal value

Returns:



15
16
17
# File 'lib/dirty_seed/assigners/min_max_helper.rb', line 15

def max
  @max ||= define_min_and_max && @max
end

#minInteger, Float

Returns the minimal value

Returns:



9
10
11
# File 'lib/dirty_seed/assigners/min_max_helper.rb', line 9

def min
  @min ||= define_min_and_max && @min
end

#min_max_validatorObject

Returns the validator that validate min and/or max



51
52
53
# File 'lib/dirty_seed/assigners/min_max_helper.rb', line 51

def min_max_validator
  validators.find { |validator| validator.is_a? validator_class }
end

Returns a value representing the maximal acceptable value depending on type

Returns:



93
94
95
96
97
98
99
100
101
# File 'lib/dirty_seed/assigners/min_max_helper.rb', line 93

def type_related_acceptable_max
  case type
  when :string
    min_max_validator.options[:maximum] || min_max_validator.options[:is]
  when :integer, :float
    min_max_validator.options[:less_than]&.-(gap) ||
      min_max_validator.options[:less_than_or_equal_to]
  end
end

Returns a value representing the minimal acceptable value depending on type

Returns:



73
74
75
76
77
78
79
80
81
# File 'lib/dirty_seed/assigners/min_max_helper.rb', line 73

def type_related_acceptable_min
  case type
  when :string
    min_max_validator.options[:minimum] || min_max_validator.options[:is]
  when :integer, :float
    min_max_validator.options[:greater_than]&.+(gap) ||
      min_max_validator.options[:greater_than_or_equal_to]
  end
end

#validator_classObject

Returns the validator class depending on the type



56
57
58
59
60
61
# File 'lib/dirty_seed/assigners/min_max_helper.rb', line 56

def validator_class
  case type
  when :string then ActiveRecord::Validations::LengthValidator
  when :integer, :float then ActiveModel::Validations::NumericalityValidator
  end
end