Class: ErrorNormalizer::MessageParser
- Inherits:
-
Object
- Object
- ErrorNormalizer::MessageParser
- Defined in:
- lib/error_normalizer/message_parser.rb,
lib/error_normalizer/message_parser/english.rb
Overview
Base implementation of the error message parser. Message parsers attempt to extract key, message and payload from the given message. Instances of MessageParser can’t parse errors on its own because it does not define any error matchers but it defines all necessary parse logic since it doesn’t depend on the locale.
You can easily define your own parser by inheriting from MessageParser:
class RussianMessageParser < ErrorNormalizer::MessageParser
locale :ru
value_matcher :must_be_equal_to, /(?<err>должен быть равным) (?<val>.+)/u
list_matcher :must_be_on_of, /\A(?<err>должен быть одним из): (?<val>.+)/u
end
ActiveModel ignored for now because we don’t plan to use its validations. In case message isn’t recognized we set error to be a simple normalized message (no spaces and special characters).
Here are the links to ActiveModel::Errors and Dry::Validation list of error messages:
Direct Known Subclasses
Defined Under Namespace
Classes: English
Constant Summary collapse
- AlreadyDefinedError =
Class.new(StandardError)
Class Attribute Summary collapse
-
.list_matchers ⇒ Hash
readonly
List matchers.
-
.value_matchers ⇒ Hash
readonly
Value matchers.
Instance Attribute Summary collapse
-
#locale ⇒ String
readonly
Parser locale.
Class Method Summary collapse
-
.list_matcher(key, matcher) ⇒ void
Define message list matcher with a corresponding error key.
-
.locale(i18n_locale = nil) ⇒ Symbol
Get or set parser locale.
-
.value_matcher(key, matcher) ⇒ void
Define message value matcher with a corresponding error key.
Instance Method Summary collapse
-
#initialize(message) ⇒ MessageParser
constructor
A new instance of MessageParser.
-
#parse ⇒ Array
Parse error message.
-
#to_a ⇒ Array
A tuple of parsed [key, message, payload].
Constructor Details
#initialize(message) ⇒ MessageParser
Returns a new instance of MessageParser.
79 80 81 82 83 84 |
# File 'lib/error_normalizer/message_parser.rb', line 79 def initialize() @locale = self.class.locale @message = @key = nil @payload = {} end |
Class Attribute Details
.list_matchers ⇒ Hash (readonly)
Returns list matchers.
76 77 78 |
# File 'lib/error_normalizer/message_parser.rb', line 76 def list_matchers @list_matchers end |
.value_matchers ⇒ Hash (readonly)
Returns value matchers.
73 74 75 |
# File 'lib/error_normalizer/message_parser.rb', line 73 def value_matchers @value_matchers end |
Instance Attribute Details
#locale ⇒ String (readonly)
Returns parser locale.
87 88 89 |
# File 'lib/error_normalizer/message_parser.rb', line 87 def locale @locale end |
Class Method Details
.list_matcher(key, matcher) ⇒ void
This method returns an undefined value.
Define message list matcher with a corresponding error key. List matchers add a “list” or “range” property to the error payload.
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/error_normalizer/message_parser.rb', line 61 def list_matcher(key, matcher) raise ArgumentError, 'matcher should be a Regexp' unless matcher.is_a?(Regexp) key = key.to_s @list_matchers ||= {} raise AlreadyDefinedError if @list_matchers.key?(key) @list_matchers[key] = matcher end |
.locale(i18n_locale = nil) ⇒ Symbol
Get or set parser locale
34 35 36 37 38 |
# File 'lib/error_normalizer/message_parser.rb', line 34 def locale(i18n_locale = nil) return @locale if i18n_locale.nil? @locale = i18n_locale.intern end |
.value_matcher(key, matcher) ⇒ void
This method returns an undefined value.
Define message value matcher with a corresponding error key. Value matchers add a “value” property to the error payload.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/error_normalizer/message_parser.rb', line 45 def value_matcher(key, matcher) raise ArgumentError, 'matcher should be a Regexp' unless matcher.is_a?(Regexp) key = key.to_s @value_matchers ||= {} raise AlreadyDefinedError if @value_matchers.key?(key) @value_matchers[key] = matcher end |
Instance Method Details
#parse ⇒ Array
Parse error message
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/error_normalizer/message_parser.rb', line 91 def parse return to_a if @key return to_a if @key @key = (@message) to_a end |
#to_a ⇒ Array
Returns a tuple of parsed [key, message, payload].
103 104 105 |
# File 'lib/error_normalizer/message_parser.rb', line 103 def to_a [@key, @message, @payload] end |