class ApiService::ErrorManager

Public Class Methods

new() click to toggle source
# File lib/api_service/error_manager.rb, line 3
def initialize
  @errors = {}
end

Public Instance Methods

add(field, message, scope = '') click to toggle source
# File lib/api_service/error_manager.rb, line 11
def add(field, message, scope = '')
  in_scope(scope) do |errors|
    field = field.to_sym
    errors[field] ||= []
    errors[field] << message
    errors
  end
end
any?() click to toggle source
# File lib/api_service/error_manager.rb, line 7
def any?
  @errors.any?
end
from_model(model) click to toggle source
# File lib/api_service/error_manager.rb, line 24
def from_model(model)
  model.errors.messages.each do |field, errors|
    @errors[field] ||= []
    @errors[field] = @errors[field].concat(errors).uniq
  end
end
to_hash() click to toggle source
# File lib/api_service/error_manager.rb, line 20
def to_hash
  @errors
end

Private Instance Methods

in_scope(scope) { |errors| ... } click to toggle source
# File lib/api_service/error_manager.rb, line 33
def in_scope(scope)
  return unless block_given?

  keys = scope.to_s.split('.')
  @errors = yield(@errors) if keys.empty?

  keys.inject(@errors) do |hash, key|
    hash[key] ||= {}
    yield(hash[key]) if key == keys.last
    hash[key]
  end
end