[dry-initializer] Inherit from a Poro

Hello,

I very much like dry-initializer because it matches what I do in absolutely all of my constructors : get some data and dependencies and store them. do nothing else.

Therefore I’m trying to use it everywhere and there is one situation in which I have some difficulties : Inheriting from a superclass having a specific constructor signature.

Example :

class ParentFromExernalLib
  def initialize(family_name); ...; end
end

def Child < ParentFromExternalLib
  extend Dry::Initializer

  option :family_name
  option :first_name
  option :age
end

How can I call super(family_name) and also benefit from the initializer generated from dry-initializer

I was hoping to be able to do something like :

def initialize(*args)
  family_name = args.first
  super(family_name)
  class.dry_initializer.assign(self, *args)
end

Is there anything like this allowed using public - stable api ?

I would also like to know the good solution to this.

One (ugly) solution may be:

  • create another wrapper class that would have ONLY injected args in constructor
  • have a regular method in the wrapper that will return a new instance of your class forwarding the argument from the method

Example where we call the wrapper class ChildFactory:

class ChildFactory
   extend Dry::Initializer
   
   option :family_name
   option :first_name
   option :age

   def build_instance(family_name)
        Child.new(family_name, first_name, age)
   end
end

class Child < ParentFromExternalLib
  def initialize(family_name:, first_name:, age:)
     ...
  end
end