module DirtySeed::Assigners::MinMaxHelper
Helps with min and max validations
Public Instance Methods
Returns a value representing the maximal acceptable value @return [Numeric]
# File lib/dirty_seed/assigners/helpers/min_max_helper.rb, line 89 def acceptable_max return unless min_max_validator min_max_validator.options[:in]&.max || type_related_acceptable_max end
Returns a value representing the minimal acceptable value @return [Numeric]
# File lib/dirty_seed/assigners/helpers/min_max_helper.rb, line 70 def acceptable_min return unless min_max_validator min_max_validator.options[:in]&.min || type_related_acceptable_min end
Returns default max depending on type @return [Numeric]
# File lib/dirty_seed/assigners/helpers/min_max_helper.rb, line 46 def ceiling case type when :string then 50 when :integer then 42 when :float then 42.0 end end
Defines min and max depending on validator @return [void] @note If min and/or max have no requirements
Define them relying on default values and on each other
# File lib/dirty_seed/assigners/helpers/min_max_helper.rb, line 23 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
Returns default min depending on type and @max @return [Numeric] @example when @max is negative and equal to -42
floor #=> -84
# File lib/dirty_seed/assigners/helpers/min_max_helper.rb, line 36 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
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 + gap = 0.01 and if the value should be lesser than 1, then the max is 1 - gap = 0.99
@return [Numeric]
# File lib/dirty_seed/assigners/helpers/min_max_helper.rb, line 110 def gap case type when :integer then 1 when :float then 0.01 end end
Returns the maximal value @return [Numeric]
# File lib/dirty_seed/assigners/helpers/min_max_helper.rb, line 15 def max @max ||= define_min_and_max && @max end
Returns the minimal value @return [Numeric]
# File lib/dirty_seed/assigners/helpers/min_max_helper.rb, line 9 def min @min ||= define_min_and_max && @min end
Returns the validator that validate min and/or max @return [ActiveModel::Validations::NumericalityValidator, ActiveRecord::Validations::LengthValidator, nil]
# File lib/dirty_seed/assigners/helpers/min_max_helper.rb, line 56 def min_max_validator validators&.find { |validator| validator.is_a? validator_class } end
(see min_max_validator
)
# File lib/dirty_seed/assigners/helpers/min_max_helper.rb, line 61 def validator_class case type when :string then ActiveRecord::Validations::LengthValidator when :integer, :float then ActiveModel::Validations::NumericalityValidator end end