<commit_before>class ProductApprover

  class Stats
    attr_reader :granted, :revoked

    def initialize
      @granted = 0
      @revoked = 0
    end

    def grant
      @granted += 1
    end

    def revoke
      @revoked += 1
    end

    def any_changed?
      @granted + @revoked > 0
    end
  end

  def initialize(all_products, user, approver)
    @all_products = all_products
    @user = user
    @approver = approver
  end

  def update_approvals(products_to_approve)
    @all_products.each_with_object(Stats.new) do |product, stats|
      if products_to_approve.include?(product)
        approve_access(product) && stats.grant
      else
        revoke_access(product) && stats.revoke
      end
    end
  end

  def approve_access(product)
    create_product_user(product) unless product.is_approved_for?(@user)
  end

  def revoke_access(product)
    if product.is_approved_for?(@user)
      get_product_user(product).try(:destroy)
    end
  end

  private

  def create_product_user(product)
    ProductUser.new(product: product, user: @user, approved_by: @approver.id).save
  end

  def get_product_user(product)
    product.product_users.find_by_user_id(@user.id)
  end

end<commit_msg>Reformat and refactor ProductApprover<commit_after>class ProductApprover

  class Stats
    attr_reader :granted, :revoked

    def initialize
      @granted = 0
      @revoked = 0
    end

    def grant
      @granted += 1
    end

    def revoke
      @revoked += 1
    end

    def any_changed?
      @granted + @revoked > 0
    end
  end

  def initialize(all_products, user, approver)
    @all_products = all_products
    @user = user
    @approver = approver
  end

  def update_approvals(products_to_approve)
    @all_products.each_with_object(Stats.new) do |product, stats|
      if products_to_approve.include?(product)
        approve_access(product) && stats.grant
      else
        revoke_access(product) && stats.revoke
      end
    end
  end

  def approve_access(product)
    create_product_user(product) unless product.is_approved_for?(@user)
  end

  def revoke_access(product)
    destroy_product_user(product) if product.is_approved_for?(@user)
  end

  private

  def create_product_user(product)
    ProductUser.create(product: product, user: @user, approved_by: @approver.id)
  end

  def destroy_product_user(product)
    get_product_user(product).try(:destroy)
  end

  def get_product_user(product)
    product.product_users.find_by_user_id(@user.id)
  end

end
{'fraction_non_alphanumeric': 0.0806904857486953, 'fraction_numerical': 0.004014452027298274, 'mean_word_length': 3.132669983416252, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '644010', 'n_tokens_mistral': 857, 'n_tokens_neox': 813, 'n_words': 177}