Module: Julewire::Core::Validation
- Defined in:
- lib/julewire/core/validation.rb
Class Method Summary collapse
- .valid_integer_limit?(value, positive:) ⇒ Boolean
- .valid_numeric_timeout?(timeout) ⇒ Boolean
- .validate_byte_limit!(value, name:) ⇒ Object
- .validate_callable!(value, name:, allow_nil: false) ⇒ Object
- .validate_integer_limit!(value, name:, positive: false) ⇒ Object
- .validate_non_negative_integer!(value, name:) ⇒ Object
- .validate_options!(options, allowed_keys, name:) ⇒ Object
- .validate_symbol_choice!(value, name:, choices:) ⇒ Object
- .validate_timeout!(timeout, name:) ⇒ Object
Class Method Details
.valid_integer_limit?(value, positive:) ⇒ Boolean
60 61 62 |
# File 'lib/julewire/core/validation.rb', line 60 def valid_integer_limit?(value, positive:) positive ? value.positive? : value >= 0 end |
.valid_numeric_timeout?(timeout) ⇒ Boolean
54 55 56 57 58 |
# File 'lib/julewire/core/validation.rb', line 54 def valid_numeric_timeout?(timeout) timeout.is_a?(Numeric) && timeout.finite? && timeout >= 0 rescue StandardError false end |
.validate_byte_limit!(value, name:) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/julewire/core/validation.rb', line 8 def validate_byte_limit!(value, name:) return if value.nil? return value if value.instance_of?(Integer) && value.positive? raise ArgumentError, "#{name} must be nil or a positive Integer" end |
.validate_callable!(value, name:, allow_nil: false) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/julewire/core/validation.rb', line 26 def validate_callable!(value, name:, allow_nil: false) return if allow_nil && value.nil? return if value.respond_to?(:call) raise ArgumentError, "#{name} must respond to #call" end |
.validate_integer_limit!(value, name:, positive: false) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/julewire/core/validation.rb', line 19 def validate_integer_limit!(value, name:, positive: false) return value if value.instance_of?(Integer) && valid_integer_limit?(value, positive: positive) qualifier = positive ? "positive" : "non-negative" raise ArgumentError, "#{name} must be a #{qualifier} Integer" end |
.validate_non_negative_integer!(value, name:) ⇒ Object
15 16 17 |
# File 'lib/julewire/core/validation.rb', line 15 def validate_non_negative_integer!(value, name:) validate_integer_limit!(value, name: name) end |
.validate_options!(options, allowed_keys, name:) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/julewire/core/validation.rb', line 33 def (, allowed_keys, name:) = .keys - allowed_keys return if .empty? raise ArgumentError, "unknown #{name} options: #{.join(", ")}" end |
.validate_symbol_choice!(value, name:, choices:) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/julewire/core/validation.rb', line 40 def validate_symbol_choice!(value, name:, choices:) choice = value.to_sym if value.respond_to?(:to_sym) return choice if choices.include?(choice) raise ArgumentError, "#{name} must be one of: #{choices.join(", ")}" end |
.validate_timeout!(timeout, name:) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/julewire/core/validation.rb', line 47 def validate_timeout!(timeout, name:) return if timeout.nil? return if valid_numeric_timeout?(timeout) raise ArgumentError, "#{name} must be nil or a non-negative finite Numeric" end |