<commit_before>class Summary < CouchRestRails::Document
  use_database :child

  view_by :name,
          :map => "function(doc) {
              if ((doc['couchrest-type'] == 'Child') && doc['name'])
             {
                emit(doc['name'],doc);
             }
          }"

  view_by :unique_identifier,
          :map => "function(doc) {
              if ((doc['couchrest-type'] == 'Child') && doc['unique_identifier'])
             {
                emit(doc['unique_identifier'],doc);
             }
          }"

  
  def self.basic_search(child_name, unique_id)
    x = search_by_unique_identifier(unique_id)
    y = results = search_by_name(child_name)

    results = and_arrays(x,y)

    results.sort { |lhs,rhs| lhs["name"] <=> rhs["name"]}
  end

  def self.and_arrays(*arrays)
    non_empty_arrays = arrays.reject{ |x| x.nil? || x.empty? }
    return [] if non_empty_arrays.empty?

    non_empty_arrays.inject do |anded_array,array|
      anded_array &= array
    end
  end

  private
  def self.search_by_name(search_value)
    if (search_value && !search_value.empty?)
      args = create_key_range(search_value)
      Summary.view("by_name", args)
    end
  end

  def self.search_by_unique_identifier(search_value)
    if (search_value && !search_value.empty?)
      args = {:startkey => search_value, :endkey => search_value}
      Summary.view("by_unique_identifier", args)
    end
  end

  def self.create_key_range(from_value)
    endkey = from_value[0].chr.next

    args = {:startkey => from_value}
    args.store(:endkey, endkey) unless endkey == "aa"

    args
  end
end<commit_msg>Daniel/Rafael/Pedro -refactored #101<commit_after>class Summary < CouchRestRails::Document
  END_CHAR_AVOIDER = "aa"
  use_database :child

  view_by :name,
          :map => "function(doc) {
              if ((doc['couchrest-type'] == 'Child') && doc['name'])
             {
                emit(doc['name'],doc);
             }
          }"

  view_by :unique_identifier,
          :map => "function(doc) {
              if ((doc['couchrest-type'] == 'Child') && doc['unique_identifier'])
             {
                emit(doc['unique_identifier'],doc);
             }
          }"

  
  def self.basic_search(child_name, unique_id)
    results = search_by_unique_identifier(unique_id)
    results = search_by_name(child_name) if results.nil?

    return [] unless results

    results.sort { |lhs,rhs| lhs["name"] <=> rhs["name"]} 
  end

  def self.and_arrays(*arrays)
    non_empty_arrays = arrays.reject{ |x| x.nil? || x.empty? }
    return [] if non_empty_arrays.empty?

    non_empty_arrays.inject do |anded_array,array|
      anded_array &= array
    end
  end

  private
  def self.search_by_name(search_value)
    if (search_value && !search_value.empty?)
      args = create_key_range(search_value)
      Summary.view("by_name", args)
    end
  end

  def self.search_by_unique_identifier(search_value)
    if (search_value && !search_value.empty?)
      args = {:startkey => search_value, :endkey => search_value}
      Summary.view("by_unique_identifier", args)
    end
  end

  def self.create_key_range(from_value)
    endkey = from_value[0].chr.next

    args = {:startkey => from_value}
    args.store(:endkey, endkey) unless endkey == END_CHAR_AVOIDER

    args
  end
end
{'fraction_non_alphanumeric': 0.13010590015128592, 'fraction_numerical': 0.0015128593040847202, 'mean_word_length': 2.697986577181208, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '1509767', 'n_tokens_mistral': 1130, 'n_tokens_neox': 1075, 'n_words': 197}