Sha256: be5187d6a8c39b662c4c731e0cf5836f9ff38e66d8d521251188a5c90cc322bb

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 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

2 entries across 2 versions & 1 rubygems

Version Path
dry-initializer-3.0.4 lib/dry/initializer/definition.rb
dry-initializer-3.0.3 lib/dry/initializer/definition.rb