<commit_before>module DataMapper
  module Validations

    # TODO: this Exception class is not referenced within dm-validations
    #   any reason not to remove it?
    class ValidationError < StandardError; end

    class ErrorSet
      extend Deprecate

      deprecate :clear!, :clear

      def self.default_error_message(violation_type, attribute_name, *violation_data)
        MessageTransformer::Default.error_message(violation_type, attribute_name, *violation_data)
      end
    end

    class ContextualRuleSet
      extend Deprecate

      deprecate :clear!,  :clear

      def execute(context_name, resource)
        # warn "#{self.class}#execute is deprecated. Use #{self.class}#validate instead."
        context(context_name).execute(resource)
      end
    end

    class Rule
      extend Deprecate

      # TODO: remove :field_name alias
      deprecate :field_name, :attribute_name

      # Call the validator. "call" is used so the operation is BoundMethod
      # and Block compatible. This must be implemented in all concrete
      # classes.
      #
      # @param [Object] resource
      #   The resource that the validator must be called against.
      #
      # @return [Boolean]
      #   true if valid, otherwise false.
      #
      def call(resource)
        # warn "#{self.class}#call is deprecated and will be removed in a future version (#{caller[0]})"
        return true if valid?(resource)

        error_message = self.custom_message ||
          MessageTransformer::Default.error_message(
            violation_type(resource),
            attribute_name,
            *violation_data(resource))

        add_error(resource, error_message, attribute_name)

        false
      end

      class Block
        def call(resource)
          result, error_message = resource.instance_eval(&self.block)
          add_error(resource, error_message, attribute_name) unless result
          result
        end
      end

      class Method
        def call(resource)
          result, error_message = resource.__send__(method)
          add_error(resource, error_message, attribute_name) unless result
          result
        end
      end

    end

    class RuleSet
      extend Deprecate

      # This is present to provide a backwards-compatible codepath to
      # ContextualRuleSet#execute
      def execute(resource)
        rules = rules_for_resource(resource)
        rules.map { |rule| rule.call(resource) }.all?
      end
    end

    class Violation
      # TODO: Extract the correct custom message for a Rule's context
      # in ContextualRuleSet#add
      def [](context_name)
        warn "Accessing custom messages by context name will be removed in a future version (#{caller[0]})"
        @custom_message[context_name]
      end
    end

    module Macros
      extend Deprecate

      deprecate :validates_absent,        :validates_absence_of
      deprecate :validates_format,        :validates_format_of
      deprecate :validates_present,       :validates_presence_of
      deprecate :validates_length,        :validates_length_of
      deprecate :validates_is_accepted,   :validates_acceptance_of
      deprecate :validates_is_confirmed,  :validates_confirmation_of
      deprecate :validates_is_number,     :validates_numericality_of
      deprecate :validates_is_primitive,  :validates_primitive_type_of
      deprecate :validates_is_unique,     :validates_uniqueness_of
    end

    module Inferred
      extend Deprecate<commit_msg>Silence deprecation warning on Violation#[] (temporarily).<commit_after>module DataMapper
  module Validations

    # TODO: this Exception class is not referenced within dm-validations
    #   any reason not to remove it?
    class ValidationError < StandardError; end

    class ErrorSet
      extend Deprecate

      deprecate :clear!, :clear

      def self.default_error_message(violation_type, attribute_name, *violation_data)
        MessageTransformer::Default.error_message(violation_type, attribute_name, *violation_data)
      end
    end

    class ContextualRuleSet
      extend Deprecate

      deprecate :clear!,  :clear

      def execute(context_name, resource)
        # warn "#{self.class}#execute is deprecated. Use #{self.class}#validate instead."
        context(context_name).execute(resource)
      end
    end

    class Rule
      extend Deprecate

      # TODO: remove :field_name alias
      deprecate :field_name, :attribute_name

      # Call the validator. "call" is used so the operation is BoundMethod
      # and Block compatible. This must be implemented in all concrete
      # classes.
      #
      # @param [Object] resource
      #   The resource that the validator must be called against.
      #
      # @return [Boolean]
      #   true if valid, otherwise false.
      #
      def call(resource)
        # warn "#{self.class}#call is deprecated and will be removed in a future version (#{caller[0]})"
        return true if valid?(resource)

        error_message = self.custom_message ||
          MessageTransformer::Default.error_message(
            violation_type(resource),
            attribute_name,
            *violation_data(resource))

        add_error(resource, error_message, attribute_name)

        false
      end

      class Block
        def call(resource)
          result, error_message = resource.instance_eval(&self.block)
          add_error(resource, error_message, attribute_name) unless result
          result
        end
      end

      class Method
        def call(resource)
          result, error_message = resource.__send__(method)
          add_error(resource, error_message, attribute_name) unless result
          result
        end
      end

    end

    class RuleSet
      extend Deprecate

      # This is present to provide a backwards-compatible codepath to
      # ContextualRuleSet#execute
      def execute(resource)
        rules = rules_for_resource(resource)
        rules.map { |rule| rule.call(resource) }.all?
      end
    end

    class Violation
      # TODO: Extract the correct custom message for a Rule's context
      # in ContextualRuleSet#add
      def [](context_name)
        # warn "Accessing custom messages by context name will be removed in a future version (#{caller[0]})"
        @custom_message[context_name]
      end
    end

    module Macros
      extend Deprecate

      deprecate :validates_absent,        :validates_absence_of
      deprecate :validates_format,        :validates_format_of
      deprecate :validates_present,       :validates_presence_of
      deprecate :validates_length,        :validates_length_of
      deprecate :validates_is_accepted,   :validates_acceptance_of
      deprecate :validates_is_confirmed,  :validates_confirmation_of
      deprecate :validates_is_number,     :validates_numericality_of
      deprecate :validates_is_primitive,  :validates_primitive_type_of
      deprecate :validates_is_unique,     :validates_uniqueness_of
    end

    module Inferred
      extend Deprecate
{'fraction_non_alphanumeric': 0.06250889933077032, 'fraction_numerical': 0.0005695571693008686, 'mean_word_length': 2.628099173553719, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5311036', 'n_tokens_mistral': 1975, 'n_tokens_neox': 1915, 'n_words': 536}