Module: BusinessError
- Defined in:
- lib/business_error.rb,
lib/business_error/error.rb,
lib/business_error/config.rb,
lib/business_error/version.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
'1.1.0'
Instance Attribute Summary collapse
-
#defs ⇒ Object
Returns the value of attribute defs.
Instance Method Summary collapse
- #_get_code ⇒ Object
-
#_get_group ⇒ Object
.
- #_get_http ⇒ Object
- #_get_locale ⇒ Object
- #all ⇒ Object
- #code_start_at(code) ⇒ Object
- #define_px(name, message = '', code = _get_code, http: _get_http) ⇒ Object
- #format(template) ⇒ Object
- #group(group_name = :private, code_start_at = @code, http: _get_http, format: @format, &block) ⇒ Object
- #http(status_code) ⇒ Object
- #inherited(subclass) ⇒ Object
- #mattr_reader(name, message = name.to_s.humanize, code = _get_code, http: _get_http, group: _get_group, format: @format) ⇒ Object (also: #define)
- #print ⇒ Object
Instance Attribute Details
#defs ⇒ Object
Returns the value of attribute defs.
10 11 12 |
# File 'lib/business_error.rb', line 10 def defs @defs end |
Instance Method Details
#_get_code ⇒ Object
82 83 84 85 86 |
# 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 ⇒ Object
78 79 80 |
# File 'lib/business_error.rb', line 78 def _get_group @group_name || :public end |
#_get_http ⇒ Object
88 89 90 |
# File 'lib/business_error.rb', line 88 def _get_http @http_status || Config.default_http_status end |
#_get_locale ⇒ Object
92 93 94 |
# File 'lib/business_error.rb', line 92 def _get_locale RequestStore.store[:err_locale] ||= 'en' end |
#all ⇒ Object
72 73 74 |
# File 'lib/business_error.rb', line 72 def all puts defs_tree.stringify_keys.to_yaml.gsub(' :', ' ') end |
#code_start_at(code) ⇒ Object
39 40 41 |
# File 'lib/business_error.rb', line 39 def code_start_at code @code = code end |
#define_px(name, message = '', code = _get_code, http: _get_http) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/business_error.rb', line 51 def define_px name, = '', code = _get_code, http: _get_http group_name = name.to_s.split('_').first.to_sym group group_name do mattr_reader name, , code, http: http, group: group_name end end |
#format(template) ⇒ Object
47 48 49 |
# 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) ⇒ Object
32 33 34 35 36 37 |
# 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) ⇒ Object
43 44 45 |
# File 'lib/business_error.rb', line 43 def http status_code @http_status = status_code end |
#inherited(subclass) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# 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) ⇒ Object Also known as: define
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/business_error.rb', line 12 def mattr_reader name, = name.to_s.humanize, code = _get_code, http: _get_http, group: _get_group, format: @format define_singleton_method(name) do |locale = _get_locale| msg = .is_a?(Hash) ? ([locale] || [:en]) : 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: , code: code, http: http } ((@defs ||= { })[group] ||= []) << name end |
#print ⇒ Object
68 69 70 |
# File 'lib/business_error.rb', line 68 def print puts defs_tree[self.name].stringify_keys.to_yaml.gsub(' :', ' ') end |