-
-
Notifications
You must be signed in to change notification settings - Fork 752
Define derived metadata. #1496
Define derived metadata. #1496
Conversation
Ping me when you'd like a review ;) |
Ping...this is ready for review. I'm not totally satisfied with the name |
# @private | ||
def apply_derived_metadata_to(metadata) | ||
@derived_metadata_blocks.each do |filter, block| | ||
block.call(metadata) if filter.empty? || MetadataFilter.any_apply?(filter, metadata) |
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.
Why if filter.empty?
here?
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.
Because if you don't pass any filters, it should always apply:
RSpec.configure do |config|
config.define_derived_metadata do |metadata|
# this block should always run against every metadata hash
end
end
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.
Oh, to apply the block globally, I see.
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.
Figured it out as you commented there ;)
I like how simple this has turned out, how about |
Oh yeah, the other open question is the semantics of passing multiple filters to |
I think I like |
I didn't know we used I think I have a minor preference for |
I'm actually ok with |
Well, that was the necessary semantic for rspec-rails to be able to use its old way of inclusion:
I think config.define_derived_metadata(:a => 1, :b => 2) do |metadata|
if metadata.values_at(:a, :b) == [1, 2]
# mutate metadata
end
end ...but you can't as easily simulate the |
Good to merge? |
expect(ex.metadata).to include(:description => "foo", :reverse_description => "oof") | ||
end | ||
|
||
it 'allows multiple configured blocks to be applied, in order of definition' do |
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.
although, I like to be able to do this, i find the syntax somewhat confusing... it seems like the 2nd line overrides the first.
how do you see this being used?
it seems to me that it would be easier to have a single derived_metadata hash, which you can just edit...
c.update_metadata { |m| m[:b1_desc] = m[:description] + " (block 1)" }
c.update_metadata { |m| m[:b2_desc] = m[:b1_desc] + " (block 2)" }
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.
@yelled3 this is mainly being used for rspec-rails magic to prevent mutation of the hash pre runtime.
Apart from one final thought, and that's that we should now prevent the metadata hash being mutated conventionally... (Even if we just dup it where it's accessed similarly to |
Hmm...I'm on the fence about that. While it's a good idea to document the fact that we recommend not mutating metadata, consider the idea that rspec-rails has been doing so for years, and until you dug into rspec/rspec-rails#825, we had never seen any problems caused by metadata mutation....so in the vast majority of cases, it works fine. I think it's more likely to confuse to users to silently ignore any mutation they've done (which would happen if we used |
This fixes #969 and supercedes #1089.
Not ready for merge yet (or even at a point where I'd ask it to be reviewed)...I'm pushing this so I can make an rspec-rails PR that depends on this.