-
Notifications
You must be signed in to change notification settings - Fork 85
Zip plucked values with tstruct keys in the same order for pluck_to_tstruct
#563
base: master
Are you sure you want to change the base?
Zip plucked values with tstruct keys in the same order for pluck_to_tstruct
#563
Conversation
|
||
# loosely based on pluck_to_hash gem | ||
# https://github.com/girishso/pluck_to_hash/blob/master/lib/pluck_to_hash.rb | ||
keys_one = pluck_keys.size == 1 | ||
pluck(*pluck_keys).map do |row| | ||
row = [row] if keys_one | ||
value = Hash[map_nil_values_to_default(tstruct_props, tstruct_keys.zip(row))] | ||
value = Hash[map_nil_values_to_default(tstruct_props, tstruct_keys_in_pluck_order.zip(row))] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When we zip the tstruct keys with the plucked values, we want to make sure the corresponding key and value match up correctly.
This requires that our tstruct keys are in the same order as the pluck_keys
, which is why we want to subtract the associations_keys
and then append them to the end of tstruct_keys
on line 34. This matches the operations and order changes we performed for the pluck_keys
on line 33.
const :wand_wood_type, String | ||
const :house, String |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this change in the order of the tstruct props, the spec on line 138 fails without the new changes to pluck_to_tstruct.rb
.
sorbet-rails/spec/pluck_to_tstruct_spec.rb
Lines 138 to 147 in 447ecbe
context 'pluck with associations' do | |
associations = { wand_wood_type: "wands.wood_type" } | |
expected = [ | |
WizardWithWandT.new(name: "Harry Potter", house: "Gryffindor", wand_wood_type: "Holly"), | |
WizardWithWandT.new(name: "Hermione Granger", house: "Gryffindor", wand_wood_type: "Vine"), | |
] | |
it_should_behave_like 'pluck_to_tstruct with associations', WizardWithWandT, associations, expected | |
end |
Rather than the expected array, the actual array would look like:
[
WizardWithWandT.new(name: "Harry Potter", house: "Holly", wand_wood_type: "Gryffindor"),
WizardWithWandT.new(name: "Hermione Granger", house: "Vine", wand_wood_type: "Gryffindor"),
]
Codecov Report
@@ Coverage Diff @@
## master #563 +/- ##
==========================================
- Coverage 97.48% 97.44% -0.04%
==========================================
Files 115 115
Lines 2978 2979 +1
==========================================
Hits 2903 2903
- Misses 75 76 +1
... and 1 file with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
Fixes #562