<commit_before>module ESA
  module Extendable
    extend ActiveSupport::Concern

    included do |base|
      cattr_accessor :extensions
      self.extensions = {}

      def simple_type
        self.type.split('::').last
      end

      def self.register_extension(expression, extension)
        self.extensions[expression] = extension
      end

      def self.lookup_extension(name)
        if self.extensions.present?
          matches = self.extensions.map do |expr,extension|
            if expr.is_a? Regexp and expr.match(name).present?
              extension
            elsif expr.is_a? String and expr == name
              extension
            else
              nil
            end
          end.compact

          if matches.present?
            matches.first
          else
            nil
          end
        else
          self.name
        end
      end

      def self.extension_name(accountable)
        if accountable.is_a? Class
          if accountable.respond_to? :accountable_name
            lookup_extension(accountable.accountable_name)
          else
            lookup_extension(accountable.name)
          end
        elsif accountable.is_a? Object and not accountable.is_a? String
          extension_name(accountable.class)
        else
          lookup_extension(accountable)
        end
      end

      def self.extension_class(accountable)
        if extension_name(accountable).present?
          extension_name(accountable).constantize
        else
          nil
        end
      end

      def self.extension_instance(accountable)
        if extension_class(accountable).present?
          extension_class(accountable).first_or_create
        else
          nil
        end
      end

      def self.accountable_name(extension=self)
        registered_keys_for(extension).find{|k| k.is_a? String}
      end

      def self.registered_keys_for(extension=self)
        if extension.is_a? Class
          self.extensions.select{|k,v| v == extension.name}.keys
        elsif extension.is_a? Object and not extension.is_a? String
          registered_keys_for(extension.class)
        else
          self.extensions.select{|k,v| v == extension}.keys
        end
      end<commit_msg>Provide a method that can be neatly overridden to look up specific instances<commit_after>module ESA
  module Extendable
    extend ActiveSupport::Concern

    included do |base|
      cattr_accessor :extensions
      self.extensions = {}

      def simple_type
        self.type.split('::').last
      end

      def self.register_extension(expression, extension)
        self.extensions[expression] = extension
      end

      def self.lookup_extension(name)
        if self.extensions.present?
          matches = self.extensions.map do |expr,extension|
            if expr.is_a? Regexp and expr.match(name).present?
              extension
            elsif expr.is_a? String and expr == name
              extension
            else
              nil
            end
          end.compact

          if matches.present?
            matches.first
          else
            nil
          end
        else
          self.name
        end
      end

      def self.extension_name(accountable)
        if accountable.is_a? Class
          if accountable.respond_to? :accountable_name
            lookup_extension(accountable.accountable_name)
          else
            lookup_extension(accountable.name)
          end
        elsif accountable.is_a? Object and not accountable.is_a? String
          extension_name(accountable.class)
        else
          lookup_extension(accountable)
        end
      end

      def self.extension_class(accountable)
        if extension_name(accountable).present?
          extension_name(accountable).constantize
        else
          nil
        end
      end

      def self.extension_instance(accountable)
        if extension_class(accountable).present?
          extension_class(accountable).instance_for(accountable)
        else
          nil
        end
      end

      def self.instance_for(accountable)
        self.first_or_create
      end

      def self.accountable_name(extension=self)
        registered_keys_for(extension).find{|k| k.is_a? String}
      end

      def self.registered_keys_for(extension=self)
        if extension.is_a? Class
          self.extensions.select{|k,v| v == extension.name}.keys
        elsif extension.is_a? Object and not extension.is_a? String
          registered_keys_for(extension.class)
        else
          self.extensions.select{|k,v| v == extension}.keys
        end
      end
{'fraction_non_alphanumeric': 0.0651890482398957, 'fraction_numerical': 0.0, 'mean_word_length': 1.851920693928129, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4525564', 'n_tokens_mistral': 1232, 'n_tokens_neox': 1188, 'n_words': 269}