{"id":16547535,"url":"https://github.com/waiting-for-dev/kwork","last_synced_at":"2025-07-11T22:08:27.120Z","repository":{"id":66166820,"uuid":"545437715","full_name":"waiting-for-dev/kwork","owner":"waiting-for-dev","description":"Composable service objects","archived":false,"fork":false,"pushed_at":"2023-09-19T14:09:30.000Z","size":131,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-04T10:25:37.188Z","etag":null,"topics":[],"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/waiting-for-dev.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-10-04T11:21:10.000Z","updated_at":"2023-09-21T01:13:15.000Z","dependencies_parsed_at":"2024-11-14T19:47:17.713Z","dependency_job_id":null,"html_url":"https://github.com/waiting-for-dev/kwork","commit_stats":null,"previous_names":["waiting-for-dev/kwork"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/waiting-for-dev/kwork","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/waiting-for-dev%2Fkwork","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/waiting-for-dev%2Fkwork/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/waiting-for-dev%2Fkwork/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/waiting-for-dev%2Fkwork/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/waiting-for-dev","download_url":"https://codeload.github.com/waiting-for-dev/kwork/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/waiting-for-dev%2Fkwork/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264904719,"owners_count":23681261,"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":[],"created_at":"2024-10-11T19:14:38.749Z","updated_at":"2025-07-11T22:08:26.877Z","avatar_url":"https://github.com/waiting-for-dev.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Kwork\n\nKwork is a library for building business transactions in Ruby designed to:\n\n- Be written in a declarative and chainable way.\n- Decouple individual operations from business transactions.\n- Injecting operations for testing purposes or reusability.\n- Be used with different result types (e.g. `Kwork::Result`,\n  `Dry::Monads::Result`, `Dry::Monads::Maybe`...).\n- Provide transactional safety for different database adapters (e.g.,\n  `ROM`, `ActiveRecord`...).\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'kwork', github: 'waiting-for-dev/kwork'\n```\n\nAnd then execute:\n\n    $ bundle install\n\n## Usage\n\nUsing [`dry-auto_inject`](https://dry-rb.org/gems/dry-auto_inject/1.0/) \u0026 [`dry-monads`](https://dry-rb.org/gems/dry-monads/1.6/).\n\n```ruby\nrequire \"kwork\"\n\nclass CheckOutOrder\n  include Kwork[adapter: :result]\n  include Deps[:update_line_items, :update_order, :calculate_best_prices, :enqueue_order_completed_email]\n  \n  def call(order_id, attrs)\n    attrs = step validate(attrs)\n    line_items = step update_line_items.(order_id, attrs[:line_items])\n    order = step update_order.(order_id, attrs.except(:line_items))\n    \n    step calculate_best_prices.(order:, line_items:)\n    step enqueue_order_completed_email.(order)\n    \n    success(order)\n  end\n  \n  private\n  \n  def validate(attrs)\n    # ...\n  end\nend\n\ninclude Dry::Monads[:result]\n\ncase CheckoutOrder.new\nin Success[message]\n  puts message\nin Failure[error]\n  puts error\nend\n```\n\n### Advanced usage\n\nYou can leverage [transactable](https://alchemists.io/projects/transactable) to elegantly use the Railway pattern for data transformation or the whole transactional workflow. For now, it only works with `Dry::Monads::Result` adapter:\n\n```ruby\nclass CreateUser\n  include Kwork[adapter: :result]\n  include Deps[\"user_repo\", \"validate_user\"]\n  include Dry::Monads[:result]\n  \n  DEFAULT_USER_ATTRS = {\n    country: :us,\n    currency: :usd\n  }\n\n  def call(user_attrs)\n    user = pipe user_attrs,\n      merge(DEFAULT_USER_ATTRS),\n      method(:validate_user)\n\n    step create_user(user)\n  end\n\n  private\n\n  def create_user(user)\n    Success(user_repo.create(user))\n  end\nend\n```\n\n### Extensions\n\nMore often than not, a business transaction needs to be wrapped within a database transaction. To support this use case, Kwork gives you the ability to extend the transaction callback so you can wrap it with your own code. A couple of extensions are shipped by default, but you can easily build your own.\n\n#### ROM\n\nYou need to add [rom](https://rom-rb.org/) to your `Gemfile` to use it. When this extension is used, the Kwork transaction is wrapped within a database transaction, which is rolled back in the case of returning a failure.\n\n```ruby\nrequire \"kwork\"\nrequire \"kwork/extensions/rom\"\n\nrom = # ROM container\n\nclass AddUser\n  include Kwork[\n    extension: Kwork::Extensions::ROM[rom, :default] # :default is the name of the gateway\n  ]\n  # ...\nend\n```\n\n#### ActiveRecord\n\nOn a Rails application, you can use the ActiveRecord extension. The raw Kwork transaction will be wrapped within a database transaction, and it'll be rolled back in case of returning a failure.\n\n```ruby\nrequire \"kwork\"\nrequire \"kwork/extensions/active_record\"\n\nclass AddUser\n  include Kwork[\n    extension: Kwork::Extensions::ActiveRecord\n  ]\n  # ...\nend\n```\n\n#### Custom extensions\n\nCustom extensions are just anything responding to `#call` accepting the Kwork transaction callback. You only need to ensure that you respond the result of executing that callback. Take into account that the callback will return an instance of `Kwork::Result`, regardless of the adapter in use. That ensures fully operability with any result type.\n\n```ruby\nrequire \"kwork\"\n\nMyExtension = lambda do |callback|\n callback.().tap do |result|\n   do_something if result.success?\n end\nend\n\nclass AddUser\n  include Kwork[\n    extension: MyExtension\n  ]\n  # ...\nend\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/kwork. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/kwork/blob/master/CODE_OF_CONDUCT.md).\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the Kwork project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/kwork/blob/master/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwaiting-for-dev%2Fkwork","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwaiting-for-dev%2Fkwork","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwaiting-for-dev%2Fkwork/lists"}