Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
ariejan committed Aug 13, 2008
0 parents commit c4bd614
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 0 deletions.
20 changes: 20 additions & 0 deletions MIT-LICENSE
@@ -0,0 +1,20 @@
Copyright (c) 2008 [name of plugin creator]

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17 changes: 17 additions & 0 deletions README
@@ -0,0 +1,17 @@
ActsAsGold
==========

Allows a model to have copper, silver and gold, based on a single integer column on the model itself.

For example, this Character model has the following migration:

add_column :money, :integer, :limit => 20


Example
=======

Example goes here.


Copyright (c) 2008 [name of plugin creator], released under the MIT license
22 changes: 22 additions & 0 deletions Rakefile
@@ -0,0 +1,22 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the acts_as_gold plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Generate documentation for the acts_as_gold plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'ActsAsGold'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end
3 changes: 3 additions & 0 deletions init.rb
@@ -0,0 +1,3 @@
$:.unshift "#{File.dirname(__FILE__)}/lib"
require 'active_record/acts/gold'
ActiveRecord::Base.class_eval { include ActiveRecord::Acts::Gold }
132 changes: 132 additions & 0 deletions lib/active_record/acts/gold.rb
@@ -0,0 +1,132 @@
module ActiveRecord
module Acts #:nodoc:
module Gold #:nodoc:
def self.included(base)
base.extend(ClassMethods)
end

# 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 :money
# 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, 58 Silver, 07 Copper
#
module ClassMethods
# Configuration options are:
#
# * +column+ - specifies the column name to use for keeping the money integer (default: +money+)
def acts_as_gold(options = {})
configuration = { :column => "money" }
configuration.update(options) if options.is_a?(Hash)

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

# Allow Fixnum and Bignum to easily convert to Gold, Silver or Copper.
# This allows for things like:
#
# character.money = 2.gold + 45.silver + 50.copper
module IntegerExtensions
# 1.gold => 10000
def gold
self * 10000
end

# 1.silver => 100
def silver
self * 100
end

# Dummy, 1.copper => !
def copper
self
end
end

# All the methods available to records that have the acts_as_gold method enabled.
module InstanceMethods
# 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
def earn(amount)
update_attribute(:money, money + amount)
end

# 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
def spend(amount)
if money >= amount
update_attribute(:money, money - amount)
else
raise HotChocolate::NotEnoughMoneyError
end
end

# Return the amount of Gold
def gold
split_copper.first.divmod(100).first
end

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

def copper
split_copper.last
end

private

def split_copper
self.send(money_column).divmod(100)
end
end
end
end
end

class Fixnum
include ActiveRecord::Acts::Gold::IntegerExtensions
end

class Bignum
include ActiveRecord::Acts::Gold::IntegerExtensions
end
4 changes: 4 additions & 0 deletions tasks/acts_as_gold_tasks.rake
@@ -0,0 +1,4 @@
# desc "Explaining what the task does"
# task :acts_as_gold do
# # Task goes here
# end
8 changes: 8 additions & 0 deletions test/acts_as_gold_test.rb
@@ -0,0 +1,8 @@
require 'test/unit'

class ActsAsGoldTest < Test::Unit::TestCase
# Replace this with your real tests.
def test_this_plugin
flunk
end
end
1 change: 1 addition & 0 deletions uninstall.rb
@@ -0,0 +1 @@
# Uninstall hook code here

0 comments on commit c4bd614

Please sign in to comment.