Where do I put my application's business logic? I don't like to write business logics in rails' models or controllers. Peafowl encapsulate your business logic as a peafowl.
Add peafowl
to your Gemfile and bundle install
:
gem 'peafowl'
In addition to Interactor gem features, Peafowl support input validation. It is recommended that read Interactor gem before using Peafowl.
Peafowl use ActiveModel to provide validation for inputs. Following example shows a simple login service in Peafowl:
class LoginService
include Peafowl
attribute :username, String
attribute :password, String
validates :username, presence: true
validates :password, presence: true
def call
sample_username = 'misugi'.freeze
sample_password = 'captain_tsubasa'.freeze
add_error!('Username or Password is not valid!') unless username == sample_username && password == sample_password
context[:user] = { username: sample_username }
end
end
And now you can use your service:
result = LoginService.call(username: 'misugi', password: 'captain_tsubasa')
raise UnauthorizedException.new(result.errors) if result.failure?
current_user = result.user
You should define your input parameters with attribute
keyword:
attribute param_name, String
String
is parameter type and Peafowl will cast the passed argument to specified type (by Virtus gem).
Peafowl services returns array of string as error messages. add_error
and add_error!
get a string or array of string as argument.
add_error
method adds error(s) to error list and does not break service execution. After end of execution failure?
method will return true
(and success?
will return false
).
add_error('Your error message.') if condition
add_error!
is same as add_error with the difference that add_error! will break the execution of call method.
Every gem which has been compared in
this article has some drawbacks. I like active_interaction
but it isn't flexible and doesn't support composition. interactor
is very simple one with a flexible structure but doesn't support input validation.
Here are some advantages of Peafowl:
- Input validation
- Composition
- Simple error handling
- Readability, simplicity & flexibility
- Output validation
- Input type validation
- Better error handling