public
Description: A Rails plugin that allows you to extend a model with Gold, Silver and Copper money as featured in World of Warcraft
Homepage:
Clone URL: git://github.com/ariejan/acts_as_gold.git
Click here to lend your support to: acts_as_gold and make a donation at www.pledgie.com !
name age message
file .gitignore Wed Aug 13 01:36:54 -0700 2008 Enabled gem buildig, hopefully [ariejan]
file Changelog Loading commit data...
file LICENSE Wed Aug 13 01:36:54 -0700 2008 Enabled gem buildig, hopefully [ariejan]
file README.textile
file Rakefile Wed Aug 13 00:57:17 -0700 2008 Initial import [ariejan]
file acts_as_gold.gemspec Thu Aug 14 00:36:25 -0700 2008 Bumpded to 1.0.5 [ariejan]
file init.rb Thu Aug 14 00:32:14 -0700 2008 Fixed bug that AR is not monkey patched. Bumped... [ariejan]
directory lib/
directory test/ Wed Aug 13 04:18:21 -0700 2008 [1.0.2] Added tests, some minor bugfixes. [ariejan]
README.textile

acts_as_gold

Acts_as_gold extends ActiveRecord models to use a single integer column to store money in the form of Gold, Silver and Copper. This money system can be found in many games, including World of Warcraft.

Written by Ariejan de Vroom

Copyright 2008 Ariejan de Vroom

Download and installation

You can install acts_as_gold either as a Ruby on Rails plugin or a Ruby Gem. It’s recommended you use the Ruby Gem to get easier updates later on.

Plugin installation

Simply install the plugin:

./script/plugin install git://github.com/ariejan/acts_as_gold.git

Gem installation

Just install the gem:

gem install ariejan-acts_as_gold --source http://gems.github.com

Add the following to your environment.rb if you want to use Rails 2.1’s dependency manager (which is highly recommended):

config.gem "ariejan-acts_as_gold",
           :lib    => "acts_as_gold",
           :source => "http://gems.github.com"

Enabling acts_as_gold

This will use player.money to store the current amount of money.

class Player < ActiveRecord::Base
  acts_as_gold	# Uses the +money+ attribute from Player
end

You may also specify a different column for storing money, for example pennies

class Player < ActiveRecord::Base
  acts_as_gold :column => :pennies # Uses the +pennies+ attribute from Player
end

Using acts_as_gold

Acts_as_gold adds two things. If you make a model act as gold, you’ll get three bonus methods: gold, silver and copper.

You can have a maximum of 99 copper and 99 silver. 99 copper becomes 1 silver and 99 silver becomes 1 gold. The amount of gold is limited by the integer type you use.

214,748 Gold, 36 Silver, 47 Copper for a default :integer column (int(11))
922,337,203,685,477 Gold, 58 Silver, 07 Copper for a bigint (int(20)).

player.money = 3005075
player.gold => 300
player.silver => 50
player.copper => 75

A sample migration to add the money column to your model:

add_column :players, :money, :integer, :limit => 20, :default => 15000

This migration allows up to approximately 922,377 billion gold and gives the player 1 gold and 50 silver by default.

Earning and spending

It’s really easy to earn and spend money.

Earning money is no problem. You may want want to set a limit to the maximum amount of money you can have to avoid interger overflows.

player.earn(2.gold + 25.silver)

Spending is also easy. This method return true if the money was spend successfully. It will raise a NotEnoughMoneyError if there’s not enough money to be spend.

player.spend(10.silver + 3.copper)

Fixnum and Bignum extensions

As een in the previous examples, you can use several helper methods on Fixnum and Bignum to convert them to correct money values. You can use these throughout your application:

25.gold
33.silver
78.copper

More Information

Ariejan.net