module Gateway::Feature::CategorizeError::ClassMethods

Protected Instance Methods

categorize_error(*params, &block) click to toggle source
# File lib/gateway/feature/categorize_error.rb, line 46
def categorize_error(*params, &block)
  errors, actions, categories = extract_params(params)

  self.error_catalog ||= {}

  categories.each do |error_category|
    self.error_catalog[error_category] ||= {}
    actions.each do |action|
      self.error_catalog[error_category][action] ||= {}
      self.error_catalog[error_category][action][:errors] ||= []
      self.error_catalog[error_category][action][:errors] += errors
      self.error_catalog[error_category][action][:lambdas] ||= []
      self.error_catalog[error_category][action][:lambdas] << block if block
    end
  end
end
extract_params(params) click to toggle source
# File lib/gateway/feature/categorize_error.rb, line 63
def extract_params(params)
  opt = params.pop
  opt ||= {}
  errors = params.flatten

  actions = [opt[:for]].flatten.compact
  actions = ['all'] if actions.blank?

  # convert actions to string
  actions.map!{ |action| action.to_s }

  categories = [opt[:as]].flatten.compact
  categories = error_categories if categories.blank?

  categories.each do |error_category|
    unless error_categories.include?(error_category.to_sym)
      raise "Error Category: #{error_category} must be one of #{error_categories}."
    end
  end

  [errors, actions, categories]
end