Module: DirtySeed::Assigners::MinMaxHelper
Overview
Helps with min and max validations
Instance Method Summary collapse
-
#acceptable_max ⇒ Integer, Float
Returns a value representing the maximal acceptable value.
-
#acceptable_min ⇒ Integer, Float
Returns a value representing the minimal acceptable value.
-
#ceiling ⇒ Integer, Float
Returns default max depending on type.
-
#define_min_and_max ⇒ void
Defines min and max depending on validator.
-
#floor ⇒ Integer, Float
Returns default min depending on type.
-
#gap ⇒ Integer, 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.
-
#max ⇒ Integer, Float
Returns the maximal value.
-
#min ⇒ Integer, Float
Returns the minimal value.
-
#min_max_validator ⇒ Object
Returns the validator that validate min and/or max.
-
#type_related_acceptable_max ⇒ Integer, Float
Returns a value representing the maximal acceptable value depending on type.
-
#type_related_acceptable_min ⇒ Integer, Float
Returns a value representing the minimal acceptable value depending on type.
-
#validator_class ⇒ Object
Returns the validator class depending on the type.
Instance Method Details
#acceptable_max ⇒ Integer, Float
Returns a value representing the maximal acceptable value
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.[:in]&.max || end |
#acceptable_min ⇒ Integer, Float
Returns a value representing the minimal acceptable value
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.[:in]&.min || end |
#ceiling ⇒ Integer, Float
Returns default max depending on type
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_max ⇒ void
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 |
#floor ⇒ Integer, Float
Returns default min depending on type
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 |
#gap ⇒ Integer, 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
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 |
#max ⇒ Integer, Float
Returns the maximal value
15 16 17 |
# File 'lib/dirty_seed/assigners/min_max_helper.rb', line 15 def max @max ||= define_min_and_max && @max end |
#min ⇒ Integer, Float
Returns the minimal value
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_validator ⇒ Object
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 |
#type_related_acceptable_max ⇒ Integer, Float
Returns a value representing the maximal acceptable value depending on type
93 94 95 96 97 98 99 100 101 |
# File 'lib/dirty_seed/assigners/min_max_helper.rb', line 93 def case type when :string min_max_validator.[:maximum] || min_max_validator.[:is] when :integer, :float min_max_validator.[:less_than]&.-(gap) || min_max_validator.[:less_than_or_equal_to] end end |
#type_related_acceptable_min ⇒ Integer, Float
Returns a value representing the minimal acceptable value depending on type
73 74 75 76 77 78 79 80 81 |
# File 'lib/dirty_seed/assigners/min_max_helper.rb', line 73 def case type when :string min_max_validator.[:minimum] || min_max_validator.[:is] when :integer, :float min_max_validator.[:greater_than]&.+(gap) || min_max_validator.[:greater_than_or_equal_to] end end |
#validator_class ⇒ Object
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 |