Skip to content

Repository files navigation

Code ClimateCodeship Status for nebulab/simple_command

SimpleCommand

A simple, standardized way to build and use Service Objects (aka Commands) in Ruby

Requirements

  • Ruby 2.0+

Installation

Add this line to your application's Gemfile:

gem'simple_command'

And then execute:

$ bundle

Or install it yourself as:

$ gem install simple_command

Usage

Here's a basic example of a command that authenticates a user

# define a command classclassAuthenticateUser# put SimpleCommand before the class' ancestors chainprependSimpleCommandincludeActiveModel::Validations# optional, initialize the command with some argumentsdefinitialize(email,password)@email=email@password=passwordend# mandatory: define a #call method. its return value will be available# through #resultdefcallifuser=User.find_by(email: @email)&.authenticate(@password)returnuserelseerrors.add(:base,:failure)endnilendend

in your locale file

# config/locales/en.ymlen:
activemodel:
errors:
models:
authenticate_user:
failure: Wrong email or password

Then, in your controller:

classSessionsController < ApplicationControllerdefcreate# initialize and execute the command# NOTE: `.call` is a shortcut for `.new(args).call`command=AuthenticateUser.call(session_params[:email],session_params[:password])# check command outcomeifcommand.success?# command#result will contain the user instance, if foundsession[:user_token]=command.result.secret_tokenredirect_toroot_pathelseflash.now[:alert]=t(command.errors.full_messages.to_sentence)render:newendendprivatedefsession_paramsparams.require(:session).permit(:email,:password)endend

Test with Rspec

Make the spec file spec/commands/authenticate_user_spec.rb like:

describeAuthenticateUserdosubject(:context){described_class.call(username,password)}describe'.call'docontext'when the context is successful'dolet(:username){'correct_user'}let(:password){'correct_password'}it'succeeds'doexpect(context).tobe_successendendcontext'when the context is not successful'dolet(:username){'wrong_user'}let(:password){'wrong_password'}it'fails'doexpect(context).tobe_failureendendendend

Contributing

  1. Fork it ( https://github.com/nebulab/simple_command/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

About

A simple, standardized way to build and use Service Objects (aka Commands) in Ruby

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages