{"id":13835632,"url":"https://github.com/nebulab/simple_command","last_synced_at":"2025-05-15T02:06:16.265Z","repository":{"id":24976160,"uuid":"28394525","full_name":"nebulab/simple_command","owner":"nebulab","description":"A simple, standardized way to build and use Service Objects (aka Commands) in Ruby","archived":false,"fork":false,"pushed_at":"2023-09-11T04:27:30.000Z","size":54,"stargazers_count":630,"open_issues_count":6,"forks_count":54,"subscribers_count":31,"default_branch":"master","last_synced_at":"2025-05-10T12:09:13.492Z","etag":null,"topics":["ruby","service-object"],"latest_commit_sha":null,"homepage":"http://nebulab.it","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/nebulab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2014-12-23T11:28:33.000Z","updated_at":"2025-05-08T05:39:17.000Z","dependencies_parsed_at":"2024-01-13T21:10:40.195Z","dependency_job_id":null,"html_url":"https://github.com/nebulab/simple_command","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nebulab%2Fsimple_command","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nebulab%2Fsimple_command/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nebulab%2Fsimple_command/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nebulab%2Fsimple_command/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nebulab","download_url":"https://codeload.github.com/nebulab/simple_command/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254259370,"owners_count":22040819,"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":["ruby","service-object"],"created_at":"2024-08-04T14:01:07.031Z","updated_at":"2025-05-15T02:06:16.248Z","avatar_url":"https://github.com/nebulab.png","language":"Ruby","readme":"[![Code Climate](https://codeclimate.com/github/nebulab/simple_command/badges/gpa.svg)](https://codeclimate.com/github/nebulab/simple_command)\n![CI](https://github.com/nebulab/simple_command/actions/workflows/ci.yml/badge.svg)\n\n# SimpleCommand\n\nA simple, standardized way to build and use _Service Objects_ (aka _Commands_) in Ruby\n\n## Requirements\n\n* Ruby 2.6+\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'simple_command'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install simple_command\n\n## Usage\n\nHere's a basic example of a command that authenticates a user\n\n```ruby\n# define a command class\nclass AuthenticateUser\n  # put SimpleCommand before the class' ancestors chain\n  prepend SimpleCommand\n  include ActiveModel::Validations\n\n  # optional, initialize the command with some arguments\n  def initialize(email, password)\n    @email = email\n    @password = password\n  end\n\n  # mandatory: define a #call method. its return value will be available\n  #            through #result\n  def call\n    if user = User.find_by(email: @email)\u0026.authenticate(@password)\n      return user\n    else\n      errors.add(:base, :failure)\n    end\n    nil\n  end\nend\n```\n\nin your locale file\n```yaml\n# config/locales/en.yml\nen:\n  activemodel:\n    errors:\n      models:\n        authenticate_user:\n          failure: Wrong email or password\n```\n\nThen, in your controller:\n\n```ruby\nclass SessionsController \u003c ApplicationController\n  def create\n    # initialize and execute the command\n    # NOTE: `.call` is a shortcut for `.new(args).call`\n    command = AuthenticateUser.call(session_params[:email], session_params[:password])\n\n    # check command outcome\n    if command.success?\n      # command#result will contain the user instance, if found\n      session[:user_token] = command.result.secret_token\n      redirect_to root_path\n    else\n      flash.now[:alert] = t(command.errors.full_messages.to_sentence)\n      render :new\n    end\n  end\n\n  private\n\n  def session_params\n    params.require(:session).permit(:email, :password)\n  end\nend\n```\n\n## Test with Rspec\nMake the spec file `spec/commands/authenticate_user_spec.rb` like:\n\n```ruby\ndescribe AuthenticateUser do\n  subject(:context) { described_class.call(username, password) }\n\n  describe '.call' do\n    context 'when the context is successful' do\n      let(:username) { 'correct_user' }\n      let(:password) { 'correct_password' }\n      \n      it 'succeeds' do\n        expect(context).to be_success\n      end\n    end\n\n    context 'when the context is not successful' do\n      let(:username) { 'wrong_user' }\n      let(:password) { 'wrong_password' }\n\n      it 'fails' do\n        expect(context).to be_failure\n      end\n    end\n  end\nend\n```\n\n## Contributing\n\n1. Fork it ( https://github.com/nebulab/simple_command/fork )\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnebulab%2Fsimple_command","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnebulab%2Fsimple_command","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnebulab%2Fsimple_command/lists"}