lib/dry/schema/message_compiler.rb in dry-schema-0.1.1 vs lib/dry/schema/message_compiler.rb in dry-schema-0.2.0
- old
+ new
@@ -17,78 +17,67 @@
# @api private
def initialize(messages, options = {})
@messages = messages
@options = options
@full = @options.fetch(:full, false)
- @hints = @options.fetch(:hints, true)
- @locale = @options.fetch(:locale, messages.default_locale)
- @default_lookup_options = { locale: locale }
+ @locale = @options[:locale]
+ @default_lookup_options = @locale ? { locale: locale } : EMPTY_HASH
end
# @api private
def full?
@full
end
# @api private
- def hints?
- @hints
- end
-
- # @api private
def with(new_options)
return self if new_options.empty?
self.class.new(messages, options.merge(new_options))
end
# @api private
def call(ast)
- MessageSet[ast.map { |node| visit(node) }, failures: options.fetch(:failures, true)]
+ 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
# @api private
- def visit(node, *args)
- __send__(:"visit_#{node[0]}", node[1], *args)
+ def visit(node, opts = EMPTY_OPTS.dup)
+ __send__(:"visit_#{node[0]}", node[1], opts)
end
# @api private
- def visit_failure(node, opts = EMPTY_OPTS.dup)
+ def visit_failure(node, opts)
rule, other = node
visit(other, opts.(rule: rule))
end
# @api private
- def visit_hint(node, opts = EMPTY_OPTS.dup)
- if hints?
- visit(node, opts.(message_type: :hint))
- end
+ def visit_hint(node, opts)
+ nil
end
# @api private
- def visit_each(node, opts = EMPTY_OPTS.dup)
- # TODO: we can still generate a hint for elements here!
- []
- end
-
- # @api private
- def visit_not(node, opts = EMPTY_OPTS.dup)
+ def visit_not(node, opts)
visit(node, opts.(not: true))
end
# @api private
- def visit_and(node, opts = EMPTY_OPTS.dup)
+ def visit_and(node, opts)
left, right = node.map { |n| visit(n, opts) }
if right
[left, right]
else
left
end
end
# @api private
- def visit_or(node, opts = EMPTY_OPTS.dup)
+ def visit_or(node, opts)
left, right = node.map { |n| visit(n, opts) }
if [left, right].flatten.map(&:path).uniq.size == 1
Message::Or.new(left, right, -> k { messages[k, default_lookup_options] })
elsif right.is_a?(Array)
@@ -97,11 +86,18 @@
[left, right]
end
end
# @api private
- def visit_predicate(node, base_opts = EMPTY_OPTS.dup)
+ def visit_namespace(node, opts)
+ ns, rest = node
+ self.class.new(messages.namespaced(ns), options).visit(rest, opts)
+ end
+
+ # @api private
+ def visit_predicate(node, opts)
+ base_opts = opts.dup
predicate, args = node
*arg_vals, val = args.map(&:last)
tokens = message_tokens(args)
@@ -119,38 +115,41 @@
raise MissingMessageError, "message for #{predicate} was not found"
end
text = message_text(rule, template, tokens, options)
- message_class = options[:message_type] == :hint ? Hint : Message
-
- message_class[
+ message_type(options)[
predicate, path, text,
args: arg_vals,
input: input,
rule: rule || msg_opts[:name]
]
end
# @api private
- def visit_key(node, opts = EMPTY_OPTS.dup)
+ def message_type(*)
+ Message
+ end
+
+ # @api private
+ def visit_key(node, opts)
name, other = node
visit(other, opts.(path: name))
end
# @api private
- def visit_set(node, opts = EMPTY_OPTS.dup)
+ def visit_set(node, opts)
node.map { |el| visit(el, opts) }
end
# @api private
def visit_implication(node, *args)
_, right = node
visit(right, *args)
end
# @api private
- def visit_xor(node, opts = EMPTY_OPTS.dup)
+ def visit_xor(node, opts)
left, right = node
[visit(left, opts), visit(right, opts)].uniq
end
# @api private