Sha256: 183c39ca13bd622e303ae60c2df4914a0c92aaad3c962529d56f3af5393f4966

Contents?: true

Size: 1.68 KB

Versions: 2

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module Dry
  module 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
        }.compact
      end

      def name
        @name ||= "#{option ? "option" : "parameter"} '#{source}'"
      end
      alias_method :to_s, :name
      alias_method :to_str, :name
      alias_method :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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-initializer-3.1.1 lib/dry/initializer/definition.rb
dry-initializer-3.1.0 lib/dry/initializer/definition.rb