Sha256: 7a48d4ef3c4cd4089039503d2ac0caddcf5ca59f283b8d3644c7b9d171dfcba3

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module Dry
  module Initializer
    module Builders
      # @private
      class Reader
        def self.[](definition)
          new(definition).call
        end

        def call
          lines.flatten.compact.join("\n")
        end

        private

        def initialize(definition)
          @target = definition.target
          @ivar   = definition.ivar
          @null   = definition.null
          @reader = definition.reader
        end

        def lines
          [undef_line, attribute_line, method_lines, type_line]
        end

        def undef_line
          "undef :#{@target} if method_defined?(:#{@target})" \
            "  || private_method_defined?(:#{@target})" \
            "  || protected_method_defined?(:#{@target})"
        end

        def attribute_line
          return unless @reader

          "attr_reader :#{@target}" unless @null
        end

        def method_lines
          return unless @reader
          return unless @null

          [
            "def #{@target}",
            "  #{@ivar} unless Dry::Initializer::UNDEFINED == #{@ivar}",
            "end"
          ]
        end

        def type_line
          "#{@reader} :#{@target}" if %i[private protected].include? @reader
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-initializer-3.1.1 lib/dry/initializer/builders/reader.rb