{"id":15405618,"url":"https://github.com/krisleech/domain3","last_synced_at":"2025-03-24T21:41:04.358Z","repository":{"id":26705891,"uuid":"30163047","full_name":"krisleech/domain3","owner":"krisleech","description":"Service, Form and Validator objects.","archived":false,"fork":false,"pushed_at":"2015-07-28T15:32:00.000Z","size":140,"stargazers_count":1,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-17T04:44:45.949Z","etag":null,"topics":[],"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/krisleech.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","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}},"created_at":"2015-02-01T23:00:15.000Z","updated_at":"2015-02-09T22:46:15.000Z","dependencies_parsed_at":"2022-08-18T05:25:25.350Z","dependency_job_id":null,"html_url":"https://github.com/krisleech/domain3","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krisleech%2Fdomain3","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krisleech%2Fdomain3/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krisleech%2Fdomain3/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krisleech%2Fdomain3/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/krisleech","download_url":"https://codeload.github.com/krisleech/domain3/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245359248,"owners_count":20602322,"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-01T16:17:32.453Z","updated_at":"2025-03-24T21:41:04.329Z","avatar_url":"https://github.com/krisleech.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Domain3\n\nLightweight Service, Form and Validator objects.\n\nDecoupled and testable.\n\nLeans heavily on other libraries.\n\n*Service Objects*\n\n* Dependency injection via initializer\n* Broadcasting of sync or async events\n\n*Forms Objects*\n\n* Initialization from hash\n* Attribute type coercion\n* ActiveModel compliant (Rails only)\n* Simple Validations\n\n*Validators*\n\n* Complicated validations, e.g. against entities\n\n## Installation\n\n```ruby\ngem 'domain3'\n```\n\n## Usage\n\nUsing a little DSL, mostly objects:\n\n```ruby\ndef new\n  @form = RegisterUser.new_form(form_params)\nend\n\ndef create\n  @form = RegisterUser.new_form(form_params)\n\n  command = RegisterUser.new\n\n  command.subscribe(AuditListener.new)\n\n  command.on(:success) { redirect_to :index }\n  command.on(:failure) { render action: :new }\n\n  command.execute(@form)\nend\n```\n\n### Forms\n\n```ruby\nclass RegisterUser\n  class Form\n    include D3::Form\n\n    attribute :username, type: String, presence: true\n    attribute :email,    type: String, presence: true\n    attribute :password, type: String, presence: true\n  end\nend\n```\n\nThe `Form` will have the [Lotus::Validations](https://github.com/lotus/validations) module included.\n\nWhere ActiveModel is present [ActiveModel::Conversion](http://api.rubyonrails.org/classes/ActiveModel/Model.html)\nwill be included.\n\n### Services\n\n```ruby\nclass RegisterUser\n  include D3::Service\n\n  dependency :user_repo, default: 'User',\n             :validator, default: 'Validator'\n\n  def execute(form)\n    if validator.valid?(form)\n      user = User.create!(form.attributes)\n\n      broadcast(:success, user.id)\n      broadcast(:user_registered, user.id)\n    else\n      broadcast(:failed, form)\n    end\n  end\nend\n```\n\nThe service has [Wisper](https://github.com/krisleech/wisper) and [Medicine](https://github.com/krisleech/medicine) included.\n\nMedicine provides a macro for declaring dependencies and their defaults.\n\nWisper provides broadcasting of events and subscribing of listeners.\n\n### Validators\n\n```ruby\nclass RegisterUser\n  class Validator\n    include D3::Validator\n\n    dependency :user_repo, 'User'\n\n    def validate(form)\n      if user_repo.exists?(username: form.username)\n        form.errors.add(:username, 'is already taken')\n      end\n    end\n  end\nend\n```\n\n## Contributing\n\nYes, please.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrisleech%2Fdomain3","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkrisleech%2Fdomain3","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrisleech%2Fdomain3/lists"}