Module: LunaPark::Extensions::HasErrors::ClassMethods
- Defined in:
- lib/luna_park/extensions/has_errors.rb
Instance Method Summary collapse
-
#business_error(title, txt = nil, i18n_key: nil, i18n: nil, notify: nil, &default_message_block) ⇒ Object
(also: #error)
Define business error.
-
#custom_error(title, inherit_from, txt = nil, i18n_key: nil, i18n: nil, notify: nil, &default_message_block) ⇒ Object
Define error with a custom superclass.
-
#error_class_name(title) ⇒ Object
Get error class name.
-
#system_error(title, txt = nil, i18n_key: nil, i18n: nil, notify: nil, &default_message_block) ⇒ Object
Define business error.
Instance Method Details
#business_error(title, txt = nil, i18n_key: nil, i18n: nil, notify: nil, &default_message_block) ⇒ Object Also known as: error
Define business error
74 75 76 |
# File 'lib/luna_park/extensions/has_errors.rb', line 74 def business_error(title, txt = nil, i18n_key: nil, i18n: nil, notify: nil, &) # rubocop:disable Metrics/ParameterLists custom_error title, Errors::Business, txt, i18n: i18n || i18n_key, notify: notify, & end |
#custom_error(title, inherit_from, txt = nil, i18n_key: nil, i18n: nil, notify: nil, &default_message_block) ⇒ Object
Define error with a custom superclass. The superclass must be inherited from LunaPark::Errors::Base.
rubocop:disable Metrics/ParameterLists
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/luna_park/extensions/has_errors.rb', line 118 def custom_error(title, inherit_from, txt = nil, i18n_key: nil, i18n: nil, notify: nil, &) unless inherit_from < Errors::Base raise ArgumentError, 'inherit_from must be a superclass of LunaPark::Errors::Base' end error_class = Class.new(inherit_from) error_class.inherited(inherit_from) error_class.notify(notify) unless notify.nil? = ![txt, i18n || i18n_key, ].all?(&:nil?) error_class.(txt, i18n: i18n || i18n_key, &) if const_set(error_class_name(title), error_class) end |
#error_class_name(title) ⇒ Object
Get error class name
144 145 146 147 148 149 150 |
# File 'lib/luna_park/extensions/has_errors.rb', line 144 def error_class_name(title) case title when String then title when Symbol then title.to_s.split('_').collect!(&:capitalize).join else raise ArgumentError, "Unknown type `#{title}` for error title" end end |
#system_error(title, txt = nil, i18n_key: nil, i18n: nil, notify: nil, &default_message_block) ⇒ Object
Define business error
95 96 97 |
# File 'lib/luna_park/extensions/has_errors.rb', line 95 def system_error(title, txt = nil, i18n_key: nil, i18n: nil, notify: nil, &) # rubocop:disable Metrics/ParameterLists custom_error title, Errors::System, txt, i18n: i18n || i18n_key, notify: notify, & end |