<commit_before># frozen_string_literal: true

module CanCan
  module ConditionsMatcher
    # Matches the block or conditions hash
    def matches_conditions?(action, subject, attribute = nil, *extra_args)
      return call_block_with_all(action, subject, extra_args) if @match_all
      return matches_block_conditions(subject, attribute, *extra_args) if @block
      return matches_non_block_conditions(subject) unless conditions_empty?

      true
    end

    private

    def subject_class?(subject)
      klass = (subject.is_a?(Hash) ? subject.values.first : subject).class
      [Class, Module].include? klass
    end

    def matches_block_conditions(subject, *extra_args)
      return @base_behavior if subject_class?(subject)

      @block.call(subject, *extra_args.compact)
    end

    def matches_non_block_conditions(subject)
      return nested_subject_matches_conditions?(subject) if subject.class == Hash
      return matches_conditions_hash?(subject) unless subject_class?(subject)

      # Don't stop at "cannot" definitions when there are conditions.
      @base_behavior
    end

    def nested_subject_matches_conditions?(subject_hash)
      parent, _child = subject_hash.first
      matches_conditions_hash?(parent, @conditions[parent.class.name.downcase.to_sym] || {})
    end

    # Checks if the given subject matches the given conditions hash.
    # This behavior can be overridden by a model adapter by defining two class methods:
    # override_matching_for_conditions?(subject, conditions) and
    # matches_conditions_hash?(subject, conditions)
    def matches_conditions_hash?(subject, conditions = @conditions)
      return true if conditions.is_a?(Hash) && conditions.empty?

      adapter = model_adapter(subject)

      if adapter.override_conditions_hash_matching?(subject, conditions)
        return adapter.matches_conditions_hash?(subject, conditions)
      end

      matches_all_conditions?(adapter, conditions, subject)
    end

    def matches_all_conditions?(adapter, conditions, subject)
      if conditions.is_a?(Hash)
        matches_hash_conditions(adapter, conditions, subject)
      elsif conditions.respond_to?(:include?)
        conditions.include?(subject)
      else
        subject == conditions
      end
    end

    def matches_hash_conditions(adapter, conditions, subject)
      conditions.all? do |name, value|
        if adapter.override_condition_matching?(subject, name, value)
          adapter.matches_condition?(subject, name, value)
        else
          condition_match?(subject.send(name), value)
        end
      end<commit_msg>Swap arguments of some methods to be consistent (#777)<commit_after># frozen_string_literal: true

module CanCan
  module ConditionsMatcher
    # Matches the block or conditions hash
    def matches_conditions?(action, subject, attribute = nil, *extra_args)
      return call_block_with_all(action, subject, extra_args) if @match_all
      return matches_block_conditions(subject, attribute, *extra_args) if @block
      return matches_non_block_conditions(subject) unless conditions_empty?

      true
    end

    private

    def subject_class?(subject)
      klass = (subject.is_a?(Hash) ? subject.values.first : subject).class
      [Class, Module].include? klass
    end

    def matches_block_conditions(subject, *extra_args)
      return @base_behavior if subject_class?(subject)

      @block.call(subject, *extra_args.compact)
    end

    def matches_non_block_conditions(subject)
      return nested_subject_matches_conditions?(subject) if subject.class == Hash
      return matches_conditions_hash?(subject) unless subject_class?(subject)

      # Don't stop at "cannot" definitions when there are conditions.
      @base_behavior
    end

    def nested_subject_matches_conditions?(subject_hash)
      parent, _child = subject_hash.first
      matches_conditions_hash?(parent, @conditions[parent.class.name.downcase.to_sym] || {})
    end

    # Checks if the given subject matches the given conditions hash.
    # This behavior can be overridden by a model adapter by defining two class methods:
    # override_matching_for_conditions?(subject, conditions) and
    # matches_conditions_hash?(subject, conditions)
    def matches_conditions_hash?(subject, conditions = @conditions)
      return true if conditions.is_a?(Hash) && conditions.empty?

      adapter = model_adapter(subject)

      if adapter.override_conditions_hash_matching?(subject, conditions)
        return adapter.matches_conditions_hash?(subject, conditions)
      end

      matches_all_conditions?(adapter, subject, conditions)
    end

    def matches_all_conditions?(adapter, subject, conditions)
      if conditions.is_a?(Hash)
        matches_hash_conditions(adapter, subject, conditions)
      elsif conditions.respond_to?(:include?)
        conditions.include?(subject)
      else
        puts "does #{subject} match #{conditions}?"
        subject == conditions
      end
    end

    def matches_hash_conditions(adapter, subject, conditions)
      conditions.all? do |name, value|
        if adapter.override_condition_matching?(subject, name, value)
          adapter.matches_condition?(subject, name, value)
        else
          condition_match?(subject.send(name), value)
        end
      end
{'fraction_non_alphanumeric': 0.07897222747024372, 'fraction_numerical': 0.0005667863215567731, 'mean_word_length': 3.7522441651705565, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3606561', 'n_tokens_mistral': 1416, 'n_tokens_neox': 1414, 'n_words': 366}