From 9d9d65cd9890fa584dbcffc25a2adc8d538d6db9 Mon Sep 17 00:00:00 2001 From: nepalez Date: Mon, 15 Apr 2019 23:33:03 +0300 Subject: [PATCH] Fix validation of underscored names --- lib/dry/initializer/dispatchers/build_nested_type.rb | 7 ++++--- spec/type_constraint_spec.rb | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/dry/initializer/dispatchers/build_nested_type.rb b/lib/dry/initializer/dispatchers/build_nested_type.rb index f1f6609..f05a5c7 100644 --- a/lib/dry/initializer/dispatchers/build_nested_type.rb +++ b/lib/dry/initializer/dispatchers/build_nested_type.rb @@ -13,7 +13,7 @@ module Dry::Initializer::Dispatchers::BuildNestedType # rubocop: disable Metrics/ParameterLists def call(parent:, source:, target:, type: nil, block: nil, **options) check_certainty!(source, type, block) - check_name!(target) + check_name!(target, block) type ||= build_nested_type(parent, target, block) { parent: parent, source: source, target: target, type: type, **options } end @@ -22,8 +22,8 @@ def call(parent:, source:, target:, type: nil, block: nil, **options) private def check_certainty!(source, type, block) - return unless type return unless block + return unless type raise ArgumentError, <<~MESSAGE You should define coercer of values of argument '#{source}' @@ -31,7 +31,8 @@ def check_certainty!(source, type, block) MESSAGE end - def check_name!(name) + def check_name!(name, block) + return unless block return unless name[/^_|__|_$/] raise ArgumentError, <<~MESSAGE diff --git a/spec/type_constraint_spec.rb b/spec/type_constraint_spec.rb index 8a0085a..86f4364 100644 --- a/spec/type_constraint_spec.rb +++ b/spec/type_constraint_spec.rb @@ -5,14 +5,14 @@ before do class Test::Foo extend Dry::Initializer - param :foo, proc(&:to_s), optional: true + param :__foo__, proc(&:to_s), optional: true end end subject { Test::Foo.new :foo } it "coerces a value" do - expect(subject.foo).to eq "foo" + expect(subject.__foo__).to eq "foo" end end