Multiple Trash properties that use the same key from a source hash
danwa5 opened this issue · 3 comments
I'm not sure if this is a bug or not, but when a Trash class has multiple properties that use the same key in a source hash, the property translation stops working for one of the properties.
Sample Class
class Product < Hashie::Trash
property :release_date, transform_with: ->(date) { DateTime.parse(date.to_s).to_s rescue '' }
end
Here's some sample output that shows the expected behavior for a valid or invalid date.
2.6.5 :001 > product = Product.new(release_date: '2021-06-02')
=> #<Product release_date="2021-06-02T00:00:00+00:00">
2.6.5 :002 > product = Product.new(release_date: 'foobar')
=> #<Product release_date="">
If I add an additional property that uses release_date
, the property translation of release_date
stops working, but the is_new_release
property is correct.
class Product < Hashie::Trash
property :release_date, transform_with: ->(date) { DateTime.parse(date.to_s).to_s rescue '' }
property :is_new_release, from: :release_date, transform_with: ->(date) { DateTime.parse(date) >= Date.today }
end
release_date
should be formatted to be "2021-06-02T00:00:00+00:00", as in the previous example.
2.6.5 :001 > product = Product.new(release_date: '2021-06-02')
=> #<Product is_new_release=true release_date="2021-06-02">
2.6.5 :002 > product = Product.new(release_date: '2020-012-01')
=> #<Product is_new_release=false release_date="2020-012-01">
I looked through the documentation and didn't see another way to deal with a scenario like this.
This is most likely a bug. The Trash behavior is hard to reason about and hard to prove correct or incorrect.
If you're feeling up to it, a fix or test would be a great contribution!
@michaelherold - Hi, I'd like to push a feature branch with a fix for this bug, but I'm getting the following error:
@michaelherold - I figured it out. Pull request submitted.