Skip to content

Commit

Permalink
Move code related to setting up Hint objects to the hints ext
Browse files Browse the repository at this point in the history
  • Loading branch information
solnic committed Feb 20, 2019
1 parent 1adb451 commit fa96510
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 27 deletions.
37 changes: 37 additions & 0 deletions lib/dry/schema/extensions/hints.rb
@@ -1,3 +1,4 @@
require 'dry/schema/message'
require 'dry/schema/message_compiler'

require 'dry/schema/extensions/hints/message_compiler_methods'
Expand All @@ -6,6 +7,42 @@

module Dry
module Schema
# Hint-specific Message extensions
#
# @see Message
#
# @api public
class Message
# @see Message::Or
#
# @api public
class Or
# @api private
def hint?
false
end
end

# @api private
def hint?
false
end
end

# A hint message sub-type
#
# @api private
class Hint < Message
def self.[](predicate, path, text, options)
Hint.new(predicate, path, text, options)
end

# @api private
def hint?
true
end
end

module Extensions
MessageCompiler.prepend(Hints::MessageCompilerMethods)
MessageSet.prepend(Hints::MessageSetMethods)
Expand Down
5 changes: 5 additions & 0 deletions lib/dry/schema/extensions/hints/message_compiler_methods.rb
Expand Up @@ -34,6 +34,11 @@ def filter(messages)
end
end

# @api private
def message_type(options)
options[:message_type].equal?(:hint) ? Hint : Message
end

# @api private
def visit_hint(node, opts = EMPTY_OPTS.dup)
if hints?
Expand Down
24 changes: 0 additions & 24 deletions lib/dry/schema/message.rb
Expand Up @@ -34,11 +34,6 @@ def initialize(left, right, messages)
@path = left.path
end

# @api private
def hint?
false
end

# Return a string representation of the message
#
# @api public
Expand Down Expand Up @@ -75,29 +70,10 @@ def to_s
text
end

# @api private
def hint?
false
end

# @api private
def eql?(other)
other.is_a?(String) ? text == other : super
end
end

# A hint message sub-type
#
# @api private
class Hint < Message
def self.[](predicate, path, text, options)
Hint.new(predicate, path, text, options)
end

# @api private
def hint?
true
end
end
end
end
9 changes: 6 additions & 3 deletions lib/dry/schema/message_compiler.rb
Expand Up @@ -113,16 +113,19 @@ def visit_predicate(node, base_opts = EMPTY_OPTS.dup)

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 message_type(*)
Message
end

# @api private
def visit_key(node, opts = EMPTY_OPTS.dup)
name, other = node
Expand Down
2 changes: 2 additions & 0 deletions lib/dry/schema/message_compiler/visitor_opts.rb
@@ -1,3 +1,5 @@
require 'dry/schema/message'

module Dry
module Schema
# @api private
Expand Down

0 comments on commit fa96510

Please sign in to comment.