Class: Royal::PointBalance

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/royal/point_balance.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.apply_change_to_points(owner, amount, **attributes) ⇒ Integer

Returns the new points balance.

Parameters:

  • owner (ActiveRecord::Base)
  • amount (Integer)
  • attributes (Hash)

    Additional attributes to set on the new record.

Returns:

  • (Integer)

    Returns the new points balance.



43
44
45
46
47
# File 'lib/royal/point_balance.rb', line 43

def self.apply_change_to_points(owner, amount, **attributes)
  with_lock_on_owner(owner) do
    create!(owner: owner, amount: amount, **attributes).balance
  end
end

.latest_for_owner(owner) ⇒ PointBalance?

Parameters:

  • owner (ActiveRecord::Base)

Returns:



30
31
32
# File 'lib/royal/point_balance.rb', line 30

def self.latest_for_owner(owner)
  PointBalance.where(owner: owner).order(:sequence).last
end

.with_lock_on_owner(owner, &block) ⇒ Object

Parameters:

  • owner (ActiveRecord::Base)


35
36
37
# File 'lib/royal/point_balance.rb', line 35

def self.with_lock_on_owner(owner, &block)
  Royal.config.locking.call(owner, &block)
end

Instance Method Details

#original_balanceInteger

Returns the balance before this operation.

Returns:

  • (Integer)


52
53
54
# File 'lib/royal/point_balance.rb', line 52

def original_balance
  balance - amount
end