dry-initializer main

Container Version

Instead of extending a class with the Dry::Initializer, you can include a container with the initializer method only. This method should be preferred when you don't need subclassing.

require 'dry-initializer'

class User
  # notice `-> do .. end` syntax
  include Dry::Initializer.define -> do
    param  :name,  proc(&:to_s)
    param  :role,  default: proc { 'customer' }
    option :admin, default: proc { false }
  end
end

If you still need the DSL (param and option) to be inherited, use the direct extension:

require 'dry-initializer'

class BaseService
  extend Dry::Initializer

  class << self
    alias_method :dependency, :param
  end
end

class ShowUser < BaseService
  dependency :user

  def call
    puts user&.name
  end
end

octocatEdit on GitHub