Sha256: 88385383713270b748268c16e20255813d45e21abb3b209c81a5bdbb3c432db8

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

module Dry::Initializer::Plugins
  # Base class for plugins
  #
  # A plugin should has class method [.call] that takes argument name and
  # settings and return a chunk of code for the #initialize method body.
  #
  class Base
    include Dry::Initializer::Errors

    # Builds the proc for the `__after_initializer__` callback
    #
    # @param [#to_s] name
    # @param [Hash<Symbol, Object>] settings
    #
    # @return [String, Proc, nil]
    #
    def self.call(name, settings)
      new(name, settings).call
    end

    # @private
    attr_reader :name, :settings

    # Initializes a builder with argument name and settings
    # @param (see .call)
    def initialize(name, settings)
      @name     = name
      @settings = settings
    end

    # Checks equality to another instance by name
    # @return [Boolean]
    def ==(other)
      other.instance_of?(self.class) && (other.name == name)
    end

    # Builds a chunk of code
    # @return (see .call)
    def call
    end

    # Returns the name for the attribute
    # @return (see .name)
    def rename
      @rename ||= settings[:option] ? (settings[:as] || name) : name
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-initializer-0.10.0 lib/dry/initializer/plugins/base.rb
dry-initializer-0.9.3 lib/dry/initializer/plugins/base.rb