<commit_before>require 'active_record'
require 'oai'
require 'time'
require 'xml/libxml'

module Miletus
  module NamespaceHelper

    class Namespace < Struct.new(:uri, :prefix, :schema_location)

      def schema
        @schema_obj ||= build_schema_object
      end

      private

      def build_schema_object
        require 'open-uri'
        schema_doc = Nokogiri::XML(open(schema_location), url = schema_location)
        Nokogiri::XML::Schema.from_document(schema_doc)
      end

    end

    @@namespaces = [
      Namespace.new('http://www.openarchives.org/OAI/2.0/',
        'oai', 'http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd'),
      Namespace.new('http://www.openarchives.org/OAI/2.0/oai-identifier',
        'oaii', 'http://www.openarchives.org/OAI/2.0/oai-identifier.xsd'),
      Namespace.new('http://purl.org/dc/elements/1.1/',
        'dc', 'http://dublincore.org/schemas/xmls/simpledc20021212.xsd'),
      Namespace.new('http://www.openarchives.org/OAI/2.0/oai_dc/',
        'oai_dc', 'http://www.openarchives.org/OAI/2.0/oai_dc.xsd'),
      Namespace.new('http://ands.org.au/standards/rif-cs/registryObjects',
        'rif','http://services.ands.org.au' +
          '/documentation/rifcs/schema/registryObjects.xsd')
    ]

    # Lookup on
    Namespace.members.each do |key|
      method_sym = ("ns_by_%s" % key).to_sym
      define_method(method_sym) do |value|
        idx = @@namespaces.index { |obj| obj.send(key) == value }
        idx.nil? ? nil : @@namespaces[idx]
      end
      module_function method_sym
    end

    def ns_decl
      # Convenience definition for XPath matching
      @@namespaces.each_with_object({}) do |ns, obj|
        obj[ns.prefix] = ns.uri
      end
    end<commit_msg>Refactoring NamespaceHelper a bit.<commit_after>require 'active_record'
require 'oai'
require 'time'
require 'xml/libxml'

module Miletus
  module NamespaceHelper

    class Namespace < Struct.new(:uri, :prefix, :schema_location)

      def self.instances
        @@instances ||= [
          Namespace.new('http://www.openarchives.org/OAI/2.0/',
            'oai', 'http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd'),
          Namespace.new('http://www.openarchives.org/OAI/2.0/oai-identifier',
            'oaii', 'http://www.openarchives.org/OAI/2.0/oai-identifier.xsd'),
          Namespace.new('http://purl.org/dc/elements/1.1/',
            'dc', 'http://dublincore.org/schemas/xmls/simpledc20021212.xsd'),
          Namespace.new('http://www.openarchives.org/OAI/2.0/oai_dc/',
            'oai_dc', 'http://www.openarchives.org/OAI/2.0/oai_dc.xsd'),
          Namespace.new('http://ands.org.au/standards/rif-cs/registryObjects',
            'rif','http://services.ands.org.au' +
              '/documentation/rifcs/schema/registryObjects.xsd')
        ]
      end

      def schema
        @schema_obj ||= build_schema_object
      end

      private

      def build_schema_object
        require 'open-uri'
        schema_doc = Nokogiri::XML(open(schema_location), url = schema_location)
        Nokogiri::XML::Schema.from_document(schema_doc)
      end

    end

    # Lookup on
    Namespace.members.each do |key|
      method_sym = ("ns_by_%s" % key).to_sym
      define_method(method_sym) do |value|
        Namespace.instances.find { |obj| obj.send(key) == value }
      end
      module_function method_sym
    end

    def ns_decl
      # Convenience definition for XPath matching
      Namespace.instances.each_with_object({}) do |ns, obj|
        obj[ns.prefix] = ns.uri
      end
    end
{'fraction_non_alphanumeric': 0.14673452078032231, 'fraction_numerical': 0.012439920836867402, 'mean_word_length': 2.5629405840886204, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 12, 'xml': 6}, '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': '6028650', 'n_tokens_mistral': 1273, 'n_tokens_neox': 1211, 'n_words': 177}