Class: Tidylib::ValidationErrors

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tidylib/validation_errors.rb

Constant Summary collapse

VERSION =
"0.1.2"

Instance Method Summary collapse

Constructor Details

#initializeValidationErrors

Returns a new instance of ValidationErrors.



9
10
11
# File 'lib/tidylib/validation_errors.rb', line 9

def initialize
  @errors = []
end

Instance Method Details

#<<(error) ⇒ Object

Raises:

  • (ArgumentError)


59
60
61
62
# File 'lib/tidylib/validation_errors.rb', line 59

def <<(error)
  raise ArgumentError unless error.respond_to?(:topic) && error.respond_to?(:message) && error.respond_to?(:context)
  @errors << error
end

#[](topic) ⇒ Object Also known as: on



27
28
29
30
31
32
33
# File 'lib/tidylib/validation_errors.rb', line 27

def [](topic)
  @errors.select do |error|
    error.topic == topic
  end.map do |error|
    [ error.message, error.context ]
  end
end

#add(topic, message, context = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/tidylib/validation_errors.rb', line 17

def add(topic, message, context={})
  error = OpenStruct.new(
    topic: topic,
    message: message,
    context: context
  )

  @errors << error
end

#clearObject



51
52
53
# File 'lib/tidylib/validation_errors.rb', line 51

def clear
  @errors = []
end

#countObject



55
56
57
# File 'lib/tidylib/validation_errors.rb', line 55

def count
  @errors.length
end

#each(&blk) ⇒ Object



36
37
38
39
40
# File 'lib/tidylib/validation_errors.rb', line 36

def each(&blk)
  @errors.each do |error|
    yield error.topic, error.message, error.context
  end
end

#empty?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/tidylib/validation_errors.rb', line 13

def empty?
  @errors.empty?
end

#grouped_by_topicObject



42
43
44
45
46
47
48
49
# File 'lib/tidylib/validation_errors.rb', line 42

def grouped_by_topic
  @errors.inject({}) do |grouped, error|
    grouped[error.topic] ||= []
    grouped[error.topic] << [error.message, error.context]

    grouped
  end
end