Class: Banalize::Errors

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

Overview

Errors class provides stograge of error messages while running policy checks.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Errors

Returns a new instance of Errors.



6
7
8
9
# File 'lib/banalize/errors.rb', line 6

def initialize klass
  @klass = klass
  @messages = []
end

Instance Attribute Details

#messagesObject

Returns the value of attribute messages.



11
12
13
# File 'lib/banalize/errors.rb', line 11

def messages
  @messages
end

Class Method Details

.to_s(messages = []) ⇒ Object

Convert Array of Hashes of error messages into readable form



34
35
36
37
38
39
40
# File 'lib/banalize/errors.rb', line 34

def self.to_s messages=[]
  return '' if messages.empty?

  messages.map do |err|
    "#{err[:message]}#{err[:line] ? ", on line #{err[:line]}" : ''}"
  end
end

Instance Method Details

#add(message, line = nil) ⇒ Object



13
14
15
# File 'lib/banalize/errors.rb', line 13

def add message, line=nil
  @messages << { :message => message, :line => line }
end

#any?Boolean

Retrun true if there are any errors

Returns:

  • (Boolean)


26
27
28
# File 'lib/banalize/errors.rb', line 26

def any?
  ! empty?
end

#empty?Boolean

Retrun true if there are no errors

Returns:

  • (Boolean)


19
20
21
# File 'lib/banalize/errors.rb', line 19

def empty?
  @messages.empty?
end