Class: Sequent::Core::Helpers::ValueValidators
- Inherits:
-
Object
- Object
- Sequent::Core::Helpers::ValueValidators
- Defined in:
- lib/sequent/core/helpers/value_validators.rb
Constant Summary collapse
- INVALID_STRING_CHARS =
[ "\u0000", ].freeze
- VALIDATORS =
{ ::Symbol => ->(_) { true }, ::String => ->(value) { valid_string?(value) }, ::Integer => ->(value) { valid_integer?(value) }, ::Boolean => ->(value) { valid_bool?(value) }, ::Date => ->(value) { valid_date?(value) }, ::Time => ->(value) { valid_time?(value) }, ::DateTime => ->(value) { valid_date_time?(value) }, }.freeze
Class Method Summary collapse
- .for(klass) ⇒ Object
- .valid_bool?(value) ⇒ Boolean
- .valid_date?(value) ⇒ Boolean
- .valid_date_time?(value) ⇒ Boolean
- .valid_integer?(value) ⇒ Boolean
- .valid_string?(value) ⇒ Boolean
- .valid_time?(value) ⇒ Boolean
Instance Method Summary collapse
-
#initialize(klass) ⇒ ValueValidators
constructor
A new instance of ValueValidators.
- #valid_value?(value) ⇒ Boolean
Constructor Details
#initialize(klass) ⇒ ValueValidators
Returns a new instance of ValueValidators.
79 80 81 |
# File 'lib/sequent/core/helpers/value_validators.rb', line 79 def initialize(klass) @klass = klass end |
Class Method Details
.for(klass) ⇒ Object
75 76 77 |
# File 'lib/sequent/core/helpers/value_validators.rb', line 75 def self.for(klass) new(klass) end |
.valid_bool?(value) ⇒ Boolean
29 30 31 32 33 |
# File 'lib/sequent/core/helpers/value_validators.rb', line 29 def self.valid_bool?(value) return true if value.blank? value.is_a?(TrueClass) || value.is_a?(FalseClass) || value == 'true' || value == 'false' end |
.valid_date?(value) ⇒ Boolean
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/sequent/core/helpers/value_validators.rb', line 35 def self.valid_date?(value) return true if value.blank? return true if value.is_a?(Date) return false unless value =~ /\d{4}-\d{2}-\d{2}/ begin !!Date.iso8601(value) rescue StandardError false end end |
.valid_date_time?(value) ⇒ Boolean
47 48 49 50 51 52 53 54 55 |
# File 'lib/sequent/core/helpers/value_validators.rb', line 47 def self.valid_date_time?(value) return true if value.blank? begin value.is_a?(DateTime) || !!DateTime.iso8601(value.dup) rescue StandardError false end end |
.valid_integer?(value) ⇒ Boolean
23 24 25 26 27 |
# File 'lib/sequent/core/helpers/value_validators.rb', line 23 def self.valid_integer?(value) value.blank? || Integer(value) rescue StandardError false end |
.valid_string?(value) ⇒ Boolean
67 68 69 70 71 72 73 |
# File 'lib/sequent/core/helpers/value_validators.rb', line 67 def self.valid_string?(value) return true if value.nil? value.to_s && INVALID_STRING_CHARS.none? { |invalid_char| value.to_s.include?(invalid_char) } rescue StandardError false end |
Instance Method Details
#valid_value?(value) ⇒ Boolean
83 84 85 |
# File 'lib/sequent/core/helpers/value_validators.rb', line 83 def valid_value?(value) VALIDATORS[@klass].call(value) end |