Sha256: f6666ce1725b18d33bca298b8aa0fc8c1a094cd254884e7c194ee96ec6f1c50e

Contents?: true

Size: 799 Bytes

Versions: 3

Compression:

Stored size: 799 Bytes

Contents

module Dry::Initializer::Plugins
  # Plugin builds a code for variable setter:
  #
  # @example
  #   VariableSetter.call(:user, option: false)
  #   # => "@user = user"
  #
  #   VariableSetter.call(:user, option: true)
  #   # => "@user = __options__.fetch(:user)"
  #
  #   VariableSetter.call(:user, option: true, optional: true)
  #   # => "@user = __options__.fetch(:user, Dry::Initializer::UNDEFINED)"
  #
  class VariableSetter < Base
    def param?
      settings[:option] != true
    end

    def required?
      !settings.key?(:default) && !settings[:optional]
    end

    def call
      return "@#{name} = #{name}" if param?
      return "@#{name} = __options__.fetch(:#{name})" if required?
      "@#{name} = __options__.fetch(:#{name}, Dry::Initializer::UNDEFINED)"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-initializer-0.9.0 lib/dry/initializer/plugins/variable_setter.rb
dry-initializer-0.8.1 lib/dry/initializer/plugins/variable_setter.rb
dry-initializer-0.8.0 lib/dry/initializer/plugins/variable_setter.rb