ariejan-acts_as_gold 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog ADDED
@@ -0,0 +1,3 @@
1
+ 2008-08-13
2
+
3
+ [ariejan] Initial release of the acts_as_gold plugin and gem.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Ariejan de Vroom
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,74 @@
1
+ h1. acts_as_gold
2
+
3
+ Written by "Ariejan de Vroom":mailto:ariejan@ariejan.net.
4
+
5
+ Copyright 2008 Ariejan de Vroom
6
+
7
+ h2. Download
8
+
9
+ Github: "Page":http://github.com/ariejan/acts_as_gold/tree/master "Clone":git://github.com/ariejan/acts_as_gold.git
10
+
11
+ Gem: <pre>gem install ariejan-acts_as_gold --source http://gems.github.com</pre>
12
+
13
+ Note: if you install acts_as_gold using the gem from Github, you'll need this
14
+ in your environment.rb if you want to use Rails 2.1's dependency manager:
15
+
16
+ config.gem "ariejan-acts_as_gold",
17
+ :lib => "acts_as_gold",
18
+ :source => "http://gems.github.com"
19
+
20
+ h2. Enabling acts_as_gold
21
+
22
+ <pre><code># This will use player.money to store the current amount of money.
23
+ class Player < ActiveRecord::Base
24
+ acts_as_gold
25
+ end
26
+
27
+ # You may also specify a different columnt for storing money
28
+ class Player < ActiveRecord::Base
29
+ acts_as_gold :current_value
30
+ end</code></pre>
31
+
32
+ h2. Using acts_as_gold
33
+
34
+ Acts_as_gold adds two things. If you make a model act as gold, you'll get three bonus methods: +gold+, +silver+ and +copper+.
35
+
36
+ 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.
37
+
38
+ 214,748 Gold, 36 Silver, 47 Copper for a default :integer column (int(11))
39
+ 922,337,203,685,477 Gold, 58 Silver, 07 Copper for a bigint (int(20)).
40
+
41
+ <pre><code>player.money = 3005075
42
+ player.gold => 300
43
+ player.silver => 50
44
+ player.copper => 75</code></pre>
45
+
46
+ A sample migration to add the money column to your model:
47
+
48
+ <pre><code>add_column :players, :money, :integer, :limit => 20, :default => 15000</code></pre>
49
+
50
+ This migration allows up to approximately 922,377 billion gold and gives the player 1 gold and 50 silver by default.
51
+
52
+ h2. Earning and spending
53
+
54
+ It's really easy to earn and spend money.
55
+
56
+ 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.
57
+
58
+ <pre><code>player.earn(2.gold + 25.silver)</code></pre>
59
+
60
+ 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.
61
+
62
+ <pre><code>player.spend(10.silver + 3.copper)</code></pre>
63
+
64
+ h2. Fixnum and Bignum extensions
65
+
66
+ 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:
67
+
68
+ <pre><code>25.gold
69
+ 33.silver
70
+ 78.copper</code></pre>
71
+
72
+ h2. More Information
73
+
74
+ "Ariejan.net":http://ariejan.net
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the acts_as_gold plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the acts_as_gold plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'ActsAsGold'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
@@ -0,0 +1,132 @@
1
+ module ActiveRecord
2
+ module Acts #:nodoc:
3
+ module Gold #:nodoc:
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ # This +acts_as+ extension provides the capabilities for splitting a given interger column into
9
+ # three separte coin values: Copper, Silver and Gold.
10
+ #
11
+ # A simple example: a Charachter has money ( t.integer :money, :limit => 20)
12
+ #
13
+ # class Character < ActiveRecord::Base
14
+ # acts_as_gold :money
15
+ # end
16
+ #
17
+ # character.money = 57503
18
+ # character.gold = 5
19
+ # character.silver = 75
20
+ # character.copper = 3
21
+ #
22
+ # character.money += 2.gold + 10.copper
23
+ # character.gold = 7
24
+ # character.copper = 13
25
+ # character.money = 77513
26
+ #
27
+ # Copper and Silver have a maximum of 99. E.g. 100 copper => 1 silver and 100 silver => 1 Gold.
28
+ # The maximum amount of money depends on the integer type used in the database:
29
+ #
30
+ # signed int(11) 2,147,483,647 => 214,748 Gold, 36 Silver, 47 Copper
31
+ # signed int(20) 9,223,372,036,854,775,807 => 922,337,203,685,477 Gold, 58 Silver, 07 Copper
32
+ #
33
+ module ClassMethods
34
+ # Configuration options are:
35
+ #
36
+ # * +column+ - specifies the column name to use for keeping the money integer (default: +money+)
37
+ def acts_as_gold(options = {})
38
+ configuration = { :column => "money" }
39
+ configuration.update(options) if options.is_a?(Hash)
40
+
41
+ class_eval <<-EOV
42
+ include ActiveRecord::Acts::Gold::InstanceMethods
43
+
44
+ def money_column
45
+ '#{configuration[:column]}'
46
+ end
47
+
48
+ EOV
49
+ end
50
+ end
51
+
52
+ # Allow Fixnum and Bignum to easily convert to Gold, Silver or Copper.
53
+ # This allows for things like:
54
+ #
55
+ # character.money = 2.gold + 45.silver + 50.copper
56
+ module IntegerExtensions
57
+ # 1.gold => 10000
58
+ def gold
59
+ self * 10000
60
+ end
61
+
62
+ # 1.silver => 100
63
+ def silver
64
+ self * 100
65
+ end
66
+
67
+ # Dummy, 1.copper => !
68
+ def copper
69
+ self
70
+ end
71
+ end
72
+
73
+ # All the methods available to records that have the acts_as_gold method enabled.
74
+ module InstanceMethods
75
+ # Earn money.
76
+ #
77
+ # Either enter a total money value, or sum it up with gold and def silver. To earn
78
+ # 1 Gold, 95 silver and 0 copper you can do the following:
79
+ #
80
+ # character.earn(19500)
81
+ # character.earn(1.gold + 95.silver)
82
+ #
83
+ # This return true if the amount was added successfully
84
+ def earn(amount)
85
+ update_attribute(:money, money + amount)
86
+ end
87
+
88
+ # Spend money
89
+ #
90
+ # You can specify money the same way as with earning money.
91
+ #
92
+ # We return true if the money was spend successfully.
93
+ #
94
+ # This will raise a 'ActiveRecord::Acts::Gold::NotEnoughMoneyError' when there's not enough money
95
+ def spend(amount)
96
+ if money >= amount
97
+ update_attribute(:money, money - amount)
98
+ else
99
+ raise HotChocolate::NotEnoughMoneyError
100
+ end
101
+ end
102
+
103
+ # Return the amount of Gold
104
+ def gold
105
+ split_copper.first.divmod(100).first
106
+ end
107
+
108
+ def silver
109
+ split_copper.first.divmod(100).last
110
+ end
111
+
112
+ def copper
113
+ split_copper.last
114
+ end
115
+
116
+ private
117
+
118
+ def split_copper
119
+ self.send(money_column).divmod(100)
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
125
+
126
+ class Fixnum
127
+ include ActiveRecord::Acts::Gold::IntegerExtensions
128
+ end
129
+
130
+ class Bignum
131
+ include ActiveRecord::Acts::Gold::IntegerExtensions
132
+ end
@@ -0,0 +1 @@
1
+ require 'active_record/acts/gold'
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ariejan-acts_as_gold
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ariejan de Vroom
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-08-13 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activerecord
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "1.0"
23
+ version:
24
+ description: acts_as_gold allows you to extend a model with money in the form of Gold, Silver and Copper, as seen in World of Warcraft
25
+ email: ariejan@ariejan.net
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README.textile
32
+ files:
33
+ - Changelog
34
+ - LICENSE
35
+ - Rakefile
36
+ - README.textile
37
+ - lib/acts_as_gold.rb
38
+ - lib/active_record/acts/gold.rb
39
+ has_rdoc: true
40
+ homepage:
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --line-numbers
44
+ - --inline-source
45
+ - --main
46
+ - README.textile
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.2.0
65
+ signing_key:
66
+ specification_version: 2
67
+ summary: acts_as_gold extends a model with Gold, Silver and Copper money.
68
+ test_files: []
69
+