<commit_before># coding: utf-8

module Thinreports
  module Report

    class Internal
      attr_reader :pages
      attr_reader :page
      attr_reader :page_count
      attr_reader :default_layout
      attr_reader :layout_registry
      attr_reader :events

      attr_accessor :page_create_handler
      attr_accessor :generate_handler

      # @param [Thinreports::Report::Base] report
      # @param options (see Thinreports::Report::Base#initialize)
      def initialize(report, options)
        @report = report
        # Default layout
        @default_layout = prepare_layout(options[:layout])

        @layout_registry = {}
        @finalized  = false
        @pages      = []
        @page       = nil
        @page_count = 0

        @page_create_handler = nil
        @generate_handler = nil

        @events = Report::Events.new
      end

      # @see Thinreports::Report::Base#use_layout
      def register_layout(layout, options = {}, &block)
        layout = if options.empty? || options[:default]
          @default_layout = init_layout(layout)
        else
          id = options[:id].to_sym

          if layout_registry.key?(id)
            raise ArgumentError, "Id :#{id} is already in use."
          end
          layout_registry[id] = init_layout(layout, id)
        end
        layout.config(&block)
      end

      def add_page(new_page)
        finalize_current_page
        insert_page(new_page)
      end

      def copy_page
        finalize_current_page(at: :copy)
        insert_page(page.copy)
      end

      def finalize
        unless finalized?
          finalize_current_page
          @finalized = true

          # [DEPRECATION] Please use Report::Base#on_generate callback instead.
          events.dispatch(Report::Events::Event.new(:generate,
                                                    @report, nil, pages))

          @generate_handler.call(pages) if @generate_handler
        end
      end

      def finalized?
        @finalized
      end

      def load_layout(id_or_filename)
        return @default_layout if id_or_filename.nil?

        layout = case id_or_filename
        when Symbol
          layout_registry[id_or_filename]
        when String
          prepare_layout(id_or_filename)
        else
          raise ArgumentError, 'Invalid argument for layout.'
        end
        @default_layout = layout unless @default_layout
        layout
      end

    private

      def insert_page(new_page)
        @pages << new_page

        if new_page.count?
          @page_count += 1<commit_msg>Deleted unnecessary code, debris of obsoleted the callbacks (pointed out in #27)<commit_after># coding: utf-8

module Thinreports
  module Report

    class Internal
      attr_reader :pages
      attr_reader :page
      attr_reader :page_count
      attr_reader :default_layout
      attr_reader :layout_registry
      attr_reader :events

      attr_accessor :page_create_handler

      # @param [Thinreports::Report::Base] report
      # @param options (see Thinreports::Report::Base#initialize)
      def initialize(report, options)
        @report = report
        # Default layout
        @default_layout = prepare_layout(options[:layout])

        @layout_registry = {}
        @finalized  = false
        @pages      = []
        @page       = nil
        @page_count = 0

        @page_create_handler = nil

        @events = Report::Events.new
      end

      # @see Thinreports::Report::Base#use_layout
      def register_layout(layout, options = {}, &block)
        layout = if options.empty? || options[:default]
          @default_layout = init_layout(layout)
        else
          id = options[:id].to_sym

          if layout_registry.key?(id)
            raise ArgumentError, "Id :#{id} is already in use."
          end
          layout_registry[id] = init_layout(layout, id)
        end
        layout.config(&block)
      end

      def add_page(new_page)
        finalize_current_page
        insert_page(new_page)
      end

      def copy_page
        finalize_current_page(at: :copy)
        insert_page(page.copy)
      end

      def finalize
        unless finalized?
          finalize_current_page
          @finalized = true

          # [DEPRECATION] You can do the same implements as the :generate Event
          #   with the following code:
          #
          #   report = Thinreports::Report.new layout: 'foo.tlf'
          #   report.start_new_page do |page|
          #     page.item(:price).value = 100000
          #   end
          #
          #   report.pages.each do |page|
          #     page[:title] = 'Common Title'
          #   end
          #
          #   report.generate filename: 'foo.pdf'
          #
          events.dispatch(Report::Events::Event.new(:generate,
                                                    @report, nil, pages))
        end
      end

      def finalized?
        @finalized
      end

      def load_layout(id_or_filename)
        return @default_layout if id_or_filename.nil?

        layout = case id_or_filename
        when Symbol
          layout_registry[id_or_filename]
        when String
          prepare_layout(id_or_filename)
        else
          raise ArgumentError, 'Invalid argument for layout.'
        end
        @default_layout = layout unless @default_layout
        layout
      end

    private

      def insert_page(new_page)
        @pages << new_page

        if new_page.count?
          @page_count += 1
{'fraction_non_alphanumeric': 0.07682481751824817, 'fraction_numerical': 0.0025547445255474453, 'mean_word_length': 1.8681318681318682, 'pattern_counts': {'":': 0, '<': 7, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5506147', 'n_tokens_mistral': 1538, 'n_tokens_neox': 1484, 'n_words': 358}