<commit_before># A collection of MIDI devices
# Provides methods to locate specific devices

module JSound::Midi::Devices
  class DeviceCollection
    include JSound::Util      

    def initialize
      @coll = []
    end  

    # List all attribute values for the given attribute
    def list_all(attribute=:description)
      @coll.collect{|device| device[attribute]}
    end

    # List attribute values for the given attribute, omitting any unknown values
    def list(attribute=:description)
      list_all(attribute).delete_if{|value| value =~ unknown? }
    end

    def find(criteria)
      search :find, criteria
    end

    def find_all(criteria)
      search :find_all, criteria
    end

    def [](criteria)
      if criteria.kind_of? Fixnum or criteria.kind_of? Range
        @coll[criteria]
      else
        find_all(criteria)
      end
    end

    def /(regexp)
      regexp = Regexp.new(regexp.to_s) if not regexp.kind_of? Regexp
      find(regexp)
    end

    def to_s
      @coll.join("\n")
    end

    def to_json(indent='')
      "[\n" + @coll.map{|device| device.to_json(indent+'  ') }.join(",\n") + "\n]"
    end

    private

    def search(iterator, criteria)
      field, target_value = field_and_target_value_for(criteria)
      matcher = matcher_for(target_value)
      @coll.send(iterator) do |device|        
        device[field].send(matcher, target_value)
      end
    end

    def field_and_target_value_for(criteria)
      if criteria.respond_to? :[] and criteria.respond_to? :keys
        first_key = criteria.keys.first
        return first_key, criteria[first_key]
      else
        return :description, criteria
      end
    end

    def matcher_for(target_value)
      case target_value
      when Regexp then '=~'
      else '=='
      end
    end

    def method_missing(sym, *args, &block)
      @coll.send(sym, *args, &block)
    end
          
  end 
end<commit_msg>DEVICES.DeviceName now finds a device matching the description 'DeviceName'<commit_after># A collection of MIDI devices
# Provides methods to locate specific devices

module JSound::Midi::Devices
  class DeviceCollection
    include JSound::Util      

    def initialize
      @coll = []
    end  

    # List all attribute values for the given attribute
    def list_all(attribute=:description)
      @coll.collect{|device| device[attribute]}
    end

    # List attribute values for the given attribute, omitting any unknown values
    def list(attribute=:description)
      list_all(attribute).delete_if{|value| value =~ unknown? }
    end

    def find(criteria)
      search :find, criteria
    end

    def find_all(criteria)
      search :find_all, criteria
    end

    def [](criteria)
      if criteria.kind_of? Fixnum or criteria.kind_of? Range
        @coll[criteria]
      else
        find_all(criteria)
      end
    end

    def /(regexp)
      regexp = Regexp.new(regexp.to_s) if not regexp.kind_of? Regexp
      find(regexp)
    end

    def to_s
      @coll.join("\n")
    end

    def to_json(indent='')
      "[\n" + @coll.map{|device| device.to_json(indent+'  ') }.join(",\n") + "\n]"
    end

    private

    def search(iterator, criteria)
      field, target_value = field_and_target_value_for(criteria)
      matcher = matcher_for(target_value)
      @coll.send(iterator) do |device|        
        device[field].send(matcher, target_value)
      end
    end

    def field_and_target_value_for(criteria)
      if criteria.respond_to? :[] and criteria.respond_to? :keys
        first_key = criteria.keys.first
        return first_key, criteria[first_key]
      else
        return :description, criteria
      end
    end

    def matcher_for(target_value)
      case target_value
      when Regexp then '=~'
      else '=='
      end
    end

    def method_missing(sym, *args, &block)
      if @coll.respond_to? sym
        @coll.send(sym, *args, &block)
      elsif args.length == 0 and block.nil?
        self / sym
      else
        super
      end
    end
          
  end 
end
{'fraction_non_alphanumeric': 0.09323084552442351, 'fraction_numerical': 0.0002479543763947434, 'mean_word_length': 2.433191489361702, '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': '1074753', 'n_tokens_mistral': 1288, 'n_tokens_neox': 1250, 'n_words': 326}