Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve match_data in rules #372

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/rake/task.rb
Expand Up @@ -38,6 +38,10 @@ class Task
# will be skipped unless you reenable them.
attr_reader :already_invoked

# MatchData object returned when matching the task name
# against the rule pattern.
attr_accessor :match_data

# Return task name
def to_s
name
Expand Down
10 changes: 7 additions & 3 deletions lib/rake/task_manager.rb
Expand Up @@ -152,10 +152,14 @@ def enhance_with_matching_rule(task_name, level=0)
fail Rake::RuleRecursionOverflowError,
"Rule Recursion Too Deep" if level >= 16
@rules.each do |pattern, args, extensions, order_only, block|
if pattern && pattern.match(task_name)
if pattern && (m = pattern.match(task_name))
task = attempt_rule(task_name, pattern, args, extensions, block, level)
task | order_only unless order_only.nil?
return task if task

if task
task | order_only unless order_only.nil?
task.match_data = m
return task
end
end
end
nil
Expand Down
9 changes: 9 additions & 0 deletions test/test_rake_rules.rb
Expand Up @@ -409,4 +409,13 @@ def test_works_with_chained_extensions_in_rules
assert_equal [MINFILE], @runs
end

def test_rule_match_data
rule(/^(?<timestamp>\d{14})_(?<migration_name>[a-z_]+)\.rb$/) do |t|
@runs << t.match_data[:timestamp]
@runs << t.match_data[:migration_name]
end
Task["20191015182958_create_users.rb"].invoke
assert_equal ["20191015182958", "create_users"], @runs
end

end