-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
345 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
require 'active_model' | ||
|
||
module AuxiliaryRails | ||
class AbstractCommand | ||
include ActiveModel::Model | ||
include ActiveModel::Attributes | ||
|
||
def self.call(*args) | ||
new(*args).call | ||
end | ||
|
||
def call | ||
raise NotImplementedError | ||
end | ||
|
||
def errors | ||
@errors ||= ActiveModel::Errors.new(self) | ||
end | ||
|
||
def failure? | ||
status?(:failure) | ||
end | ||
|
||
def status?(value) | ||
ensure_execution! | ||
|
||
status == value&.to_sym | ||
end | ||
|
||
def success? | ||
status?(:success) | ||
end | ||
|
||
def transaction(&block) | ||
ActiveRecord::Base.transaction(&block) if block_given? | ||
end | ||
|
||
# Method for ActiveModel::Errors | ||
def read_attribute_for_validation(attr_name) | ||
if attr_name == :command | ||
self | ||
else | ||
attribute(attr_name) | ||
end | ||
end | ||
|
||
# Method for ActiveModel::Translation | ||
def self.i18n_scope | ||
:commands | ||
end | ||
|
||
protected | ||
|
||
attr_accessor :status | ||
|
||
def ensure_empty_errors! | ||
return if errors.empty? | ||
|
||
error!("`#{self.class}` contains errors.") | ||
end | ||
|
||
def ensure_empty_status! | ||
return if status.nil? | ||
|
||
error!("`#{self.class}` was already executed.") | ||
end | ||
|
||
def ensure_execution! | ||
return if status.present? | ||
|
||
error!("`#{self.class}` was not executed yet.") | ||
end | ||
|
||
def error!(message = nil) | ||
message ||= "`#{self.class}` raised error." | ||
raise ApplicationError, message | ||
end | ||
|
||
def failure!(message = nil) | ||
ensure_empty_status! | ||
|
||
errors.add(:command, :failed, message: message) unless message.nil? | ||
|
||
self.status = :failure | ||
self | ||
end | ||
|
||
def success! | ||
ensure_empty_errors! | ||
ensure_empty_status! | ||
|
||
self.status = :success | ||
self | ||
end | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
lib/generators/auxiliary_rails/install_commands_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require 'rails' | ||
|
||
module AuxiliaryRails | ||
class InstallCommandsGenerator < ::Rails::Generators::Base | ||
source_root File.expand_path('templates', __dir__) | ||
|
||
def copy_application_command_file | ||
copy_file 'application_command_template.rb', | ||
'app/commands/application_command.rb' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require 'rails' | ||
|
||
module AuxiliaryRails | ||
class InstallGenerator < ::Rails::Generators::Base | ||
def install | ||
generate 'auxiliary_rails:install_commands' | ||
generate 'auxiliary_rails:install_errors' | ||
generate 'auxiliary_rails:install_rubocop --no-specify-gems' | ||
end | ||
end | ||
end |
2 changes: 2 additions & 0 deletions
2
lib/generators/auxiliary_rails/templates/application_command_template.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class ApplicationCommand < AuxiliaryRails::AbstractCommand | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
RSpec.describe AuxiliaryRails::AbstractCommand do | ||
describe '#call' do | ||
subject(:cmd_class) { described_class } | ||
|
||
it 'raises NotImplementedError exception' do | ||
expect { cmd_class.call }.to raise_error NotImplementedError | ||
end | ||
end | ||
|
||
describe '.call' do | ||
subject(:cmd) { described_class.new } | ||
|
||
it 'raises NotImplementedError exception' do | ||
expect { cmd.call }.to raise_error NotImplementedError | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
require 'support/sample_classes/sample_commands' | ||
|
||
RSpec.describe SampleCommands do | ||
describe 'SuccessCommand' do | ||
describe '#call' do | ||
subject(:cmd_class) { SampleCommands::SuccessCommand } | ||
|
||
it do | ||
expect(cmd_class.call).to be_a cmd_class | ||
end | ||
end | ||
|
||
describe '.call' do | ||
subject(:cmd) { SampleCommands::SuccessCommand.new } | ||
|
||
it 'returns self as a result' do | ||
expect(cmd.call).to be cmd | ||
end | ||
|
||
it do | ||
expect(cmd.call).to be_success | ||
end | ||
end | ||
end | ||
|
||
describe 'DoubleStatusSetCommand' do | ||
subject(:cmd) { SampleCommands::DoubleStatusSetCommand.new } | ||
|
||
describe '.call' do | ||
it do | ||
expect { cmd.call }.to raise_error ApplicationError, | ||
'`SampleCommands::DoubleStatusSetCommand` was already executed.' | ||
end | ||
end | ||
end | ||
|
||
describe 'SuccessWithErrorsCommand' do | ||
subject(:cmd) { SampleCommands::SuccessWithErrorsCommand.new } | ||
|
||
describe '.call' do | ||
it do | ||
expect { cmd.call }.to raise_error ApplicationError, | ||
'`SampleCommands::SuccessWithErrorsCommand` contains errors.' | ||
end | ||
end | ||
end | ||
|
||
describe 'FailureWithErrorsCommand' do | ||
subject(:cmd) { SampleCommands::FailureWithErrorsCommand.new } | ||
|
||
describe '.call' do | ||
it do | ||
expect { cmd.call }.to change(cmd.errors, :count).from(0).to(1) | ||
end | ||
|
||
it do | ||
puts cmd.call.errors.full_messages | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.