Skip to content

Commit

Permalink
Add validations to the required fields
Browse files Browse the repository at this point in the history
  • Loading branch information
AnotherRegularDude committed Aug 15, 2023
1 parent 11a1887 commit 105f7d9
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/table_sync/publishing/batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class TableSync::Publishing::Batch
include Tainbox
include TableSync::Utils::RequiredValidator

attribute :object_class
attribute :original_attributes
Expand All @@ -11,6 +12,8 @@ class TableSync::Publishing::Batch

attribute :event, default: :update

require_attributes :object_class, :original_attributes

def publish_later
job.perform_later(job_attributes)
end
Expand Down
3 changes: 3 additions & 0 deletions lib/table_sync/publishing/raw.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class TableSync::Publishing::Raw
include Tainbox
include TableSync::Utils::RequiredValidator

attribute :model_name
attribute :table_name
Expand All @@ -13,6 +14,8 @@ class TableSync::Publishing::Raw

attribute :event, default: :update

require_attributes :model_name, :original_attributes

def publish_now
message.publish
end
Expand Down
1 change: 1 addition & 0 deletions lib/table_sync/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ module Utils
require_relative "utils/proc_array"
require_relative "utils/proc_keywords_resolver"
require_relative "utils/interface_checker"
require_relative "utils/required_validator"
end
end
43 changes: 43 additions & 0 deletions lib/table_sync/utils/required_validator.rb
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
3 changes: 3 additions & 0 deletions spec/publishing/batch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

include_examples "publisher#publish_now with stubbed message",
TableSync::Publishing::Message::Batch
include_examples "publisher#new without expected fields",
TableSync::Publishing::Batch,
%i[object_class original_attributes]

context "real user" do
context "sequel" do
Expand Down
4 changes: 4 additions & 0 deletions spec/publishing/raw_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@
include_examples "publisher#publish_now without stubbed message",
TableSync::Publishing::Message::Raw
end

include_examples "publisher#new without expected fields",
TableSync::Publishing::Raw,
%i[model_name original_attributes]
end
16 changes: 16 additions & 0 deletions spec/support/shared/publishers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@
end
end

# needs let(:attributes)
shared_examples "publisher#new without expected fields" do |publisher_class, required_attributes|
required_attributes.each do |attribute|
context "without #{attribute}" do
it "raises an error" do
expect { publisher_class.new(attributes.except(attribute)) }.to raise_error do |error|
expect(error).to be_an_instance_of(ArgumentError)
expect(error.message).to eq(
"Some of required attributes is not provided: [:#{attribute}]",
)
end
end
end
end
end

# needs let(:existing_user)
shared_examples "publisher#publish_now with real user, for given orm" do |orm|
let(:user) { DB[:users].where(id: existing_user.id).first }
Expand Down

0 comments on commit 105f7d9

Please sign in to comment.