Class: Rulix::Validation

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/rulix/validation.rb

Instance Method Summary collapse

Instance Method Details

#data_remaining?(val) ⇒ Boolean

Returns:

  • (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_messagesObject



7
8
9
# File 'lib/rulix/validation.rb', line 7

def error_messages
  extract_error_messages self
end

#extract_error_messages(hash) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/rulix/validation.rb', line 11

def extract_error_messages hash
  hash.map do |key, val|
    if data_remaining?(val)
      extract_error_messages 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

Returns:

  • (Boolean)


3
4
5
# File 'lib/rulix/validation.rb', line 3

def valid?
  deep_compact.empty?
end