Sha256: 2f84bb28e3527322a238a3c56ac59d1ef57778d1ff34c671d6a43fd23b3d442c

Contents?: true

Size: 1.19 KB

Versions: 9

Compression:

Stored size: 1.19 KB

Contents

module Dry::Initializer::Builders
  # @private
  class Initializer
    require_relative "signature"
    require_relative "attribute"

    def self.[](config)
      new(config).call
    end

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

    private

    def initialize(config)
      @config = config
      @definitions = config.definitions.values
    end

    def lines
      [
        undef_line,
        define_line,
        params_lines,
        options_lines,
        end_line
      ]
    end

    def undef_line
      "undef :__dry_initializer_initialize__" \
      " if private_method_defined? :__dry_initializer_initialize__"
    end

    def define_line
      "private def __dry_initializer_initialize__(#{Signature[@config]})"
    end

    def params_lines
      @definitions.reject(&:option)
                  .flat_map { |item| Attribute[item] }
                  .map { |line| "  " << line }
    end

    def options_lines
      @definitions.select(&:option)
                  .flat_map { |item| Attribute[item] }
                  .map { |line| "  " << line }
    end

    def end_line
      "end"
    end

    def private_line
      "private :__dry_initializer_initialize__"
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
dry-initializer-3.0.2 lib/dry/initializer/builders/initializer.rb
dry-initializer-3.0.1 lib/dry/initializer/builders/initializer.rb
dry-initializer-3.0.0 lib/dry/initializer/builders/initializer.rb
dry-initializer-2.5.0 lib/dry/initializer/builders/initializer.rb
dry-initializer-2.4.0 lib/dry/initializer/builders/initializer.rb
dry-initializer-2.3.0 lib/dry/initializer/builders/initializer.rb
dry-initializer-2.2.0 lib/dry/initializer/builders/initializer.rb
dry-initializer-2.1.0 lib/dry/initializer/builders/initializer.rb
dry-initializer-2.0.0 lib/dry/initializer/builders/initializer.rb