Class: ResultVault

Inherits:
Object
  • Object
show all
Defined in:
lib/result_vault.rb,
lib/result_vault/version.rb

Overview

A mechanism for returning a multi-factor result from any object call.

Examples:

result = ResultVault.new(error_message: "missing user", user_id: 123)
result.user_name = 'Test User'

result.user_id        #=> 123
result.user_name      #=> 'Test User'
result.error_message  #=> "missing user"

Constant Summary collapse

VERSION =
'1.4.1'

Instance Method Summary collapse

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

Parameters:

  • method_sym (Symbol)

    the name of the method

  • arguments (Array)

    the arguments for the 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

#dataObject

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]  = 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_messageString

Resturns the stored error message or an empty string

Returns:

  • (String)


97
98
99
# File 'lib/result_vault.rb', line 97

def error_message
  @error_message
end

#error_message=(err_msg) ⇒ Object

Sets the error message

Parameters:

  • err_msg (String)

    a description of the error



108
109
110
# File 'lib/result_vault.rb', line 108

def error_message=(err_msg)
  _update_vault(:error_message, err_msg)
end

#exceptionObject

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

Parameters:

  • xcptn (Exception)

    an instance of the Exception class



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

Parameters:

  • (Symbol, String)

Returns:

  • (Boolean)


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

#statusSymbol, ...

Resturns the status or nil

Returns:

  • (Symbol, String, *, nil)


55
56
57
# File 'lib/result_vault.rb', line 55

def status
  @status ||= nil
end

#status=(new_status) ⇒ Object

Sets the status

Parameters:

  • new_status (*)

    a description of the error



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

Returns:

  • (boolean)

    success



27
28
29
# File 'lib/result_vault.rb', line 27

def success?
  !!@success
end

#update(**args) ⇒ Object

Set multiple data in a single call

Parameters:

  • args (Hash)


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