-
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
1 parent
9c69e75
commit fb442af
Showing
7 changed files
with
366 additions
and
161 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
module IDoNotNeedInteractor | ||
class Context < Hash # rubocop:disable Style/Documentation | ||
attr_reader :errors, :_executed | ||
|
||
def initialize(**initial_values) | ||
super() | ||
@errors = [] | ||
@_executed = [] | ||
merge! initial_values | ||
end | ||
|
||
def register(object) | ||
@_executed << object | ||
end | ||
|
||
def success? = errors.empty? | ||
|
||
def failure? = !success? | ||
|
||
def failed = _executed.find { _1.failed == true } | ||
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,32 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_model" | ||
|
||
module IDoNotNeedInteractor | ||
module Contract | ||
module ActiveModel # rubocop:disable Style/Documentation | ||
def self.included(descendant) # rubocop:disable Metrics/MethodLength | ||
class << descendant | ||
def contract(&block) | ||
@contract = Class.new do | ||
include ::ActiveModel::API | ||
include ::ActiveModel::Attributes | ||
include ::ActiveModel::Validations | ||
|
||
def self.model_name | ||
::ActiveModel::Name.new(self, nil, "temp") | ||
end | ||
end | ||
@contract.class_exec(&block) | ||
end | ||
end | ||
end | ||
|
||
def validate(ctx) | ||
self.class.instance_variable_get(:@contract).new(**ctx).validate! | ||
rescue ::ActiveModel::ValidationError => e | ||
ctx.errors << e.message | ||
end | ||
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,23 @@ | ||
# frozen_string_literal: true | ||
|
||
require "dry-validation" | ||
|
||
module IDoNotNeedInteractor | ||
module Contract | ||
module DryValidation # rubocop:disable Style/Documentation | ||
def self.included(descendant) | ||
class << descendant | ||
def contract(&block) | ||
@contract = Class.new(::Dry::Validation::Contract) | ||
@contract.class_exec(&block) | ||
end | ||
end | ||
end | ||
|
||
def validate(ctx) | ||
validation_result = self.class.instance_variable_get(:@contract).new.call(ctx) | ||
ctx.errors << validation_result.errors.to_h if validation_result.errors.any? | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.