{"id":13878566,"url":"https://github.com/hoppergee/solidservice","last_synced_at":"2025-04-12T05:42:51.332Z","repository":{"id":39918062,"uuid":"487290350","full_name":"hoppergee/solidservice","owner":"hoppergee","description":"A servcie pattern with a simple API","archived":false,"fork":false,"pushed_at":"2022-05-22T02:28:19.000Z","size":44,"stargazers_count":40,"open_issues_count":6,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-12T05:42:44.305Z","etag":null,"topics":["rails","ruby","rubygem","service"],"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/hoppergee.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}},"created_at":"2022-04-30T13:54:16.000Z","updated_at":"2023-01-02T16:00:55.000Z","dependencies_parsed_at":"2022-08-27T08:02:04.809Z","dependency_job_id":null,"html_url":"https://github.com/hoppergee/solidservice","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoppergee%2Fsolidservice","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoppergee%2Fsolidservice/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoppergee%2Fsolidservice/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoppergee%2Fsolidservice/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hoppergee","download_url":"https://codeload.github.com/hoppergee/solidservice/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248525161,"owners_count":21118616,"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":["rails","ruby","rubygem","service"],"created_at":"2024-08-06T08:01:53.318Z","updated_at":"2025-04-12T05:42:51.310Z","avatar_url":"https://github.com/hoppergee.png","language":"Ruby","readme":"# SolidService\n\n[![CI](https://github.com/hoppergee/solidservice/actions/workflows/main.yml/badge.svg)](https://github.com/hoppergee/solidservice/actions/workflows/main.yml)\n[![Maintainability](https://api.codeclimate.com/v1/badges/625ef769e60ab39159ce/maintainability)](https://codeclimate.com/github/hoppergee/solidservice/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/625ef769e60ab39159ce/test_coverage)](https://codeclimate.com/github/hoppergee/solidservice/test_coverage)\n\nA service pattern with a simple API.\n\n```ruby\nresult = ASolidService.call(any: 'thing', you: 'like')\nresult.success? #=\u003e true\nresult.fail? #=\u003e false\n```\n\n- One service per action\n- Service only has one public method `.call` with a hash input argument\n- The `.call` always return a `State` object. You can ask the state object the execution result\n\nCheck the [Q\u0026A](#qa) for popular questions like:\n\n- Is this gem has any different than interactor, simple_command and dry-monads?\n- You use rescue to handle control flow? That's a bad idea.\n\n## Installation\n\nInstall the gem and add to the application's Gemfile by executing:\n\n```bash\n$ bundle add solidservice\n```\n\nOr manually add it in Gemfile\n\n```ruby\ngem 'solidservice'\n```\n\n\n## Basic Usage\n\nHere is an example for Rails app.\n\n1. Create a `services` folder\n\n```bash\nmkdir app/services\n```\n\n2. Create the service you want\n\n```ruby\nclass UpdateUser \u003c SolidService::Base\n  def call\n    if user.update(user_params)\n      success!(user: user)\n    else\n      fail!(user: user)\n    end\n  end\n\n  private\n\n  def user\n    @user ||= User.find(params[:id])\n  end\n\n  def user_params\n    @user_params ||= params[:user_params]\n  end\nend\n```\n\n3. Use this service in controller\n\n```ruby\nclass UsersController \u003c ApplicationController\n  def update\n    result = UpdateUser.call(\n      id: params[:id],\n      user_params: params.require(:user).permit(:email)\n    )\n\n    if result.success?\n      redirect_to root_path\n    else\n      @user = result.user\n      render :edit\n    end\n  end\nend\n```\n\n## Only 4 DSL in the `call` method\n\n- `success!` - Success the service immediately, any code after it won't be execute (Recommend)\n- `success` - Just update the state to success\n- `fail!` - Fail the service immediately, any code after it won't be execute (Recommend)\n- `fail` - Just update the state to fail\n\n## How to return other data\n\nYou can send data with state object like this:\n\n```ruby\nsuccess!(user: user)\nsuccess(user: user, item: item)\nfail!(email: email, error: error)\nfail(error: error)\n```\n\nThen we can get those data on the result:\n\n```ruby\nresult = ExampleService.call\nresult.success? #=\u003e true\nresult.user\nresult.item\n\nresult.success? #=\u003e false\nresult.error\nresult.email\n```\n\n## service success by default\n\nIf you don't call above 4 methods, the service will be marked as success by default. When some services which just want to execute some actions and don't want to return anything, go ahead, SolidService will take care of it.\n\n```ruby\nclass ACommandService \u003c SolidService::Base\n  def call\n    # Do some actions that don't need to return anything\n  end\nend\n\nresult = ACommandService.call\nresult.success? #=\u003e true\n```\n\n## Use service in service with `call!`\n\nSometimes, we want to use a service in another service, but don't want to doing `if/else` on the state object everywhere, then we can use `call!` for the inner service. Then the service will raise error on failure.\n\n```ruby\nclass Action2 \u003c SolidService::Base\n  def call\n    fail!(error: StandardError.new('something wrong'))\n  end\nend\n\nclass Action1 \u003c SolidService::Base\n  def call\n    Action2.call!\n  end\nend\n\nresult = Action1.call\nresult.fail? #=\u003e true\nresult.error #=\u003e #\u003cStandardError: something wrong\u003e\n```\n\n## Q\u0026A\n\n### Is this gem has any different than interactor, simple_command and dry-monads?\n\nHere are some the key advantages:\n\n- It's much simple then other service like pattern.\n- You can master it in a few of seconds and start to use it in real projects.\n- Easy to write concise, readable and maintainable code with only 4 DSL\n- Unify input and output but without any restrictions\n  - The input is a hash called `params`. It's Rails dev friendly, use it just like a controller\n  - The output is an state object with hash data. Call any methods on it, it won't raise error on data missing.\n\n### You use rescue to handle control flow? That's a bad idea.\n\n**This gem doesn't force you to do that.** Those rescue handle on `Success` and `Failure` only work when you use `success!` and `fail!`. You can only use `success` and `fail` if you don't like the rescue pattern.\n\nI use it for a very practical reason. I believe many people have met this issue in the controller:\n\n```ruby\nclass ExampleController \u003c ApplicationController\n  def create\n     if a_condition\n        render :a_page and return\n     end\n\n     # ......\n  end\nend\n```\n\nI meet the same issue in service:\n\n```ruby\nclass ExampleService \u003c SolidService::Base\n  def call\n    if check_1_failed\n      fail(user: user) and return\n    end\n\n    if check_2_failed\n      fail(user: user) and return\n    end\n\n    # ...\n  end\nend\n```\n\nI hate this. It's very low readability. So I comes up with with the rescue way to end the action immediately when I call `success!` and `fail!`\n\n```ruby\nclass ExampleService \u003c SolidService::Base\n  def call\n    fail!(user: user) if check_1_failed\n    fail!(user: user) if check_2_failed\n\n    # ...\n  end\nend\n```\n\nI think it's much better. So you see, the `rescue` is for `success!` and `fail!` only. You can still use `success` and `fail`. Even me, I did have a few circumstances need me to call success multiple times, then I will use it also.\n\n## Development\n\n```bash\nbundle install\nmeval rake # Run test\nmeval -a rake # Run tests against all Ruby versions and Rails versions\n```\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/hoppergee/solidservice. 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/hoppergee/solidservice/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 SolidService project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/hoppergee/solidservice/blob/master/CODE_OF_CONDUCT.md).\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhoppergee%2Fsolidservice","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhoppergee%2Fsolidservice","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhoppergee%2Fsolidservice/lists"}