The problem: a Person should be an OpenStruct-like Entity, with the
Entity being taking care of the Person’s OpenStruct-ness and persistence
(say, for starters, the Person being added to a pool of Entities).
Tentative approach with inheritance:
require ‘ostruct’
class Entity < OpenStruct
def initialize *args
puts ‘Entity#initialize’
super
puts “adding #{self} to pool”
end
end
class Person < Entity
def initialize *args
puts ‘Person#initialize’
super
end
end
Person.new name_prefix: ‘Dr’, given_names: [‘Horace’], surname:
‘Worblehat’
Result:
Person#initialize
Entity#initialize
adding #<Person name_prefix=“Dr”, given_names=[“Horace”],
surname=“Worblehat”> to pool
The problem: how about making Entity a module so not to
require Person to inherit from it. Something like the below:
require ‘ostruct’
module Entity
def initialize *args
puts ‘Entity#initialize’
super
puts “adding #{self} to pool”
end
end
class Person < OpenStruct
include Entity
def initialize *args
puts ‘Person#initialize’
super
end
end
Person.new name_prefix: ‘Dr’, given_names: [‘Horace’], surname:
‘Worblehat’
Result:
Person#initialize
Entity#initialize
adding #<Person name_prefix=“Dr”, given_names=[“Horace”],
surname=“Worblehat”> to pool
The real problem: how to move the OpenStruct-ness of Entities away from
Person and into the Entity? You can’t include/extend Entity into/with
OpenStruct (because OpenStruct is not a module) and you can’t make
Entity inherit from OpenStruct (because Entity is a module).
I’m fine with, say, any Entity.new + Entity.included
(not-really-)magic required or some such, I just can’t
seem to be able to put my finger on the needed wiring…
(A cherry on the top would be if Person#initialize did not
have to remember to call super; I want Person to be as simple
as possible and offload as much as possible to Entity.)
— Piotr S.