<commit_before>require 'set'

class Arrangement < ActiveRecord::Base
  set_primary_key :uuid

  has_many :participants, :primary_key => 'uuid',
    :foreign_key => 'arrangement_uuid', :dependent => :delete_all

  validates :name, :presence => true

  before_create :generate_uuid

  def generate_uuid
    self.uuid = UUID.new.generate
  end

  def total_expenses
    participants.map(&:total_expenses).inject(0,&:+)
  end 

  def average_expenses
    count = participants.count 
    if count > 0
      total_expenses / participants.count.to_d
    else
      0
    end 
  end 

  def debitors
    participants_in_debt = participants.find_all &:in_debt?
    participants_in_debt.map { |p| Debitor.new(p) }
  end 
    
  def creditors
    participants_with_claim = participants.find_all &:has_claim?
    participants_with_claim.map { |p| Creditor.new(p) }
  end 

  def settle_debt
    transfers = Transfers.new
    debitors_with_debt = debitors
    creditors.each do |creditor|
      while creditor.has_claim?
        debitor = debitors_with_debt.pop
        transfer = creditor.collect_debt debitor
        transfers << transfer
      end 

      if debitor.in_debt?
        debitors_with_debt << debitor
      end 
    end
    transfers
  end
end<commit_msg>refactoring<commit_after>require 'set'

class Arrangement < ActiveRecord::Base
  set_primary_key :uuid

  has_many :participants, :primary_key => 'uuid',
    :foreign_key => 'arrangement_uuid', :dependent => :delete_all

  validates :name, :presence => true

  before_create :generate_uuid

  def generate_uuid
    self.uuid = UUID.new.generate
  end

  def total_expenses
    participants.map(&:total_expenses).inject(0,&:+)
  end 

  def average_expenses
    count = participants.count 
    return 0 if count <= 0
    total_expenses / count.to_d
  end 

  def debitors
    participants_in_debt = participants.find_all &:in_debt?
    participants_in_debt.map { |p| Debitor.new(p) }
  end 
    
  def creditors
    participants_with_claim = participants.find_all &:has_claim?
    participants_with_claim.map { |p| Creditor.new(p) }
  end 

  def settle_debt
    transfers = Transfers.new
    debitors_with_debt = debitors
    creditors.each do |creditor|
      while creditor.has_claim?
        debitor = debitors_with_debt.pop
        transfer = creditor.collect_debt debitor
        transfers << transfer
      end 

      if debitor.in_debt?
        debitors_with_debt << debitor
      end 
    end
    transfers
  end
end
{'fraction_non_alphanumeric': 0.0797422472815143, 'fraction_numerical': 0.0024164317358034634, 'mean_word_length': 3.0324675324675323, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 12, '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': '3068257', 'n_tokens_mistral': 868, 'n_tokens_neox': 803, 'n_words': 176}