<commit_before>module ActiveAdmin
  # This is a container for resources, which acts much like a Hash.
  # It's assumed that an added resource responds to `resource_name`.
  class ResourceCollection
    include Enumerable
    extend Forwardable
    def_delegators :@resources, :empty?, :has_key?, :keys, :values, :[]=

    def initialize
      @resources = {}
    end

    def add(resource)
      self[resource.resource_name] ||= resource
    end

    # Changes `each` to pass in the value, instead of both the key and value.
    def each(&block)
      values.each &block
    end

    # Finds a resource based on the resource name, the resource class, or the base class.
    def [](klass)
      if match = @resources[klass]
        match
      elsif match = real_resources.detect{ |r| r.resource_class.to_s == klass.to_s }
        match
      elsif klass.respond_to? :base_class
        real_resources.detect{ |r| r.resource_class.to_s == klass.base_class.to_s }
      end
    end

    private

    # REFACTOR: ResourceCollection currently stores Resource and Page objects. That doesn't
    # make sense, because by definition a ResourceCollection is a collection of resources.
    def real_resources
      select{ |r| r.respond_to? :resource_class }
    end

  end
end
<commit_msg>resource lookup should also search against resource_name<commit_after>module ActiveAdmin
  # This is a container for resources, which acts much like a Hash.
  # It's assumed that an added resource responds to `resource_name`.
  class ResourceCollection
    include Enumerable
    extend Forwardable
    def_delegators :@resources, :empty?, :has_key?, :keys, :values, :[]=

    def initialize
      @resources = {}
    end

    def add(resource)
      self[resource.resource_name] ||= resource
    end

    # Changes `each` to pass in the value, instead of both the key and value.
    def each(&block)
      values.each &block
    end

    # Finds a resource based on the resource name, the resource class, or the base class.
    def [](klass)
      if match = @resources[klass]
        match
      elsif match = real_resources.detect{ |r| [r.resource_name.to_s, r.resource_class.to_s].include? klass.to_s }
        match
      elsif klass.respond_to? :base_class
        real_resources.detect{ |r| r.resource_class.to_s == klass.base_class.to_s }
      end
    end

    private

    # REFACTOR: ResourceCollection currently stores Resource and Page objects. That doesn't
    # make sense, because by definition a ResourceCollection is a collection of resources.
    def real_resources
      select{ |r| r.respond_to? :resource_class }
    end

  end
end

{'fraction_non_alphanumeric': 0.07855787476280834, 'fraction_numerical': 0.0, 'mean_word_length': 2.9698795180722892, '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': '7306309', 'n_tokens_mistral': 768, 'n_tokens_neox': 755, 'n_words': 271}