Slayer: A Killer Service Layer
Slayer is intended to operate as a minimal service layer for your ruby application. To achieve this, Slayer provides base classes for business logic.
Slayer provides 3 base classes for organizing your business logic:
Services. Each of these has a distinct role in your application's structure.
Slayer::Forms are objects for wrapping a set of data, usually to be passed as a parameter to a
Slayer::Commands are the bread and butter of your application's business logic.
Commands are where you compose services, and perform one-off business logic tasks. In our applications, we usually create a single
Commands should call
Services should never call
Services are the building blocks of
Commands, and encapsulate re-usable chunks of application logic.
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install slayer
Slayer Commands should implement
call, which will
fail the service based on input. Commands return a
Slayer::Result which has a predictable interface for determining
message, and a
result payload object.
# A Command that passes when given the string "foo" # and fails if given anything else. class FooCommand < :: def call(foo:) if foo == "foo" pass! result: foo, message: "Passing FooCommand" else fail! result: foo, message: "Failing FooCommand" end end end result = FooCommand.call(foo: "foo") result.success? # => true result = FooCommand.call(foo: "bar") result.success? # => false
Slayer Services are objects that should implement re-usable pieces of application logic or common tasks. To prevent circular dependencies Services are required to declare which other Service classes they depend on. If a circular dependency is detected an error is raised.
In order to enforce the lack of circular dependencies, Service objects can only call other Services that are declared in their dependencies.
class NetworkService < Slayer::Service def self.post() ... end end class StripeService < Slayer::Service dependencies NetworkService def self.pay() ... NetworkService.post(url: "stripe.com", body: my_payload) ... end end
After checking out the repo, run
bin/setup to install dependencies. Then, run
rake test 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.
To generate documentation run
yard. To view undocumented files run
yard stats --list-undoc.
Bug reports and pull requests are welcome on GitHub at https://github.com/apsislabs/slayer.
The gem is available as open source under the terms of the MIT License.