lib/dry/schema/step.rb in dry-schema-1.6.2 vs lib/dry/schema/step.rb in dry-schema-1.7.0

- old
+ new

@@ -15,56 +15,37 @@ # @api private attr_reader :executor # @api private - class Scoped - # @api private - attr_reader :path + attr_reader :path - # @api private - attr_reader :step - - # @api private - def initialize(path, step) - @path = Path[path] - @step = step - end - - # @api private - def scoped(new_path) - self.class.new(Path[[*new_path, *path]], step) - end - - # @api private - def call(result) - result.at(path) do |scoped_result| - output = step.(scoped_result).to_h - target = Array(path)[0..-2].reduce(result) { |a, e| a[e] } - - target.update(path.last => output) - end - end - end - # @api private - def initialize(type:, name:, executor:) + def initialize(type:, name:, executor:, path: Path::EMPTY) @type = type @name = name @executor = executor + @path = path validate_name(name) end # @api private def call(result) - output = executor.(result) - result.replace(output) if output.is_a?(Hash) + scoped_result = path.equal?(Path::EMPTY) ? result : result.at(path) + + output = executor.(scoped_result) + scoped_result.replace(output) if output.is_a?(Hash) output end # @api private - def scoped(path) - Scoped.new(path, self) + def scoped(parent_path) + self.class.new( + type: type, + name: name, + executor: executor, + path: Path.new([*parent_path, *path]) + ) end private # @api private