Class: Result
- Inherits:
-
Object
- Object
- Result
- Defined in:
- lib/result.rb
Overview
Retrieve messages for complex process #events store each state as hash, with keys :message and :type
Example
result=Result.new
result.info("Something happened")
result.success? # returns true
result.error("Something bad happened")
result.success? # returns false
result. # return 'info: Something happened\nerror:Something bad happened'
Instance Attribute Summary collapse
-
#events ⇒ Object
readonly
Returns the value of attribute events.
Instance Method Summary collapse
-
#add_result(new_result) ⇒ Object
Permite agregar los eventos de otro resultado.
-
#count ⇒ Object
Number of events.
- #error(message) ⇒ Object
- #info(message) ⇒ Object
-
#initialize ⇒ Result
constructor
A new instance of Result.
- #message ⇒ Object
- #success(message) ⇒ Object
- #success? ⇒ Boolean
- #warning(message) ⇒ Object
Constructor Details
#initialize ⇒ Result
Returns a new instance of Result.
41 42 43 44 |
# File 'lib/result.rb', line 41 def initialize() #@status=:ok @events=[] end |
Instance Attribute Details
#events ⇒ Object (readonly)
Returns the value of attribute events.
40 41 42 |
# File 'lib/result.rb', line 40 def events @events end |
Instance Method Details
#add_result(new_result) ⇒ Object
Permite agregar los eventos de otro resultado
68 69 70 |
# File 'lib/result.rb', line 68 def add_result(new_result) @events=@events+new_result.events end |
#count ⇒ Object
Number of events
46 47 48 |
# File 'lib/result.rb', line 46 def count @events.count end |
#error(message) ⇒ Object
55 56 57 |
# File 'lib/result.rb', line 55 def error() @events.push({type: :error, message: }) end |
#info(message) ⇒ Object
49 50 51 |
# File 'lib/result.rb', line 49 def info() @events.push({type: :info, message: }) end |
#message ⇒ Object
64 65 66 |
# File 'lib/result.rb', line 64 def @events.map {|v| "#{v[:type]}: #{v[:message]}"}.join("\n") end |
#success(message) ⇒ Object
52 53 54 |
# File 'lib/result.rb', line 52 def success() @events.push({type: :success, message: }) end |
#success? ⇒ Boolean
61 62 63 |
# File 'lib/result.rb', line 61 def success? !@events.any? {|v| v[:type]==:error} end |
#warning(message) ⇒ Object
58 59 60 |
# File 'lib/result.rb', line 58 def warning() @events.push({type: :warning, message: }) end |