model icon indicating copy to clipboard operation
model copied to clipboard

#to_hash and implicit conversion

Open antstorm opened this issue 5 years ago • 4 comments

Something that caught me off guard when using an instance of Hanami::Entity with Ruby's double splat:

class User < Hanami::Entity
  attributes do
    attribute :id,   Types::Int
    attribute :name, Types::String
  end
end

def test_me(user = nil, **options)
  puts "user: #{user}"
  puts "options: #{options}"
end

> test_me(User.new(id: 42, name: 'Tester'))
user:
options: {:id=>:42,:name=>'Tester'}

Due to implicit conversion (#to_hash) User instance gets deconstructed into a hash when passed into the test method. The behaviour was introduce in this PR — https://github.com/hanami/model/commit/26c17ebca4a52d11f3169e4e9af184af05375c33.

My question is — was it intentional and there's a legit use-case or accidental?

antstorm avatar Sep 23 '20 22:09 antstorm

This will be addressed in Hanami 2.0. This method will be gone as it has confusing behaviors like you noticed.

solnic avatar Sep 24 '20 08:09 solnic

This is great, thank you, @solnic!

Do you have a timeline in mind for Hanami 2.0? I know things have been a bit stale recently

antstorm avatar Sep 24 '20 11:09 antstorm

@antstorm we'll be posting an official roadmap/timeline very soon! I recommend following Tim's updates on his blog too. You can also see our Trello board to get a sense of where we are in the process.

solnic avatar Sep 25 '20 09:09 solnic

@solnic ah, lovely, great to see there's some progress on the project. I really enjoy Hanami and have been running it in production for 1.5 years now

antstorm avatar Sep 27 '20 21:09 antstorm