{"id":29954990,"url":"https://github.com/foca/writ","last_synced_at":"2025-10-15T10:40:07.623Z","repository":{"id":56898604,"uuid":"77083069","full_name":"foca/writ","owner":"foca","description":"Minimal command pattern implementation on top of Scrivener","archived":false,"fork":false,"pushed_at":"2017-02-01T13:30:45.000Z","size":6,"stargazers_count":16,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-14T03:32:47.562Z","etag":null,"topics":["command-pattern","lesscode","ruby","validation"],"latest_commit_sha":null,"homepage":null,"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/foca.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-12-21T20:15:26.000Z","updated_at":"2020-05-28T04:10:48.000Z","dependencies_parsed_at":"2022-08-20T17:40:34.182Z","dependency_job_id":null,"html_url":"https://github.com/foca/writ","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/foca/writ","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foca%2Fwrit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foca%2Fwrit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foca%2Fwrit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foca%2Fwrit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/foca","download_url":"https://codeload.github.com/foca/writ/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foca%2Fwrit/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279074040,"owners_count":26097509,"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","status":"online","status_checked_at":"2025-10-15T02:00:07.814Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["command-pattern","lesscode","ruby","validation"],"created_at":"2025-08-03T17:08:58.298Z","updated_at":"2025-10-15T10:40:07.593Z","avatar_url":"https://github.com/foca.png","language":"Ruby","readme":"# Writ: A minimal command pattern implementation\n\n[![Build Status](https://img.shields.io/travis/foca/writ.svg)](https://travis-ci.org/foca/writ) [![RubyGem](https://img.shields.io/gem/v/writ.svg)](https://rubygems.org/gems/writ)\n\n\u003e **writ**, _noun_:\u003cbr\u003e\n\u003e \u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;a form of written command in the name of a court or other legal authority to act, or abstain from acting, in some way.\n\n`Writ` is a minimal [command pattern][pattern] implementation, including input validation, built on top of [Scrivener][scrivener].\n\n[pattern]: http://wiki.c2.com/?CommandPattern\n[scrivener]: https://github.com/soveran/scrivener\n\n# Example\n\n``` ruby\n# Define your commands\nclass LogIn \u003c Writ\n  attr_accessor :email\n  attr_accessor :password\n\n  def validate\n    assert_email :email\n    assert_present :password\n  end\n\n  def run\n    user = User.authenticate(email, password)\n    assert user, [:email, :not_valid]\n  end\nend\n\n# And use them\noutcome = LogIn.run(req.params)\n\nif outcome.success?\n  session[:user_id] = outcome.value.id\nelse\n  outcome.input.password = nil # Don't leak it when rendering the form again\n  render \"auth/sign_in\", errors: outcome.errors, form: outcome.input\nend\n```\n\n## Install\n\n    gem install writ\n\n## Validations\n\nAs with [scrivener][], you should define a `validate` method that defines all validations for the user input. You're also welcome to use validations inside of `run`, when an error can come from the process itself. The example above, where the email/password combination yields no user, is a good example. Another example would be having to make an API call that returns an error.\n\nTake a look at the default [validations](https://github.com/soveran/scrivener#assertions) that come from scrivener, or feel free to define custom ones to suit your domain.\n\n## Why\n\nWhen using [scrivener][], I tend to add a `run` method to the objects to alter state based on user input alongside the validations. Some people like calling these objects \"services\", \"use cases\", \"commands\", or \"interactions\".\n\nI find this useful to decouple complex actions from the actors involved in them, and to keep models \"thin\" by only caring about expressing the domain without burdening themselves with expressing how users can interact with the domain, or with concepts like validation and authorization.\n\nAfter using this pattern in several production projects, I figured it might as well live in a gem I can reuse instead of copy-pasting code around.\n\n## License\n\nThis project is shared under the MIT license. See the attached [LICENSE](./LICENSE) file for details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffoca%2Fwrit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffoca%2Fwrit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffoca%2Fwrit/lists"}