Service objects for Rails, the Rails way. This Ruby gem adds service objects to your Rails applications.
1 - Add the gem to your Rails application's Gemfile:
bundle add rails_application_service
2 - Install the gem by running:
bundle install
3 - Create an app/services
directory in your Rails application:
mkdir -p app/services
The ApplicationService::Base
class provides a standard interface for calling service objects with robust type handling and validations. It leverages ActiveModel::API
for initialization with keyword arguments, ActiveModel::Attributes
for type casting, and ActiveModel::Validations
for input validation.
require "application_service"
class MyService < ApplicationService::Base
def call
# Perform the service action
end
end
my_service = MyService.call # nil
require "application_service"
class Sum < ApplicationService::Base
attribute :number_a, :integer
attribute :number_b, :integer
validates :number_a, :number_b, presence: true, numericality: { greater_than: 0 }
def call
number_a + number_b
end
end
sum = Sum.call(number_a: 1, number_b: 2) # => 3
This gem supports the following attribute types through ActiveModel::Attributes
and custom types defined in ActiveModel::Type
:
:boolean
:date
:datetime
:decimal
:float
:integer
:string
:time
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to execute the tests. You can also run bin/console
for an interactive prompt 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
, then run bundle exec rake release
. This will create a git tag for the version, push git commits and the tag, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/mariomarroquim/rails_application_service. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the ApplicationService project's codebases, issue trackers, chat rooms, and mailing lists is expected to follow the code of conduct.
You can contact me at [email protected].