Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding code comments #9

Merged
merged 1 commit into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,5 @@ AllCops:
- gemfiles/*
- vendor/**/*

Style/Documentation:
Enabled: false

Metrics/BlockLength:
IgnoredMethods: ['describe']
5 changes: 5 additions & 0 deletions lib/rails/generators/service/service_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

module Rails
module Generators
# Create AppicationService and SubService classes using Rails generators.
#
# E.g:
# bin/rails service my_service
#
class ServiceGenerator < Rails::Generators::NamedBase
desc 'This generator creates a service file at app/services'

Expand Down
11 changes: 11 additions & 0 deletions lib/service_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
require 'service_record/callbacks'

module ServiceRecord
# Base class to be extended by all service classes
#
# class MyService < ServiceRecord
# end
#
class Base
include Callbacks
include ActiveModel::Attributes
Expand All @@ -13,6 +18,8 @@ class Base

attr_accessor :result

# Wrapper around the *perform* instance method that runs all the validations
# and callbacks before eventually calling *perform*.
def self.perform(args = {})
new.tap do |service|
service.attributes = args
Expand All @@ -24,21 +31,25 @@ def self.perform(args = {})
end
end

# Wapper around the *perform* class method that raises exception if service fails
def self.perform!(args = {})
service = perform(args)
return service if service.success?

raise Failure, service
end

# Checks the service for errors. Returns +true+ if no errors are found, +false+ otherwise.
def success?
errors.empty?
end

# Checks the service for errors. Returns +false+ if no errors are found, +true+ otherwise.
def failure?
!success?
end

# Each subclass must define the *perform* method
def perform
raise NotImplementedError
end
Expand Down
1 change: 1 addition & 0 deletions lib/service_record/callbacks.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

module ServiceRecord
# Defines before/around/after callbacks for the 'perform' method
module Callbacks
extend ActiveSupport::Concern

Expand Down
1 change: 1 addition & 0 deletions lib/service_record/failure.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

module ServiceRecord
# Exception to be raised when a service fails
class Failure < StandardError
attr_reader :service

Expand Down