Skip to content

Service Layer Pattern Implementation

License

Notifications You must be signed in to change notification settings

gemologist/service_layer

Repository files navigation

ServiceLayer

Service Layer Pattern Implementation.

Provide an easy way to write service layer object.
Services are used to encapsulate application logic business.

Installation

Add this line to your application's Gemfile:

gem 'service_layer'

And then execute:

$ bundle

Or install it yourself as:

$ gem install service_layer

Usage

class MatchingService < ApplicationService
  property :tender
  render :matches_created

  def perform
    # business logic
  end
end
class TendersController < ApplicationController
  def update
    @tender = Tender.find(params[:id])
    result = MatchingService.perform(tender: @tender)

    respond_to do |format|
      if result.success?
        format.html { redirect_to tender_path(@tender), status: :see_other }
      else
        format.html do
          flash.now[:message] = t(result.error.message)
          render :edit
        end
      end
    end
  end
end

Configuration

Monad adapter

May be set to one of [:dry, :without]. :without is the default one but also deprecated. Override with :dry.

# config/initializers/service_layer.rb
 
ServiceLayer.configure do |config|
  config.monad = :dry
end

Contributing

Bug reports and pull requests are welcome on GitHub.

Everyone interacting in the ServiceLayer project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

License

The gem is available as open source under the terms of the MIT License.