Sha256: e28bb1037a037165941a47a4995c18255619ea3221aad7bbefa49c96882bddef

Contents?: true

Size: 948 Bytes

Versions: 3

Compression:

Stored size: 948 Bytes

Contents

# 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.nil? }
        .keys
    end
  end

  def self.included(klass)
    klass.prepend(PrependedInitialization)
    klass.extend(ClassMethods)
    klass.include(InstanceMethods)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
table_sync-6.5.1 lib/table_sync/utils/required_validator.rb
table_sync-6.5.0 lib/table_sync/utils/required_validator.rb
table_sync-6.4.2 lib/table_sync/utils/required_validator.rb