lib/dry/schema/message_compiler.rb in dry-schema-0.4.0 vs lib/dry/schema/message_compiler.rb in dry-schema-0.5.0
- old
+ new
@@ -26,10 +26,11 @@
DEFAULT_PREDICATE_RESOLVERS = Hash
.new(resolve_predicate).update(key?: resolve_key_predicate).freeze
EMPTY_OPTS = VisitorOpts.new
+ EMPTY_MESSAGE_SET = MessageSet.new(EMPTY_ARRAY).freeze
param :messages
option :full, default: -> { false }
option :locale, default: -> { :en }
@@ -48,15 +49,21 @@
# @api private
def with(new_options)
return self if new_options.empty?
- self.class.new(messages, options.merge(new_options))
+ updated_opts = options.merge(new_options)
+
+ return self if updated_opts.eql?(options)
+
+ self.class.new(messages, updated_opts)
end
# @api private
def call(ast)
+ return EMPTY_MESSAGE_SET if ast.empty?
+
current_messages = EMPTY_ARRAY.dup
compiled_messages = ast.map { |node| visit(node, EMPTY_OPTS.dup(current_messages)) }
MessageSet[compiled_messages, failures: options.fetch(:failures, true)]
end
@@ -121,18 +128,16 @@
options = opts.dup.update(
path: path.last, **tokens, **lookup_options(arg_vals: arg_vals, input: input)
).to_h
- template = messages[predicate, options] || raise(MissingMessageError, path)
+ template, meta = messages[predicate, options] || raise(MissingMessageError, path)
text = message_text(template, tokens, options)
- message_type(options)[
- predicate, path, text,
- args: arg_vals,
- input: input
- ]
+ message_type(options).new(
+ text: text, path: path, predicate: predicate, args: arg_vals, input: input, meta: meta
+ )
end
# @api private
def message_type(*)
Message