Class: Rulix::Validation
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Rulix::Validation
- Defined in:
- lib/rulix/validation.rb
Instance Method Summary collapse
- #data_remaining?(val) ⇒ Boolean
- #error_messages ⇒ Object
- #extract_error_messages(hash) ⇒ Object
- #format_val(key, val) ⇒ Object
- #valid? ⇒ Boolean
Instance Method Details
#data_remaining?(val) ⇒ Boolean
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rulix/validation.rb', line 21 def data_remaining? val # If val is a hash and any of its children are hashes, # or all of them are arrays # we assume that we haven't yet reached the deepest point # of the result set val.is_a?(Hash) && ( val.values.any? { |v| v.is_a?(Hash)} || val.values.all? { |v| v.is_a?(Array)} ) end |
#error_messages ⇒ Object
7 8 9 |
# File 'lib/rulix/validation.rb', line 7 def self end |
#extract_error_messages(hash) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/rulix/validation.rb', line 11 def hash hash.map do |key, val| if data_remaining?(val) val else format_val(key, val) end end.flatten end |
#format_val(key, val) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/rulix/validation.rb', line 33 def format_val key, val if val.is_a?(Array) && val.all? { |v| v.is_a?(String) } val.map do |v| "#{key.to_s.capitalize} #{v}" end else val end end |
#valid? ⇒ Boolean
3 4 5 |
# File 'lib/rulix/validation.rb', line 3 def valid? deep_compact.empty? end |