<commit_before>module Moonrope
  class Base
    
    #
    # Load a set of Moonrope configuration files from a given 
    # directory.
    #
    # @param path [String] the path to a directory containing Moonrope files
    # @return [Moonrope::Base]
    #
    def self.load(path)
      api = self.new
      api.load(path)
      api
    end
    
    # @return [Array] the array of defined structures
    attr_reader :structures
    
    # @return [Array] the array of defined controllers
    attr_accessor :controllers
    
    # @return [Array] the array of defined helpers
    attr_accessor :helpers
    
    # @return [Moonrope::DSL::BaseDSL] the base DSL
    attr_accessor :dsl
    
    # @return [Proc] the authentictor
    attr_accessor :authenticator
    
    # @return [Proc] the default access condition
    attr_accessor :default_access
    
    # @return [String] the directory the base was loaded from (if relevant)
    attr_accessor :loaded_from
    
    # @return [String] the moonrope environment
    attr_accessor :environment
    
    #
    # Initialize a new instance of the Moonrope::Base
    #
    # @yield instance evals the contents within the Base DSL
    #
    def initialize(&block)
      unload
      @environment = 'development'
      @dsl = Moonrope::DSL::BaseDSL.new(self)
      @dsl.instance_eval(&block) if block_given?
    end
    
    #
    # Reset the whole base to contain no data.
    #
    def unload
      @structures = []
      @controllers = []
      @helpers = @helpers.is_a?(Array) ? @helpers.select { |h| h.options[:unloadable] == false } : []
      @authenticator = nil
      @default_access = nil
      @loaded_from = []
    end
    
    #
    # Reload this whole base API from the path
    #
    def load(*directories)
      directories = self.loaded_from if directories.empty?
      if directories.size > 0
        unload
        self.loaded_from = []
        directories.each do |directory|
          load_directory(directory)
        end
        self
      else
        raise Moonrope::Errors::Error, "Can't reload Moonrope::Base as it wasn't required from a directory"
      end
    end
    
    alias_method :reload, :load
    
    #
    # Load from a given directory
    #
    def load_directory(directory)
      if File.exist?(directory)
        Dir["#{directory}/**/*.rb"].each do |filename|
          self.dsl.instance_eval(File.read(filename), filename)
        end
        self.loaded_from << directory unless self.loaded_from.include?(directory)
        true
      else
        false
      end
    end
    
    #
    # Return a structure of the given name
    #<commit_msg>ensure that errors when loading directories, don't cause the load path to be removed<commit_after>module Moonrope
  class Base
    
    #
    # Load a set of Moonrope configuration files from a given 
    # directory.
    #
    # @param path [String] the path to a directory containing Moonrope files
    # @return [Moonrope::Base]
    #
    def self.load(path)
      api = self.new
      api.load(path)
      api
    end
    
    # @return [Array] the array of defined structures
    attr_reader :structures
    
    # @return [Array] the array of defined controllers
    attr_accessor :controllers
    
    # @return [Array] the array of defined helpers
    attr_accessor :helpers
    
    # @return [Moonrope::DSL::BaseDSL] the base DSL
    attr_accessor :dsl
    
    # @return [Proc] the authentictor
    attr_accessor :authenticator
    
    # @return [Proc] the default access condition
    attr_accessor :default_access
    
    # @return [Array] the array of directories to load from  (if relevant)
    attr_accessor :load_directories
    
    # @return [String] the moonrope environment
    attr_accessor :environment
    
    #
    # Initialize a new instance of the Moonrope::Base
    #
    # @yield instance evals the contents within the Base DSL
    #
    def initialize(&block)
      unload
      @environment = 'development'
      @load_directories = []
      @dsl = Moonrope::DSL::BaseDSL.new(self)
      @dsl.instance_eval(&block) if block_given?
    end
    
    #
    # Reset the whole base to contain no data.
    #
    def unload
      @structures = []
      @controllers = []
      @helpers = @helpers.is_a?(Array) ? @helpers.select { |h| h.options[:unloadable] == false } : []
      @authenticator = nil
      @default_access = nil
    end
    
    #
    # Reload this whole base API from the path
    #
    def load(*directories)
      directories = self.load_directories if directories.empty?
      if directories.size > 0
        unload
        new_directories = []
        directories.each do |directory|
          if load_directory(directory)
            new_directories << directory
          end
        end
        self.load_directories = new_directories
        self
      else
        raise Moonrope::Errors::Error, "Can't reload Moonrope::Base as it wasn't required from a directory"
      end
    end
    
    alias_method :reload, :load
    
    #
    # Load from a given directory
    #
    def load_directory(directory)
      if File.exist?(directory)
        Dir["#{directory}/**/*.rb"].each do |filename|
          self.dsl.instance_eval(File.read(filename), filename)
        end
        true
      else
        false
      end
    end
    
    #
    # Add a dirctory to the directories to load
    #
    def add_load_directory(directory)
      if load_directory(directory)
        self.load_directories << directory unless self.load_directories.include?(directory)
        true
      else
        false
      end
    end
    
    #
    # Return a structure of the given name
    #
{'fraction_non_alphanumeric': 0.08174097664543524, 'fraction_numerical': 0.00035385704175513094, 'mean_word_length': 2.197398190045249, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '1960437', 'n_tokens_mistral': 1692, 'n_tokens_neox': 1669, 'n_words': 492}