Class: Compel::Errors

Inherits:
Object
  • Object
show all
Defined in:
lib/compel/errors.rb

Instance Method Summary collapse

Constructor Details

#initializeErrors

Returns a new instance of Errors.



5
6
7
# File 'lib/compel/errors.rb', line 5

def initialize
  @errors = {}
end

Instance Method Details

#add(key, error) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/compel/errors.rb', line 9

def add(key, error)
  if error.nil? || error.empty?
    return
  end

  if error.is_a?(Compel::Errors) || error.is_a?(Hash)
    if @errors[key].nil?
      @errors[key] = {}
    end

    @errors[key].merge!(error.to_hash)
  else
    if @errors[key].nil?
      @errors[key] = []
    end

    if !error.is_a?(Array)
      error = [error]
    end

    @errors[key].concat(error)
  end

  @errors
end

#empty?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/compel/errors.rb', line 39

def empty?
  length == 0
end

#lengthObject



35
36
37
# File 'lib/compel/errors.rb', line 35

def length
  @errors.keys.length
end

#to_hashObject



43
44
45
# File 'lib/compel/errors.rb', line 43

def to_hash
  @errors
end