Class: Royal::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/royal/transaction.rb

Defined Under Namespace

Classes: Operation

Instance Method Summary collapse

Constructor Details

#initializeTransaction

Returns a new instance of Transaction.



19
20
21
# File 'lib/royal/transaction.rb', line 19

def initialize
  @operations = []
end

Instance Method Details

#add_points(owner, amount, reason: nil, pointable: nil) ⇒ self

Parameters:

  • owner (ActiveRecord::Base)
  • amount (Integer)
  • reason (String, nil) (defaults to: nil)
  • pointable (ActiveRecord::Base, nil) (defaults to: nil)

Returns:

  • (self)


28
29
30
31
32
# File 'lib/royal/transaction.rb', line 28

def add_points(owner, amount, reason: nil, pointable: nil)
  @operations << Operation.new(owner, amount, reason, pointable).freeze

  self
end

#callself

Returns:

  • (self)


44
45
46
47
48
49
50
# File 'lib/royal/transaction.rb', line 44

def call
  PointBalance.transaction(requires_new: true) do
    @operations.sort_by(&:sorting_key).each(&:perform)
  end

  self
end

#subtract_points(owner, amount, reason: nil, pointable: nil) ⇒ self

Parameters:

  • owner (ActiveRecord::Base)
  • amount (Integer)
  • reason (String, nil) (defaults to: nil)
  • pointable (ActiveRecord::Base, nil) (defaults to: nil)

Returns:

  • (self)


39
40
41
# File 'lib/royal/transaction.rb', line 39

def subtract_points(owner, amount, reason: nil, pointable: nil)
  add_points(owner, -amount, reason: reason, pointable: pointable)
end