<commit_before># encoding: utf-8
module Mongoid #:nodoc:
  module Criterion #:nodoc:

    class Selector < Hash
      attr_reader :klass

      def initialize(klass)
        @klass = klass
      end

      def []=(key, value)
        super(key, try_to_typecast(key, value))
      end

      def merge!(other)
        other.each_pair do |key, value|
          self[key] = value
        end
        self
      end
      alias update merge!

      private

      def try_to_typecast(key, value)
        access = key.to_s
        return value unless klass.fields.has_key?(access)

        field = klass.fields[access]
        typecast_value_for(field, value)
      end

      def typecast_value_for(field, value)
        return value if field.type === value
        case value
        when Hash
          value = value.dup
          value.each_pair do |k, v|
            unless %w($exists $size $elemMatch).include?(k)
              value[k] = typecast_value_for(field, v)
            end
          end
        when Array
          value.map { |v| typecast_value_for(field, v) }
        when Regexp
          value
        else
          field.set(value)
        end
      end

    end

  end
end<commit_msg>Typecast $elemMatch<commit_after># encoding: utf-8
module Mongoid #:nodoc:
  module Criterion #:nodoc:

    class Selector < Hash
      attr_reader :klass

      def initialize(klass)
        @klass = klass
      end

      def []=(key, value)
        super(key, try_to_typecast(key, value))
      end

      def merge!(other)
        other.each_pair do |key, value|
          self[key] = value
        end
        self
      end
      alias update merge!

      private

      def try_to_typecast(key, value)
        access = key.to_s
        return value unless klass.fields.has_key?(access)

        field = klass.fields[access]
        typecast_value_for(field, value)
      end

      def typecast_value_for(field, value)
        return value if field.type === value
        case value
        when Hash
          value = value.dup
          value.each_pair do |k, v|
            unless %w($exists $size).include?(k)
              value[k] = typecast_value_for(field, v)
            end
          end
        when Array
          value.map { |v| typecast_value_for(field, v) }
        when Regexp
          value
        else
          field.set(value)
        end
      end

    end

  end
end
{'fraction_non_alphanumeric': 0.08156471077819392, 'fraction_numerical': 0.0008322929671244278, 'mean_word_length': 1.6388583973655324, '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': '6324037', 'n_tokens_mistral': 730, 'n_tokens_neox': 726, 'n_words': 195}