Skip to content

Instantly share code, notes, and snippets.

@matheusazzi
Last active May 18, 2023 11:36
Show Gist options
  • Select an option

  • Save matheusazzi/ae04d27e1bdf3b57a30469d9f460c437 to your computer and use it in GitHub Desktop.

Select an option

Save matheusazzi/ae04d27e1bdf3b57a30469d9f460c437 to your computer and use it in GitHub Desktop.
class ThingService
attr_reader :thing, :params
Result = Struct.new(:errors, :things) do
def success?
errors.empty?
end
end
def self.call(thing = ThingModel.new, params)
new(thing, params).call
end
def call
@something ||= find_things
if @something.success?
Result.new([...], nil)
else
Result.new([], '...')
end
end
private
def initialize(thing, params)
@thing = thing
@params = params
end
def find_things
# ...
end
end
# Usage
t = ThingService.call(params) # => ThingService::Result
t.success? # => true
t.errors # => []
t.things # => [Thing, Thing, ...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment