{"id":15406470,"url":"https://github.com/odlp/oops_a_rake","last_synced_at":"2025-04-17T12:30:09.807Z","repository":{"id":55532018,"uuid":"309977799","full_name":"odlp/oops_a_rake","owner":"odlp","description":"Write your Rake tasks as plain-old Ruby objects","archived":false,"fork":false,"pushed_at":"2021-02-11T13:06:38.000Z","size":22,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-22T12:19:14.472Z","etag":null,"topics":["rake"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/odlp.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-11-04T11:06:37.000Z","updated_at":"2021-11-14T01:33:07.000Z","dependencies_parsed_at":"2022-08-15T02:40:37.274Z","dependency_job_id":null,"html_url":"https://github.com/odlp/oops_a_rake","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/odlp%2Foops_a_rake","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/odlp%2Foops_a_rake/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/odlp%2Foops_a_rake/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/odlp%2Foops_a_rake/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/odlp","download_url":"https://codeload.github.com/odlp/oops_a_rake/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249341776,"owners_count":21254191,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["rake"],"created_at":"2024-10-01T16:23:09.086Z","updated_at":"2025-04-17T12:30:09.764Z","avatar_url":"https://github.com/odlp.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# OOP(s) a Rake\n\nWrite your Rake tasks as plain-old Ruby objects.\n\n## Setup\n\nAdd the gem to your Gemfile and `bundle install`:\n\n```ruby\ngem \"oops_a_rake\"\n```\n\nIn your Rakefile, require `oops_a_rake` and require your tasks:\n\n```ruby\n# Rakefile\n\nrequire \"oops_a_rake\"\n\nDir.glob(\"lib/tasks/**/*.rb\").each { |task| require_relative(task) }\n```\n\n## Usage\n\n### Simple task with a description\n\nWrite a class which:\n\n- responds to `#call`\n- includes `OopsARake::Task`\n\n```ruby\nclass GreetingTask\n  include OopsARake::Task\n\n  description \"An enthusiastic greeting\"\n\n  def call\n    puts \"Hello!\"\n  end\nend\n```\n\nWhen you list all the Rake tasks in the project:\n\n```sh\n$ bundle exec rake --tasks\n```\n\nYou should see the `greeting` task listed (note the optional 'task' suffix from\nthe class name is omitted):\n\n```\nrake greeting   # An enthusiastic greeting\n```\n\nN.B. Unless you include a `description` for a task then Rake won't list it by\ndefault. Run `bundle exec rake --tasks --all` to see tasks without descriptions.\n\n### Task with arguments\n\n**Note:** Only positional arguments are supported.\n\n```ruby\nclass PersonalizedGreetingTask\n  include OopsARake::Task\n\n  def call(name)\n    puts \"Hello #{name}!\"\n  end\nend\n```\n\nInvocation:\n\n```sh\nbundle exec rake \"personalized_greeting[Bob]\"\n# =\u003e Hello Bob!\n```\n\n### Task with prequisites\n\n```ruby\nclass ComplexSetupTask\n  include OopsARake::Task\n\n  prerequisites :task_one, :task_two\n\n  def call\n    # Your implementation\n  end\nend\n```\n\n### Namespaced task\n\n```ruby\nclass Admin::SpecialTask\n  include OopsARake::Task\n\n  def call\n    # Your implementation\n  end\nend\n```\n\nInvocation:\n\n```sh\nbundle exec rake admin:special\n```\n\n### Task with a custom name\n\n```ruby\nclass ObscureClassNameTask\n  include OopsARake::Task.with_options(name: \"custom_name\")\n\n  def call\n    puts \"Hello\"\n  end\nend\n```\n\nInvocation:\n\n```sh\nbundle exec rake custom_name\n```\n\n## Motivation\n\nRake is an omnipresent tool in the Ruby world. It has some drawbacks – the main\nissue I've heard repeatedly is how difficult it is to test Rake tasks.\n\nTesting Rake tasks isn't impossible, but it's complex and requires some\nfamiliarity with how Rake works (see [Test Rake Tasks Like a BOSS][testing-tasks]\nfor an excellent guide).\n\nAs a result I've seen many codebases which opt for writing thin Rake tasks that\ncall a plain Ruby object, which is tested in isolation:\n\n```ruby\ntask :greeting do |_, args|\n  SomeObject.new(*args).call\nend\n```\n\nInstead of writing this glue-code by hand it's cleaner to write your tasks as\nobjects:\n\n```ruby\n# lib/tasks/greeting_task.rb\n\nclass GreetingTask\n  include OopsARake::Task\n\n  def call(name)\n    puts \"Hello #{name}\"\n  end\nend\n```\n\nTo test this task you can then initialize a new instance and invoke `#call`.\nThis side-steps any requirement to manage Rake's world in tests. For example in\nRSpec:\n\n\n```ruby\nrequire \"tasks/greeting_task\"\n\nRSpec.describe GreetingTask do\n  it \"personalizes the greeting\" do\n    task = described_class.new\n    task.call(\"Bob\")\n\n    # ... rest of your test\n  end\nend\n```\n\nThis approach is heavily inspired by Sidekiq, which allows jobs to be tested the\nsame way:\n\n```ruby\nclass HardWorker\n  include Sidekiq::Worker\n\n  def perform(name, count)\n    # do something\n  end\nend\n```\n\n[testing-tasks]: https://thoughtbot.com/blog/test-rake-tasks-like-a-boss\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fodlp%2Foops_a_rake","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fodlp%2Foops_a_rake","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fodlp%2Foops_a_rake/lists"}