<commit_before>require 'yabass/page'

module Yabass
  module Router
    attr_reader :pages, :data
    def initialize(*args, &block)
      load_route_data
      init_routes
      super()
    end

    private 
      def load_route_data
        file_extensions = %w|yml rb|
        file_extensions.each do |ext|
          file_path = File.expand_path("data/index.#{ext}", root_path)
          if File.exist?(file_path)
            case ext
            when 'yml'
              @data = YAML.load(ERB.new(File.read(file_path)).result)
              break
            when 'rb'
              clean_room = Object.new
              @data = clean_room.instance_eval(File.read(file_path))
              break
            end
          end
        end
      end

      def init_routes
        console.error('Data is not loaded') if @data.nil?
        @pages = []
        class << @pages
          def routes; self.map(&:route); end
          def file_paths; self.map(&:file_path); end
          def data; self.map(&:data); end
          def parents; self.map(&:parent); end
        end
        @data.each do |k, v|
          set_index_route({k => v}, @data)
        end
      end

      def set_index_route(model, parent_element, prev_route = '', parents = '')
        model_name = model.keys.first
        list = model.values.first
        parents = "#{parents}/#{model_name}"
        file_path = File.expand_path("views#{parents}/index.erb", root_path)
        file_exists = File.exist?(file_path)
        hidden = /^_/ =~ model_name
        console.warn("Index view file for '#{parents}' => #{file_path} was not found") if !hidden && !file_exists
        if !hidden && file_exists
          new_route = "#{prev_route}/#{model_name}"
          page = Page.new(new_route, file_path, list, parent_element)
          @pages.push(page)
        end
        if list.kind_of?(Array)
          list.each do |element|
            set_element_route(element, parent_element, new_route || prev_route, parents)
          end
        end
      end

      def set_element_route(element, parent_element, prev_route, parents)
        key = element['key'] || element['id']<commit_msg>Updated router<commit_after>require 'yabass/page'

module Yabass
  module Router
    attr_reader :pages, :data
    def initialize(*args, &block)
      load_route_data
      init_routes
      super()
    end

    private 
      def load_route_data
        file_extensions = %w|yml rb|
        file_extensions.each do |ext|
          file_path = File.expand_path("data/index.#{ext}", root_path)
          if File.exist?(file_path)
            case ext
            when 'yml'
              @data = YAML.load(ERB.new(File.read(file_path)).result)
              break
            when 'rb'
              clean_room = Object.new
              @data = clean_room.instance_eval(File.read(file_path))
              break
            end
          end
        end
      end

      def init_routes
        @pages = []
        class << @pages
          def routes; self.map(&:route); end
          def file_paths; self.map(&:file_path); end
          def data; self.map(&:data); end
          def parents; self.map(&:parent); end
        end
        if @data
          @data.each do |k, v|
            set_index_route({k => v}, @data)
          end
        else
          console.error('Data is not loaded') 
        end
      end

      def set_index_route(model, parent_element, prev_route = '', parents = '')
        model_name = model.keys.first
        list = model.values.first
        parents = "#{parents}/#{model_name}"
        view_path = File.expand_path("views#{parents}/index.erb", root_path)
        hidden = model_name.start_with?('_') 

        unless hidden
          if File.exist?(view_path)
            new_route = "#{prev_route}/#{model_name}"
            page = Page.new(new_route, view_path, list, parent_element)
            @pages.push(page)
          else
            console.warn("Index view file for '#{parents}' => #{view_path} was not found")
          end
        end

        if list.kind_of?(Array)
          list.each do |element|
            set_element_route(element, parent_element, new_route || prev_route, parents)
          end
        end
      end

      def set_element_route(element, parent_element, prev_route, parents)
        key = element['key'] || element['id']
{'fraction_non_alphanumeric': 0.10998851894374283, 'fraction_numerical': 0.0, 'mean_word_length': 1.8790482485128883, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '3352827', 'n_tokens_mistral': 1293, 'n_tokens_neox': 1290, 'n_words': 296}