<commit_before>module EasySerializer
  class Base
    delegate :to_json, :[], to: :serialize
    attr_reader :klass_ins
    def initialize(klass_ins)
      @klass_ins = klass_ins
    end

    class << self
      def attribute(name, opts = {}, &block)
        @__serializable_attributes ||= []
        @__serializable_attributes << { name: name, block: block }.merge(opts)
      end

      def cache(bool, opts = {}, &block)
        if bool
          @__cache = opts.merge(block: block)
        end
      end

      def attributes(*args)
        args.each do |input|
          attribute(*input)
        end
      end

      def collection(name, opts = {}, &block)
        opts = opts.merge(collection: true)
        attribute(name, opts, &block)
      end
    end

    def serialize
      @serialize ||= serialized_from_cache || _serialize
    end
    alias_method :to_h, :serialize
    alias_method :to_hash, :serialize
    alias_method :to_s, :to_json

    private

    def _serialize
      __serializable_attributes.each_with_object(HashWithIndifferentAccess.new) do |setup, object|
        if setup[:key] === false
          object.merge!(attr_serializer(setup))
        else
          key = (setup[:key] ? setup[:key] : setup[:name])
          object[key] = attr_serializer(setup)
        end
      end
    end

    def serialized_from_cache
      return false unless ActiveSerializer.perform_caching
      cache = __cache
      return false unless cache
      if cache[:block]
        cache[:block].call(klass_ins, self)
      else
        key = if cache[:key]
          cache[:key].call(klass_ins)
        else
          [klass_ins, 'EasySerialized']
        end
        fail "[Serializer] No key for cache" unless key
        ActiveSerializer.cache.fetch(key) { _serialize }
      end
    end

    def __cache
      self.class.instance_variable_get(:@__cache)
    end

    def __serializable_attributes
      self.class.instance_variable_get(:@__serializable_attributes) || []
    end

    def attr_serializer(setup)
      return setup[:block].call(klass_ins) if setup[:block]
      content = klass_ins.send(setup[:name])
      return content unless serializer = setup[:serializer]
      if setup[:collection]
        content.map { |o|  cache_or_serialize(serializer, o, setup) }
      else
        cache_or_serialize(serializer, content, setup)
      end
    end

    def cache_or_serialize(serializer, content, opts)
      return unless content
      if ActiveSerializer.perform_caching && opts[:cache]
        key = if opts[:cache_key]
          opts[:cache_key].call(content)
        else
          [content, 'EasySerialized']
        end
        # Be Aware
        # We are cahing the serialized object
        ActiveSerializer.cache.fetch(key) { send_to_serializer(serializer, content) }
      else
        send_to_serializer(serializer, content)
      end
    end

    def from_setup_serializer(serializer, content)
      case serializer
      when Proc
        serializer.call(self, klass_ins, content)
      else
        serializer
      end
    end

    def send_to_serializer(serializer, content)
      return unless content
      from_setup_serializer(serializer, content).new(content).serialize
    end
  end
end<commit_msg>Remove references to previous name ActiveSerializer to EasySerializer<commit_after>module EasySerializer
  class Base
    delegate :to_json, :[], to: :serialize
    attr_reader :klass_ins
    def initialize(klass_ins)
      @klass_ins = klass_ins
    end

    class << self
      def attribute(name, opts = {}, &block)
        @__serializable_attributes ||= []
        @__serializable_attributes << { name: name, block: block }.merge(opts)
      end

      def cache(bool, opts = {}, &block)
        if bool
          @__cache = opts.merge(block: block)
        end
      end

      def attributes(*args)
        args.each do |input|
          attribute(*input)
        end
      end

      def collection(name, opts = {}, &block)
        opts = opts.merge(collection: true)
        attribute(name, opts, &block)
      end
    end

    def serialize
      @serialize ||= serialized_from_cache || _serialize
    end
    alias_method :to_h, :serialize
    alias_method :to_hash, :serialize
    alias_method :to_s, :to_json

    private

    def _serialize
      __serializable_attributes.each_with_object(HashWithIndifferentAccess.new) do |setup, object|
        if setup[:key] === false
          object.merge!(attr_serializer(setup))
        else
          key = (setup[:key] ? setup[:key] : setup[:name])
          object[key] = attr_serializer(setup)
        end
      end
    end

    def serialized_from_cache
      return false unless EasySerializer.perform_caching
      cache = __cache
      return false unless cache
      if cache[:block]
        cache[:block].call(klass_ins, self)
      else
        key = if cache[:key]
          cache[:key].call(klass_ins)
        else
          [klass_ins, 'EasySerialized']
        end
        fail "[Serializer] No key for cache" unless key
        EasySerializer.cache.fetch(key) { _serialize }
      end
    end

    def __cache
      self.class.instance_variable_get(:@__cache)
    end

    def __serializable_attributes
      self.class.instance_variable_get(:@__serializable_attributes) || []
    end

    def attr_serializer(setup)
      return setup[:block].call(klass_ins) if setup[:block]
      content = klass_ins.send(setup[:name])
      return content unless serializer = setup[:serializer]
      if setup[:collection]
        content.map { |o|  cache_or_serialize(serializer, o, setup) }
      else
        cache_or_serialize(serializer, content, setup)
      end
    end

    def cache_or_serialize(serializer, content, opts)
      return unless content
      if EasySerializer.perform_caching && opts[:cache]
        key = if opts[:cache_key]
          opts[:cache_key].call(content)
        else
          [content, 'EasySerialized']
        end
        # Be Aware
        # We are cahing the serialized object
        EasySerializer.cache.fetch(key) { send_to_serializer(serializer, content) }
      else
        send_to_serializer(serializer, content)
      end
    end

    def from_setup_serializer(serializer, content)
      case serializer
      when Proc
        serializer.call(self, klass_ins, content)
      else
        serializer
      end
    end

    def send_to_serializer(serializer, content)
      return unless content
      from_setup_serializer(serializer, content).new(content).serialize
    end
  end
end
{'fraction_non_alphanumeric': 0.0872093023255814, 'fraction_numerical': 0.0, 'mean_word_length': 2.4261006289308176, 'pattern_counts': {'":': 0, '<': 11, '<?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': '4376284', 'n_tokens_mistral': 1882, 'n_tokens_neox': 1869, 'n_words': 458}