I’ve been thinking about using Dry::Struct
for entities, but they must be mutable.
To set the attributes, post-initialization, from within the object I can use @attributes[:key] = 'whatever'
but I’m guessing this isn’t part of the public API, so subject to breaking in future releases.
This is an example of what I want to do:
class Issue < Dry::Struct
include Types
attribute :title, String
attribute? :resolved_at, HappenedAt
attribute? :resolved_by, UserId
def resolve(by:, at:)
@attributes[:resolved_at] = at
@attributes[:resolved_by] = by.id
self
end
def resolved?
!resolved_at.nil?
end
end
issue = Issue.new(title: 'Shipment Delayed')
issue.resolve(by: current_user, at: Time.now )
Maybe this is outside the use case for dry-struct?