module BusinessError

Constants

VERSION

Attributes

defs[RW]

Public Instance Methods

_get_code() click to toggle source
# File lib/business_error.rb, line 82
def _get_code
  raise ArgumentError, 'Should give a code to define your business error' if (code = @code).nil?
  @code = @code < 0 ? (code - 1) : (code + 1)
  code
end
_get_group() click to toggle source

# File lib/business_error.rb, line 78
def _get_group
  @group_name || :public
end
_get_http() click to toggle source
# File lib/business_error.rb, line 88
def _get_http
  @http_status || Config.default_http_status
end
_get_locale() click to toggle source
# File lib/business_error.rb, line 92
def _get_locale
  RequestStore.store[:err_locale] ||= 'en'
end
all() click to toggle source
# File lib/business_error.rb, line 72
def all
  puts defs_tree.stringify_keys.to_yaml.gsub(' :', ' ')
end
code_start_at(code) click to toggle source
# File lib/business_error.rb, line 39
def code_start_at code
  @code = code
end
define(name, message = name.to_s.humanize, code = _get_code, http: _get_http, group: _get_group, format: @format)
Alias for: mattr_reader
define_px(name, message = '', code = _get_code, http: _get_http) click to toggle source
# File lib/business_error.rb, line 51
def define_px name, message = '', code = _get_code, http: _get_http
  group_name = name.to_s.split('_').first.to_sym
  group group_name do
    mattr_reader name, message, code, http: http, group: group_name
  end
end
format(template) click to toggle source
# File lib/business_error.rb, line 47
def format template
  @format = template
end
group(group_name = :private, code_start_at = @code, http: _get_http, format: @format, &block) click to toggle source
# File lib/business_error.rb, line 32
def group group_name = :private, code_start_at = @code, http: _get_http, format: @format, &block
  @group_name, @code, @http, @format, group_name, code_start_at, http, format =
      group_name, code_start_at, http, format, @group_name, @code, @http, @format
  instance_eval(&block)
  @group_name, @code, @http, @format = group_name, code_start_at, http, format
end
http(status_code) click to toggle source
# File lib/business_error.rb, line 43
def http status_code
  @http_status = status_code
end
inherited(subclass) click to toggle source
# File lib/business_error.rb, line 58
def inherited(subclass)
  defs_tree[self.name]&.keys&.each do |group|
    defs_tree[self.name][group].each do |name:, **_|
      # TODO: how to undef class method?
      subclass.define_singleton_method(name) { raise NoMethodError }
      subclass.define_singleton_method(name.to_s + '!') { raise NoMethodError }
    end
  end
end
mattr_reader(name, message = name.to_s.humanize, code = _get_code, http: _get_http, group: _get_group, format: @format) click to toggle source
# File lib/business_error.rb, line 12
def mattr_reader name,
                 message = name.to_s.humanize,
                 code = _get_code,
                 http: _get_http,
                 group: _get_group,
                 format: @format
  define_singleton_method(name) do |locale = _get_locale|
    msg = message.is_a?(Hash) ? (message[locale] || message[:en]) : message
    Error.new(name, msg, code, http, format)
  end

  define_singleton_method("#{name}!") { send(name).throw! }

  defs_tree[self.name] ||= { }
  (defs_tree[self.name][group] ||= [ ]) << { name: name, msg: message, code: code, http: http }
  ((@defs ||= { })[group] ||= []) << name
end
Also aliased as: define
print() click to toggle source