Skip to content

Ruby module to provide a strict, boilerplate interface for service classes.

License

Notifications You must be signed in to change notification settings

GlenCrawford/service_interface

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ServiceInterface

A Ruby module that can be included into service classes that provides a strict, boilerplate interface, taking care of defining a class-level execute method, instantiating the service and invoking the instance-level execute method, and defining and setting the arguments (and default values) of the service (both required and optional).

Usage

Add the gem to your Gemfile:

gem 'service_interface', '~> 1.0'

Then create a service class, include the interface, and define the arguments, like so:

class TestService
  include ServiceInterface

  arguments :word, count: 5, suffix: nil

  def execute
    (Array.new(@count, @word) << @suffix).compact.join(', ')
  end
end

Then invoke the class like so:

TestService.execute(word: 'Ruby', suffix: 'Yay!')
=> 'Ruby, Ruby, Ruby, Ruby, Ruby, Yay!'

The equivalent code, written without ServiceInterface, would look something like this:

class TestService
  def self.execute(word:, count: 5, suffix: nil)
    new(
      word: word,
      count: count,
      suffix: suffix
    ).send(:execute)
  end

  def initialize(word:, count:, suffix:)
    @word = word
    @count = count
    @suffix = suffix
  end

  private

  def execute
    (Array.new(@count, @word) << @suffix).compact.join(', ')
  end
end

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/GlenCrawford/service_interface.

License

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

About

Ruby module to provide a strict, boilerplate interface for service classes.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages