module Dry::Initializer::Dispatchers::BuildNestedType
Public Instance Methods
call(parent:, source:, target:, type: nil, block: nil, **options)
click to toggle source
rubocop: disable Metrics/ParameterLists
# File lib/dry/initializer/dispatchers/build_nested_type.rb, line 18 def call(parent:, source:, target:, type: nil, block: nil, **options) check_certainty!(source, type, block) check_name!(target, block) type ||= build_nested_type(parent, target, block) {parent: parent, source: source, target: target, type: type, **options} end
Private Instance Methods
build_nested_type(parent, name, block)
click to toggle source
# File lib/dry/initializer/dispatchers/build_nested_type.rb, line 48 def build_nested_type(parent, name, block) return unless block klass_name = full_name(parent, name) build_struct(klass_name, block) end
build_struct(klass_name, block)
click to toggle source
# File lib/dry/initializer/dispatchers/build_nested_type.rb, line 59 def build_struct(klass_name, block) # rubocop: disable Security/Eval # rubocop: disable Style/DocumentDynamicEvalDefinition eval <<~RUBY, TOPLEVEL_BINDING, __FILE__, __LINE__ + 1 class #{klass_name} < Dry::Initializer::Struct end RUBY # rubocop: enable Style/DocumentDynamicEvalDefinition # rubocop: enable Security/Eval const_get(klass_name).tap { _1.class_eval(&block) } end
check_certainty!(source, type, block)
click to toggle source
rubocop: enable Metrics/ParameterLists
# File lib/dry/initializer/dispatchers/build_nested_type.rb, line 28 def check_certainty!(source, type, block) return unless block return unless type raise ArgumentError, <<~MESSAGE You should define coercer of values of argument '#{source}' either though the parameter/option, or via nested block, but not the both. MESSAGE end
check_name!(name, block)
click to toggle source
# File lib/dry/initializer/dispatchers/build_nested_type.rb, line 38 def check_name!(name, block) return unless block return unless name[/^_|__|_$/] raise ArgumentError, <<~MESSAGE The name of the argument '#{name}' cannot be used for nested struct. A proper name can use underscores _ to divide alphanumeric parts only. MESSAGE end
full_name(parent, name)
click to toggle source
# File lib/dry/initializer/dispatchers/build_nested_type.rb, line 55 def full_name(parent, name) "::#{parent.name}::#{name.to_s.split("_").compact.map(&:capitalize).join}" end