<commit_before># frozen_string_literal: true

class User < ApplicationRecord
  validates :name, presence: true

  scope :order_by_name_asc, -> {
    order(arel_table['name'].lower.asc)
  }

  after_save do |user|
    Audit.create! difference: user.balance - user.balance_before_last_save, drink: @purchased_drink.nil? ? 0 : @purchased_drink.id, user: user.audit? ? user.id : nil
  end

  def deposit(amount)
    self.balance += amount
    save!
  end

  def buy(drink)
    self.balance -= drink.price
    @purchased_drink = drink
    save!
  end

  def payment(amount)
    self.balance -= amount
    save!
  end

  def initial
    first = name[0, 1].downcase<commit_msg>:bug: Use pessimistic locking for users (closes #97)<commit_after># frozen_string_literal: true

class User < ApplicationRecord
  validates :name, presence: true

  scope :order_by_name_asc, -> {
    order(arel_table['name'].lower.asc)
  }

  after_save do |user|
    Audit.create! difference: user.balance - user.balance_before_last_save, drink: @purchased_drink.nil? ? 0 : @purchased_drink.id, user: user.audit? ? user.id : nil
  end

  def deposit(amount)
    with_lock do
      self.balance += amount
      save!
    end
  end

  def buy(drink)
    with_lock do
      self.balance -= drink.price
      @purchased_drink = drink
      save!
    end
  end

  def payment(amount)
    with_lock do
      self.balance -= amount
      save!
    end
  end

  def initial
    first = name[0, 1].downcase
{'fraction_non_alphanumeric': 0.10286103542234333, 'fraction_numerical': 0.005449591280653951, 'mean_word_length': 3.002724795640327, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6111373', 'n_tokens_mistral': 521, 'n_tokens_neox': 499, 'n_words': 123}