CommandServiceObject

Rails Generator for command service object.

Installation

Add this line to your application's Gemfile:

gem 'command_service_object'

And then execute:

$ bundle

Or install it yourself as:

$ gem install command_service_object

Usage

$ rails g service [service_name] [usecases usecases]

Example

$ rails g service user create update delete

output

create  app/services/user_service
create  app/services/user_service/usecases
create  app/services/user_service/commands
create  app/services/user_service/errors
create  app/services/user_service/usecases/create.rb
create  app/services/user_service/commands/create.rb
create  app/services/user_service/usecases/update.rb
create  app/services/user_service/commands/update.rb
create  app/services/user_service/usecases/delete.rb
create  app/services/user_service/commands/delete.rb

then you can edit command params

you can read Virtus gem docs for more info.

# app/services/user_service/commands/create.rb
module UserService::Commands
  class Create
    include Virtus.model
attribute :name, String
attribute :phone, String
attribute :age, Integer

end end

and then add your business logic
```ruby
# app/services/user_service/usecases/create.rb
module UserService::Usecases
  class Create < ServiceBase
    def call
        # your business logic goes here
        # keep call method clean by using private methods for Business logic
        do_something
        do_another_something
    end

    private

    def do_something
        # Business logic
        # Don't catch errors ApplicationService will do that for you
        raise Errors::CustomeError if ERROR
    end

    def do_another_something
        # another business logic
    end
  end
end

usage from controller

class UsersController < ApplicationController
  def create
    cmd    = UserService::Commands::Create.new(user_params)
    result = ApplicationService.call(cmd)

    if result.ok?
      render json: result.value!.as_json, status: 201
    else
      render json: { message: result.error }, status: 422
    end
  end
end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/adham90/command_service_object. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the CommandServiceObject project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.