Rails 7 dry-struct add attribute from method

336 views Asked by At

I'm using dry-struct for non-model class Payout - it's basically structured hash response from API call.

module Customer class Payout < Dry::Struct

attribute :amount, Types::Integer
attribute :reference, Types::String
attribute :settlements, Types::Array

def fetch_settlements
  CustomerApi.fetch_settlements_by_reference(platform_merchant_id, reference)
end

end end

Is it possible to set attribute :settlements by the fetch_settlements method ? I was trying something like:

attribute :settlements, Types::Array.optional.default(feth_settlements)

but I'm getting an error of

undefined local variable or method `fetch_settlements

0

There are 0 answers