Sha256: 8d69169c3ba9ff5ffd00a58f4b364df65518782f814ffb638e677fafc5ac703d

Contents?: true

Size: 1.12 KB

Versions: 5

Compression:

Stored size: 1.12 KB

Contents

#
# Prepares the target name of a parameter or an option.
#
# Unlike source, the target must satisfy requirements for Ruby variable names.
# It also shouldn't be in conflict with names used by the gem.
#
module Dry::Initializer::Dispatchers::PrepareTarget
  extend self

  # List of variable names reserved by the gem
  RESERVED = %i[
    __dry_initializer_options__
    __dry_initializer_config__
    __dry_initializer_value__
    __dry_initializer_definition__
    __dry_initializer_initializer__
  ].freeze

  def call(source:, target: nil, as: nil, **options)
    target ||= as || source
    target = target.to_s.to_sym.downcase

    check_ruby_name!(target)
    check_reserved_names!(target)

    { source: source, target: target, **options }
  end

  private

  def check_ruby_name!(target)
    return if target[/\A[[:alpha:]_][[:alnum:]_]*\??\z/u]

    raise ArgumentError,
          "The name `#{target}` is not allowed for Ruby methods"
  end

  def check_reserved_names!(target)
    return unless RESERVED.include?(target)

    raise ArgumentError,
          "The method name `#{target}` is reserved by the dry-initializer gem"
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dry-initializer-3.0.4 lib/dry/initializer/dispatchers/prepare_target.rb
dry-initializer-3.0.3 lib/dry/initializer/dispatchers/prepare_target.rb
dry-initializer-3.0.2 lib/dry/initializer/dispatchers/prepare_target.rb
dry-initializer-3.0.1 lib/dry/initializer/dispatchers/prepare_target.rb
dry-initializer-3.0.0 lib/dry/initializer/dispatchers/prepare_target.rb