Hi there,
I’m trying to factorize and reuse a list of common attributes in a Dry::Struct.
As a simplified example, consider the following StructA and StructB:
require 'dry-struct'
module Types
include Dry::Types.module
end
class StructA < Dry::Struct
attribute :attr_a, Types::String
attribute :uuid, Types::String
end
class StructB < Dry::Struct
attribute :attr_b, Types::String
attribute :uuid, Types::String
end
The uuid attribute is common to both structures. I’d like to make its definition reusable.
Inheritance works, ie. :
require 'dry-struct'
module Types
include Dry::Types.module
end
class CommonStruct < Dry::Struct
attribute :uuid, Types::String
end
class StructA < CommonStruct
attribute :attr_a, Types::String
end
class StructB < CommonStruct
attribute :attr_b, Types::String
end
But I’d like to mixin the CommonStruct attributes instead of relying on inheritance, ie. something like this:
class StructA < Dry::Struct
include CommonStruct
attribute :attr_a, Types::String
end
class StructB < Dry::Struct
include CommonStruct
attribute :attr_b, Types::String
end
How should I write the CommonStruct to do so?
Thanks for your help!
Vincent