1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-07 00:58:02 +00:00

Add Specify Dependencies For A Rake Task as a Ruby TIL

This commit is contained in:
jbranchaud
2022-05-31 11:37:06 -05:00
parent 0715b7099f
commit 61d148f0d8
2 changed files with 39 additions and 1 deletions

View File

@@ -0,0 +1,37 @@
# Specify Dependencies For A Rake Task
Let's say you have rake task that performs some important business task. For
instance, a book seller might want a task that can tell them the top selling
books from a CSV report.
```ruby
task :top_selling_books do
# read in CSV and process for top selling books
end
```
This works great if the CSV file already exists on the machine from which this
task is run. What if it isn't tho?
The CSV is a prerequesite for this task. We explicitly define it as a
prerequisite using the task dependency syntax.
```ruby
task :download_latest_book_sales_csv do
# saves a CSV of book sales
end
task top_selling_books: :download_latest_book_sales_csv do
# read in CSV and process for top selling books
end
```
We can even define multiple task dependencies with an array.
```ruby
task top_selling_books: [:download_latest_book_sales_csv, :clean_csv] do
# read in CSV and process for top selling books
end
```
[source](https://subscription.packtpub.com/book/hardware_and_creative/9781783280773/1/ch01lvl1sec13/task-dependencies-prerequisites)