Skip to content

Repository files navigation

Light Services

Light Services is a simple yet powerful way to organize your business logic. This Ruby gem helps you build services that are easy to test, maintain, and understand.

Get started with Quickstart

Features

  • Simple: Define your service as a class with arguments, steps, and outputs
  • Transactions: Automatically rollback database changes if any step fails
  • Inheritance: Inherit from other services to reuse logic seamlessly
  • Error Handling: Collect errors from steps and handle them your way
  • Context: Run multiple services sequentially within the same context
  • Framework Agnostic: Compatible with Rails, Hanami, or any Ruby framework
  • Modularity: Isolate and test your services with ease
  • 100% Test Coverage: Bugs are not welcome here!
  • Battle-Tested: In production use since 2017

Simple Example

classGreetService < Light::Services::Base# Argumentsarg:namearg:age# Stepsstep:build_messagestep:send_message# Outputsoutput:messageprivatedefbuild_messageself.message="Hello, #{name}! You are #{age} years old."enddefsend_message# Send logic goes hereendend

Advanced Example

classUser::ResetPassword < Light::Services::Base# Argumentsarg:user,type: User,optional: truearg:email,type: :string,optional: truearg:send_email,type: :boolean,default: true# Stepsstep:validatestep:find_user,unless: :user?step:generate_reset_tokenstep:save_reset_tokenstep:send_reset_email,if: :send_email?# Outputsoutput:user,type: Useroutput:reset_token,type: :stringprivatedefvalidateerrors.add(:base,"user or email is required")if !user? && !email?enddeffind_userself.user=User.find_by("LOWER(email) = ?",email.downcase)errors.add(:email,"not found")unlessuserenddefgenerate_reset_tokenself.reset_token=SecureRandom.hex(32)enddefsave_reset_tokenuser.update!(reset_password_token: reset_token,reset_password_sent_at: Time.current,)rescueActiveRecord::RecordInvalid=>eerrors.from_record(e.record)enddefsend_reset_emailMailer::SendEmail.with(self)# Call sub-service with the same context.run(template: :reset_password,user:,reset_token:)endend

Do you like what you see? Get started with Light Services today!

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors