README.md in u-service-0.7.0 vs README.md in u-service-0.8.0
- old
+ new
@@ -10,11 +10,11 @@
- [μ-service (Micro::Service)](#%ce%bc-service-microservice)
- [Required Ruby version](#required-ruby-version)
- [Installation](#installation)
- [Usage](#usage)
- - [How to create a basic Service Object?](#how-to-create-a-basic-service-object)
+ - [How to create a Service Object?](#how-to-create-a-service-object)
- [How to use the Service Object result hooks?](#how-to-use-the-service-object-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)
- [Development](#development)
@@ -42,11 +42,11 @@
$ gem install u-service
## Usage
-### How to create a basic Service Object?
+### How to create a Service Object?
```ruby
class Multiply < Micro::Service::Base
attributes :a, :b
@@ -65,10 +65,14 @@
result = Multiply.call(a: 2, b: 2)
p result.success? # true
p result.value # 4
+# Note:
+# The result of a Micro::Service#call
+# is an instance of Micro::Service::Result
+
#----------------------------#
# Calling a service instance #
#----------------------------#
result = Multiply.new(a: 2, b: 3).call
@@ -162,15 +166,18 @@
Add2ToAllNumbers = Micro::Service::Pipeline[
Steps::ConvertToNumbers,
Steps::Add2
]
-DoubleAllNumbers = Micro::Service::Pipeline[
- Steps::ConvertToNumbers,
- Steps::Double
-]
+# An alternative way to declare pipelines within classes.
+class DoubleAllNumbers
+ include Micro::Service::Pipeline
+
+ pipeline Steps::ConvertToNumbers, Steps::Double
+end
+
result = Add2ToAllNumbers.call(relation: %w[1 1 2 2 3 4])
p result.success? # true
p result.value # [3, 3, 4, 4, 5, 6]
@@ -198,11 +205,11 @@
# ArgumentError (missing keyword: :numbers)
```
### How to validate Service Object attributes?
-Note: To do this your application must have the (activemodel >= 3.2)(https://rubygems.org/gems/activemodel) as a dependency.
+Note: To do this your application must have the [activemodel >= 3.2](https://rubygems.org/gems/activemodel) as a dependency.
```ruby
#
# By default, if your project has the activemodel
# any kind of service attribute can be validated.
@@ -211,12 +218,12 @@
attribute :a
attribute :b
validates :a, :b, presence: true, numericality: true
def call!
- return Success(number: a * b) if valid?
+ return Failure(errors: self.errors) unless valid?
- Failure(errors: self.errors)
+ Success(number: a * b)
end
end
#
# But if do you want an automatic way to fail