Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/brett-richardson/base_service

Base Service for Ruby Objects
https://github.com/brett-richardson/base_service

oop ruby ruby-gem ruby-library ruby-on-rails rubygem services solid

Last synced: about 1 month ago
JSON representation

Base Service for Ruby Objects

Awesome Lists containing this project

README

        

BaseService
===========

The client should extend this module, in order to implement this common
service object pattern.

### Advanced Syntax

```ruby

MyService.new(param1, param2).call do |on|
on.success { |result| puts "Success #{result}" }

on.failure { |error| puts "Generic failure #{error}" }
on.failure(:param1_error) { |error| puts "Problem with param1 #{param1}" }
on.failure(:param2_error) { |error| puts "Problem with param1 #{param1}" }
end

```

### Semi-Advanced Syntax

```ruby

MyService.new(param1, param2).call do |on|
puts "Result: #{on.result}"
end

```

## Support for (depreacted) legacy behaviour

No command-query separation. :(

```ruby

result = MyService.new(param1, param2).call

```

## Example Service

```ruby

class MyService
include BaseService

def initialize(param1, param2)
@param1, @param2 = param1, param2
end

private
attr_reader :param1, :param2

def perform
return failure "no param1", :param1_error unless param1.present?
return failure "no param2", :param2_error unless param2.present?
return failure "neither param1 or 2" unless param1.present? && param2.present?

return "success!"
end
end

```