Class: Pangea::Validation::Result
Instance Method Summary collapse
- #add_error(message) ⇒ Object
- #add_suggestion(message) ⇒ Object
- #add_warning(message) ⇒ Object
-
#errors ⇒ Object
Return frozen copies so callers cannot mutate internal state.
-
#finalize! ⇒ Object
Freeze the result, preventing further mutation.
- #finalized? ⇒ Boolean
-
#initialize ⇒ Result
constructor
A new instance of Result.
- #suggestions ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
- #warnings ⇒ Object
Constructor Details
#initialize ⇒ Result
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
46 47 48 49 50 |
# File 'lib/pangea/validation.rb', line 46 def add_error() raise FrozenError, "cannot modify finalized ValidationResult" if @finalized @errors << end |
#add_suggestion(message) ⇒ Object
58 59 60 61 62 |
# File 'lib/pangea/validation.rb', line 58 def add_suggestion() raise FrozenError, "cannot modify finalized ValidationResult" if @finalized @suggestions << end |
#add_warning(message) ⇒ Object
52 53 54 55 56 |
# File 'lib/pangea/validation.rb', line 52 def add_warning() raise FrozenError, "cannot modify finalized ValidationResult" if @finalized @warnings << end |
#errors ⇒ Object
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
74 75 76 |
# File 'lib/pangea/validation.rb', line 74 def finalized? @finalized end |
#suggestions ⇒ Object
38 39 40 |
# File 'lib/pangea/validation.rb', line 38 def suggestions @suggestions.frozen? ? @suggestions : @suggestions.dup.freeze end |
#to_s ⇒ Object
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
42 43 44 |
# File 'lib/pangea/validation.rb', line 42 def valid? @errors.empty? end |
#warnings ⇒ Object
34 35 36 |
# File 'lib/pangea/validation.rb', line 34 def warnings @warnings.frozen? ? @warnings : @warnings.dup.freeze end |