Sha256: 63e4ddf6cee6640de7ed67c81c0a78bfc62a88ef4290fcf8e6cc32e6ae90deec

Contents?: true

Size: 1.54 KB

Versions: 3

Compression:

Stored size: 1.54 KB

Contents

module Dry::Initializer
  #
  # @private
  # @abstract
  #
  # Base class for parameter or option definitions
  # Defines methods to add corresponding reader to the class,
  # and build value of instance attribute.
  #
  class Definition
    attr_reader :option, :null, :source, :target, :ivar,
                :type, :optional, :default, :reader,
                :desc

    def options
      {
        as:       target,
        type:     type,
        optional: optional,
        default:  default,
        reader:   reader,
        desc:     desc
      }.reject { |_, value| value.nil? }
    end

    def name
      @name ||= (option ? "option" : "parameter") << " '#{source}'"
    end
    alias to_s    name
    alias to_str  name
    alias inspect name

    def ==(other)
      other.instance_of?(self.class) && (other.source == source)
    end

    def code
      Builders::Reader[self]
    end

    def inch
      @inch ||= (option ? "@option" : "@param ").tap do |text|
        text << " [Object]"
        text << (option ? " :#{source}" : " #{source}")
        text << (optional ? " (optional)" : " (required)")
        text << " #{desc}" if desc
      end
    end

    private

    def initialize(**options)
      @option   = options[:option]
      @null     = options[:null]
      @source   = options[:source]
      @target   = options[:target]
      @ivar     = "@#{@target}"
      @type     = options[:type]
      @reader   = options[:reader]
      @default  = options[:default]
      @optional = options[:optional]
      @desc     = options[:desc]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-initializer-3.0.2 lib/dry/initializer/definition.rb
dry-initializer-3.0.1 lib/dry/initializer/definition.rb
dry-initializer-3.0.0 lib/dry/initializer/definition.rb