README.md in command_service_object-0.1.1 vs README.md in command_service_object-0.2.0

- old
+ new

@@ -1,11 +1,9 @@ # CommandServiceObject -Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/command_service_object`. To experiment with that code, run `bin/console` for an interactive prompt. +Rails Generator for command service object. -TODO: Delete this and the text above, and describe your gem - ## Installation Add this line to your application's Gemfile: ```ruby @@ -20,24 +18,97 @@ $ gem install command_service_object ## Usage -TODO: Write usage instructions here + $ rails g service [service_name] [usecases usecases] +### Example + $ rails g service user create update delete +output + +```bash +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](https://github.com/solnic/virtus) for more info. +```ruby +# 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 +```ruby +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](https://rubygems.org). ## Contributing -Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/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](http://contributor-covenant.org) code of conduct. +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](http://contributor-covenant.org) code of conduct. ## License The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). ## 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](https://github.com/[USERNAME]/command_service_object/blob/master/CODE_OF_CONDUCT.md). +Everyone interacting in the CommandServiceObject project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/adham90/command_service_object/blob/master/CODE_OF_CONDUCT.md).