Module: ActsAsGold::ClassMethods

Defined in:
lib/acts_as_gold.rb

Overview

This acts_as extension provides the capabilities for splitting a given interger column into three separte coin values: Copper, Silver and Gold.

A simple example: a Charachter has money ( t.integer :money, :limit => 20)

class Character < ActiveRecord::Base
  acts_as_gold                      # Uses the money attributes
  acts_as_gold :column => :pennies  # Uses the pennies attributes
end

character.money   = 57503
character.gold    = 5
character.silver  = 75
character.copper  = 3

character.money  += 2.gold + 10.copper
character.gold   = 7
character.copper = 13
character.money  = 77513

Copper and Silver have a maximum of 99. E.g. 100 copper => 1 silver and 100 silver => 1 Gold. The maximum amount of money depends on the integer type used in the database:

signed int(11) 2,147,483,647 => 214,748 Gold, 36 Silver, 47 Copper signed int(20) 9,223,372,036,854,775,807 => 922,337,203,685,477 Gold, ++8 Silver, 07 Copper

Instance Method Summary collapse

Instance Method Details

#acts_as_gold(options = {}) ⇒ Object

Configuration options are:

  • column - specifies the column name to use for keeping the money integer (default: money)



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/acts_as_gold.rb', line 40

def acts_as_gold(options = {})
  configuration = { :column => "money" }
  configuration.update(options) if options.is_a?(Hash)

  class_eval <<-EOV
    include ActsAsGold::InstanceMethods
    
    def money_column
      '#{configuration[:column]}'
    end

  EOV
end