Class: Pangea::Validation::Result

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

Instance Method Summary collapse

Constructor Details

#initializeResult

Returns a new instance of Result.



22
23
24
25
26
27
# File 'lib/pangea/validation.rb', line 22

def initialize
  @errors = []
  @warnings = []
  @suggestions = []
  @finalized = false
end

Instance Method Details

#add_error(message) ⇒ Object

Raises:

  • (FrozenError)


46
47
48
49
50
# File 'lib/pangea/validation.rb', line 46

def add_error(message)
  raise FrozenError, "cannot modify finalized ValidationResult" if @finalized

  @errors << message
end

#add_suggestion(message) ⇒ Object

Raises:

  • (FrozenError)


58
59
60
61
62
# File 'lib/pangea/validation.rb', line 58

def add_suggestion(message)
  raise FrozenError, "cannot modify finalized ValidationResult" if @finalized

  @suggestions << message
end

#add_warning(message) ⇒ Object

Raises:

  • (FrozenError)


52
53
54
55
56
# File 'lib/pangea/validation.rb', line 52

def add_warning(message)
  raise FrozenError, "cannot modify finalized ValidationResult" if @finalized

  @warnings << message
end

#errorsObject

Return frozen copies so callers cannot mutate internal state



30
31
32
# File 'lib/pangea/validation.rb', line 30

def errors
  @errors.frozen? ? @errors : @errors.dup.freeze
end

#finalize!Object

Freeze the result, preventing further mutation. After finalize!, add_error/add_warning/add_suggestion will raise FrozenError.



66
67
68
69
70
71
72
# File 'lib/pangea/validation.rb', line 66

def finalize!
  @errors.freeze
  @warnings.freeze
  @suggestions.freeze
  @finalized = true
  self
end

#finalized?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/pangea/validation.rb', line 74

def finalized?
  @finalized
end

#suggestionsObject



38
39
40
# File 'lib/pangea/validation.rb', line 38

def suggestions
  @suggestions.frozen? ? @suggestions : @suggestions.dup.freeze
end

#to_sObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/pangea/validation.rb', line 78

def to_s
  output = []

  errs = @errors
  warns = @warnings
  suggs = @suggestions

  if errs.any?
    output << "Errors:"
    errs.each { |e| output << "  - #{e}" }
  end

  if warns.any?
    output << "\nWarnings:"
    warns.each { |w| output << "  - #{w}" }
  end

  if suggs.any?
    output << "\nSuggestions:"
    suggs.each { |s| output << "  - #{s}" }
  end

  output.join("\n")
end

#valid?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/pangea/validation.rb', line 42

def valid?
  @errors.empty?
end

#warningsObject



34
35
36
# File 'lib/pangea/validation.rb', line 34

def warnings
  @warnings.frozen? ? @warnings : @warnings.dup.freeze
end