README.md in u-service-0.12.0 vs README.md in u-service-0.13.0

- old
+ new

@@ -21,10 +21,11 @@ - [How to use the result hooks?](#how-to-use-the-result-hooks) - [How to create a pipeline of Service Objects?](#how-to-create-a-pipeline-of-service-objects) - [What is a strict Service Object?](#what-is-a-strict-service-object) - [How to validate Service Object attributes?](#how-to-validate-service-object-attributes) - [It's possible to compose pipelines with other pipelines?](#its-possible-to-compose-pipelines-with-other-pipelines) + - [Examples](#examples) - [Comparisons](#comparisons) - [Benchmarks](#benchmarks) - [Development](#development) - [Contributing](#contributing) - [License](#license) @@ -108,11 +109,11 @@ def call! return Failure(:invalid) { 'the number must be a numeric value' } unless number.is_a?(Numeric) return Failure(:lte_zero) { 'the number must be greater than 0' } if number <= 0 - Success(number * number) + Success(number * 2) end end #================================# # Printing the output if success # @@ -123,11 +124,11 @@ .on_success { |number| p number } .on_failure(:invalid) { |msg| raise TypeError, msg } .on_failure(:lte_zero) { |msg| raise ArgumentError, msg } # The output when is a success: -# 9 +# 6 #=============================# # Raising an error if failure # #=============================# @@ -249,12 +250,12 @@ # # By default, if your project has the activemodel # any kind of service attribute can be validated. # class Multiply < Micro::Service::Base - attribute :a - attribute :b + attributes :a, :b + validates :a, :b, presence: true, numericality: true def call! return Failure(errors: self.errors) unless valid? @@ -264,27 +265,32 @@ # # But if do you want an automatic way to fail # your services if there is some invalid data. # You can use: -require 'micro/service/with_validation' +# In some file. e.g: A Rails initializer +require 'micro/service/with_validation' # or require 'u-service/with_validation' + +# In the Gemfile +gem 'u-service', '~> 0.12.0', require: 'u-service/with_validation' + # Using this approach, you can rewrite the previous sample with fewer lines of code. -class Multiply < Micro::Service::WithValidation - attribute :a - attribute :b +class Multiply < Micro::Service::Base + attributes :a, :b + validates :a, :b, presence: true, numericality: true def call! Success(number: a * b) end end # Note: -# There is a strict variation for Micro::Service::WithValidation -# Use Micro::Service::Strict::Validation if do you want this behavior. +# After requiring the validation mode, the +# Micro::Service::Strict classes will inherit this new behavior. ``` ### It's possible to compose pipelines with other pipelines? Answer: Yes @@ -331,24 +337,34 @@ Add2ToAllNumbers = Steps::ConvertToNumbers >> Steps::Add2 DoubleAllNumbers = Steps::ConvertToNumbers >> Steps::Double SquareAllNumbers = Steps::ConvertToNumbers >> Steps::Square DoubleAllNumbersAndAdd2 = DoubleAllNumbers >> Steps::Add2 SquareAllNumbersAndAdd2 = SquareAllNumbers >> Steps::Add2 -DoubleAllNumbersAndSquareThem = DoubleAllNumbers >> SquareAllNumbersAndAdd2 -SquareAllNumbersAndDoubleThem = SquareAllNumbersAndAdd2 >> DoubleAllNumbers +SquareAllNumbersAndDouble = SquareAllNumbersAndAdd2 >> DoubleAllNumbers +DoubleAllNumbersAndSquareAndAdd2 = DoubleAllNumbers >> SquareAllNumbersAndAdd2 -DoubleAllNumbersAndSquareThem +SquareAllNumbersAndDouble .call(numbers: %w[1 1 2 2 3 4]) - .on_success { |value| p value[:numbers] } # [6, 6, 18, 18, 38, 66] + .on_success { |value| p value[:numbers] } # [6, 6, 12, 12, 22, 36] -SquareAllNumbersAndDoubleThem +DoubleAllNumbersAndSquareAndAdd2 .call(numbers: %w[1 1 2 2 3 4]) - .on_success { |value| p value[:numbers] } # [6, 6, 12, 12, 22, 36] + .on_success { |value| p value[:numbers] } # [6, 6, 18, 18, 38, 66] ``` Note: You can blend any of the [syntaxes/approaches to create the pipelines](#how-to-create-a-pipeline-of-service-objects)) - [examples](https://github.com/serradura/u-service/blob/master/test/micro/service/pipeline/blend_test.rb#L7-L34). +## Examples + +1. [Rescuing an exception inside of service objects](https://github.com/serradura/u-service/blob/master/examples/rescuing_exceptions.rb) +2. [Users creation](https://github.com/serradura/u-service/blob/master/examples/users_creation.rb) + + An example of how to use services pipelines to sanitize and validate the input data, and how to represents a common use case, like: create an user. +3. [CLI calculator](https://github.com/serradura/u-service/tree/master/examples/calculator) + + A more complex example which use rake tasks to demonstrate how to handle user data, and how to use different failures type to control the app flow. + ## Comparisons Check it out implementations of the same use case with different libs (abstractions). * [interactor](https://github.com/serradura/u-service/blob/master/comparisons/interactor.rb) @@ -358,9 +374,10 @@ **[interactor](https://github.com/collectiveidea/interactor)** VS **[u-service](https://github.com/serradura/u-service)** https://github.com/serradura/u-service/tree/master/benchmarks/interactor +![interactor VS u-service](https://github.com/serradura/u-service/blob/master/assets/u-service_benchmarks.png?raw=true) ## Development After checking out the repo, run `bin/setup` to install dependencies. Then, run `./test.sh` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.