Class: ResultVault
- Inherits:
-
Object
- Object
- ResultVault
- Defined in:
- lib/result_vault.rb,
lib/result_vault/version.rb
Overview
A mechanism for returning a multi-factor result from any object call.
Constant Summary collapse
- VERSION =
'1.4.1'
Instance Method Summary collapse
-
#data ⇒ Object
Return a frozen copy of the stoted data.
-
#data= ⇒ Object
Raise an exception if the user is setting data directly.
-
#error_message ⇒ String
Resturns the stored error message or an empty string.
-
#error_message=(err_msg) ⇒ Object
Sets the error message.
-
#exception ⇒ Object
Returns the stored exception.
-
#exception=(xcptn) ⇒ Object
Sets the stored exception.
-
#methods(regular = true) ⇒ Object
Return an array of method names.
-
#respond_to?(name) ⇒ Boolean
Returns true if the vaul responds to the given methods.
-
#status ⇒ Symbol, ...
Resturns the status or nil.
-
#status=(new_status) ⇒ Object
Sets the status.
-
#success=(val) ⇒ Object
(also: #ok=, #good=, #pass=, #passed=, #succeeded=)
Set success to true.
-
#success? ⇒ boolean
(also: #ok?, #good?, #pass?, #passed?, #succeeded?)
Returns the success value.
-
#update(**args) ⇒ Object
Set multiple data in a single call.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *arguments, &block) ⇒ Object (private)
Test if an ancestor class has the method, otherwise test if it’s a vault dynamic method
287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/result_vault.rb', line 287 def method_missing(method_sym, *arguments, &block) no_method_error = nil catch :check_ancestors do super rescue => e no_method_error = e throw :check_ancestors end _dynamic_method_missing(method_sym, arguments.first, no_method_error) end |
Instance Method Details
#data ⇒ Object
Return a frozen copy of the stoted data
144 145 146 147 148 149 150 |
# File 'lib/result_vault.rb', line 144 def data result = @data.dup result[:success] = success? result[:status] = status result[:error_message] = result.freeze end |
#data= ⇒ Object
Raise an exception if the user is setting data directly
134 135 136 |
# File 'lib/result_vault.rb', line 134 def data=(*) fail ArgumentError, "use :update to set or update results." end |
#error_message ⇒ String
Resturns the stored error message or an empty string
97 98 99 |
# File 'lib/result_vault.rb', line 97 def @error_message end |
#error_message=(err_msg) ⇒ Object
Sets the error message
108 109 110 |
# File 'lib/result_vault.rb', line 108 def (err_msg) _update_vault(:error_message, err_msg) end |
#exception ⇒ Object
Returns the stored exception
76 77 78 |
# File 'lib/result_vault.rb', line 76 def exception @exception end |
#exception=(xcptn) ⇒ Object
Sets the stored exception
87 88 89 |
# File 'lib/result_vault.rb', line 87 def exception=(xcptn) _update_vault(:exception, xcptn) end |
#methods(regular = true) ⇒ Object
Return an array of method names
158 159 160 161 162 163 164 |
# File 'lib/result_vault.rb', line 158 def methods(regular = true) method_names = super(regular) method_names += _data_method_names method_names -= _excluded_method_names return method_names end |
#respond_to?(name) ⇒ Boolean
Returns true if the vaul responds to the given methods
171 172 173 174 175 |
# File 'lib/result_vault.rb', line 171 def respond_to?(name) return false if _excluded_method_names.include?(name.to_sym) return true if _data_method_names.include?(name.to_sym) super end |
#status ⇒ Symbol, ...
Resturns the status or nil
55 56 57 |
# File 'lib/result_vault.rb', line 55 def status @status ||= nil end |
#status=(new_status) ⇒ Object
Sets the status
66 67 68 |
# File 'lib/result_vault.rb', line 66 def status=(new_status) _update_vault(:status, new_status) end |
#success=(val) ⇒ Object Also known as: ok=, good=, pass=, passed=, succeeded=
Set success to true
40 41 42 |
# File 'lib/result_vault.rb', line 40 def success=(val) _update_vault(:success, val) end |
#success? ⇒ boolean Also known as: ok?, good?, pass?, passed?, succeeded?
Returns the success value
27 28 29 |
# File 'lib/result_vault.rb', line 27 def success? !!@success end |
#update(**args) ⇒ Object
Set multiple data in a single call
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/result_vault.rb', line 118 def update(**args) args.each do |k, v| if k.is_a?(Symbol) send("#{k.downcase}=", v) else fail ArgumentError, ":update argument key not a Symbol: '#{k}'." end end return self end |