-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validations to the required fields
- Loading branch information
1 parent
11a1887
commit 105f7d9
Showing
7 changed files
with
73 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
module TableSync::Utils::RequiredValidator | ||
module PrependedInitialization | ||
def initialize(*) | ||
super | ||
|
||
not_filled_attrs = calculate_not_filled_attributes | ||
if not_filled_attrs.present? | ||
raise( | ||
ArgumentError, | ||
"Some of required attributes is not provided: #{not_filled_attrs.inspect}", | ||
) | ||
end | ||
end | ||
end | ||
|
||
module ClassMethods | ||
def require_attributes(*attributes) | ||
_required_attributes.push(*attributes) | ||
end | ||
|
||
def _required_attributes | ||
@_required_attributes ||= [] | ||
end | ||
end | ||
|
||
module InstanceMethods | ||
private | ||
|
||
def calculate_not_filled_attributes | ||
attributes | ||
.select { |key, value| key.in?(self.class._required_attributes) && value.blank? } | ||
.keys | ||
end | ||
end | ||
|
||
def self.included(klass) | ||
klass.prepend(PrependedInitialization) | ||
klass.extend(ClassMethods) | ||
klass.include(InstanceMethods) | ||
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
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