Module: ActiveRecord::Acts::Gold::InstanceMethods

Defined in:
lib/active_record/acts/gold.rb

Overview

All the methods available to records that have the acts_as_gold method enabled.

Instance Method Summary collapse

Instance Method Details

#copperObject



112
113
114
# File 'lib/active_record/acts/gold.rb', line 112

def copper
  split_copper.last
end

#earn(amount) ⇒ Object

Earn money.

Either enter a total money value, or sum it up with gold and def silver. To earn 1 Gold, 95 silver and 0 copper you can do the following:

character.earn(19500) character.earn(1.gold + 95.silver)

This return true if the amount was added successfully



84
85
86
# File 'lib/active_record/acts/gold.rb', line 84

def earn(amount)
  update_attribute(:money, money + amount)
end

#goldObject

Return the amount of Gold



104
105
106
# File 'lib/active_record/acts/gold.rb', line 104

def gold
  split_copper.first.divmod(100).first
end

#silverObject



108
109
110
# File 'lib/active_record/acts/gold.rb', line 108

def silver
  split_copper.first.divmod(100).last
end

#spend(amount) ⇒ Object

Spend money

You can specify money the same way as with earning money.

We return true if the money was spend successfully.

This will raise a ‘ActiveRecord::Acts::Gold::NotEnoughMoneyError’ when there’s not enough money



95
96
97
98
99
100
101
# File 'lib/active_record/acts/gold.rb', line 95

def spend(amount)
  if money >= amount
    update_attribute(:money, money - amount)
  else
    raise HotChocolate::NotEnoughMoneyError
  end
end