ariejan-acts_as_gold 1.0.1 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog CHANGED
@@ -1,3 +1,6 @@
1
1
  2008-08-13
2
-
3
- [ariejan] Initial release of the acts_as_gold plugin and gem.
2
+ * [1.0.4] ariejan - Bump to 1.0.4 to ping github
3
+ * [1.0.3] ariejan - Bump to 1.0.3 to ping github
4
+ * [1.0.2] ariejan - Added tests and fixed a bug when using a custom column to store the money value.
5
+ * [1.0.1] ariejan - Minor changes to the gemspec.
6
+ * [1.0.0] ariejan - Initial release of the acts_as_gold plugin and gem..
@@ -1,32 +1,45 @@
1
1
  h1. acts_as_gold
2
2
 
3
- Written by "Ariejan de Vroom":mailto:ariejan@ariejan.net.
3
+ 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.
4
+
5
+ Written by "Ariejan de Vroom":mailto:ariejan@ariejan.net
4
6
 
5
7
  Copyright 2008 Ariejan de Vroom
6
8
 
7
- h2. Download
9
+ h2. Download and installation
10
+
11
+ 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.
12
+
13
+ h3. Plugin installation
14
+
15
+ Simply install the plugin:
16
+
17
+ <pre>./script/plugin install git://github.com/ariejan/acts_as_gold.git</pre>
8
18
 
9
- Github: "Page":http://github.com/ariejan/acts_as_gold/tree/master "Clone":git://github.com/ariejan/acts_as_gold.git
19
+ h3. Gem installation
10
20
 
11
- Gem: <pre>gem install ariejan-acts_as_gold --source http://gems.github.com</pre>
21
+ Just install the gem:
12
22
 
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:
23
+ <pre>gem install ariejan-acts_as_gold --source http://gems.github.com</pre>
15
24
 
16
- config.gem "ariejan-acts_as_gold",
25
+ Add the following to your environment.rb if you want to use Rails 2.1's dependency manager (which is highly recommended):
26
+
27
+ <pre><code>config.gem "ariejan-acts_as_gold",
17
28
  :lib => "acts_as_gold",
18
- :source => "http://gems.github.com"
29
+ :source => "http://gems.github.com"</code></pre>
19
30
 
20
31
  h2. Enabling acts_as_gold
21
32
 
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
33
+ This will use player.money to store the current amount of money.
34
+
35
+ <pre><code>class Player < ActiveRecord::Base
36
+ acts_as_gold # Uses the +money+ attribute from Player
37
+ end</code></pre>
38
+
39
+ You may also specify a different column for storing money, for example +pennies+
26
40
 
27
- # You may also specify a different columnt for storing money
28
- class Player < ActiveRecord::Base
29
- acts_as_gold :current_value
41
+ <pre><code>class Player < ActiveRecord::Base
42
+ acts_as_gold :column => :pennies # Uses the +pennies+ attribute from Player
30
43
  end</code></pre>
31
44
 
32
45
  h2. Using acts_as_gold
@@ -1 +1,133 @@
1
- require 'active_record/acts/gold'
1
+ module ActsAsGold
2
+ # Thrown when trying to spend more than you have.
3
+ class NotEnoughMoneyError < StandardError
4
+ end
5
+
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ # This +acts_as+ extension provides the capabilities for splitting a given interger column into
11
+ # three separte coin values: Copper, Silver and Gold.
12
+ #
13
+ # A simple example: a Charachter has money ( t.integer :money, :limit => 20)
14
+ #
15
+ # class Character < ActiveRecord::Base
16
+ # acts_as_gold # Uses the money attributes
17
+ # acts_as_gold :column => :pennies # Uses the pennies attributes
18
+ # end
19
+ #
20
+ # character.money = 57503
21
+ # character.gold = 5
22
+ # character.silver = 75
23
+ # character.copper = 3
24
+ #
25
+ # character.money += 2.gold + 10.copper
26
+ # character.gold = 7
27
+ # character.copper = 13
28
+ # character.money = 77513
29
+ #
30
+ # Copper and Silver have a maximum of 99. E.g. 100 copper => 1 silver and 100 silver => 1 Gold.
31
+ # The maximum amount of money depends on the integer type used in the database:
32
+ #
33
+ # signed int(11) 2,147,483,647 => 214,748 Gold, 36 Silver, 47 Copper
34
+ # signed int(20) 9,223,372,036,854,775,807 => 922,337,203,685,477 Gold, ++8 Silver, 07 Copper
35
+ #
36
+ module ClassMethods
37
+ # Configuration options are:
38
+ #
39
+ # * +column+ - specifies the column name to use for keeping the money integer (default: +money+)
40
+ def acts_as_gold(options = {})
41
+ configuration = { :column => "money" }
42
+ configuration.update(options) if options.is_a?(Hash)
43
+
44
+ class_eval <<-EOV
45
+ include ActsAsGold::InstanceMethods
46
+
47
+ def money_column
48
+ '#{configuration[:column]}'
49
+ end
50
+
51
+ EOV
52
+ end
53
+ end
54
+
55
+ # Allow Fixnum and Bignum to easily convert to Gold, Silver or Copper.
56
+ # This allows for things like:
57
+ #
58
+ # character.money = 2.gold + 45.silver + 50.copper
59
+ module IntegerExtensions
60
+ # 1.gold => 10000
61
+ def gold
62
+ self * 10000
63
+ end
64
+
65
+ # 1.silver => 100
66
+ def silver
67
+ self * 100
68
+ end
69
+
70
+ # Dummy, 1.copper => !
71
+ def copper
72
+ self
73
+ end
74
+ end
75
+
76
+ # All the methods available to records that have the acts_as_gold method enabled.
77
+ module InstanceMethods
78
+ # Earn money.
79
+ #
80
+ # Either enter a total money value, or sum it up with gold and def silver. To earn
81
+ # 1 Gold, 95 silver and 0 copper you can do the following:
82
+ #
83
+ # character.earn(19500)
84
+ # character.earn(1.gold + 95.silver)
85
+ #
86
+ # This return true if the amount was added successfully
87
+ def earn(amount)
88
+ update_attribute(:money, money + amount)
89
+ end
90
+
91
+ # Spend money
92
+ #
93
+ # You can specify money the same way as with earning money.
94
+ #
95
+ # We return true if the money was spend successfully.
96
+ #
97
+ # This will raise a 'ActiveRecord::Acts::Gold::NotEnoughMoneyError' when there's not enough money
98
+ def spend(amount)
99
+ if money >= amount
100
+ update_attribute(:money, money - amount)
101
+ else
102
+ raise ActsAsGold::NotEnoughMoneyError
103
+ end
104
+ end
105
+
106
+ # Return the amount of Gold
107
+ def gold
108
+ split_copper.first.divmod(100).first
109
+ end
110
+
111
+ def silver
112
+ split_copper.first.divmod(100).last
113
+ end
114
+
115
+ def copper
116
+ split_copper.last
117
+ end
118
+
119
+ private
120
+
121
+ def split_copper
122
+ self.send(money_column).divmod(100)
123
+ end
124
+ end
125
+ end
126
+
127
+ class Fixnum
128
+ include ActsAsGold::IntegerExtensions
129
+ end
130
+
131
+ class Bignum
132
+ include ActsAsGold::IntegerExtensions
133
+ end
@@ -0,0 +1,91 @@
1
+ require 'test/unit'
2
+
3
+ require 'rubygems'
4
+ gem 'activerecord', '>= 1.15.4.7794'
5
+ require 'active_record'
6
+
7
+ require "#{File.dirname(__FILE__)}/../init"
8
+
9
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
10
+
11
+ def setup_db
12
+ ActiveRecord::Schema.define(:version => 1) do
13
+ create_table :players do |t|
14
+ t.column :money, :integer
15
+ end
16
+
17
+ create_table :another_players do |t|
18
+ t.column :pennies, :integer
19
+ end
20
+ end
21
+ end
22
+
23
+ def teardown_db
24
+ ActiveRecord::Base.connection.tables.each do |table|
25
+ ActiveRecord::Base.connection.drop_table(table)
26
+ end
27
+ end
28
+
29
+ class Player < ActiveRecord::Base
30
+ acts_as_gold # defaults to :column => :money
31
+ end
32
+
33
+ class AnotherPlayer < ActiveRecord::Base
34
+ acts_as_gold :column => :pennies
35
+ end
36
+
37
+ class ActsAsGoldTest < Test::Unit::TestCase
38
+
39
+ def setup
40
+ # Creates a Player iwth 250G, 44S and 55C
41
+ setup_db
42
+ Player.create(:money => 2504455)
43
+ @player = Player.find(:first)
44
+ end
45
+
46
+ def teardown
47
+ teardown_db
48
+ end
49
+
50
+ def test_alternate_column_name
51
+ AnotherPlayer.create(:pennies => 1005095)
52
+ @alt_player = AnotherPlayer.find(:first)
53
+
54
+ assert_equal 1005095, @alt_player.pennies
55
+ assert_equal 100, @alt_player.gold
56
+ assert_equal 50, @alt_player.silver
57
+ assert_equal 95, @alt_player.copper
58
+ end
59
+
60
+ def test_money_retrieval
61
+ assert_equal 2504455, @player.money
62
+
63
+ assert_equal 250, @player.gold
64
+ assert_equal 44, @player.silver
65
+ assert_equal 55, @player.copper
66
+ end
67
+
68
+ def test_numeric_extensions
69
+ assert_equal 2500000, 250.gold
70
+ assert_equal 2500, 25.silver
71
+ assert_equal 25, 25.copper
72
+ end
73
+
74
+ def test_money_earning
75
+ assert_equal 2504455, @player.money
76
+ assert @player.earn(3.gold + 22.silver + 33.copper)
77
+ assert_equal 2536688, @player.money
78
+ end
79
+
80
+ def test_money_spending
81
+ assert_equal 2504455, @player.money
82
+ assert @player.spend(3.gold + 22.silver + 33.copper)
83
+ assert_equal 2472222, @player.money
84
+ end
85
+
86
+ def test_too_much_money_spending
87
+ assert_equal 2504455, @player.money
88
+ assert_raise(ActsAsGold::NotEnoughMoneyError) { @player.spend(300.gold) }
89
+ assert_equal 2504455, @player.money
90
+ end
91
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ariejan-acts_as_gold
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ariejan de Vroom
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-08-13 00:00:00 -07:00
12
+ date: 2008-08-14 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -35,9 +35,9 @@ files:
35
35
  - Rakefile
36
36
  - README.textile
37
37
  - lib/acts_as_gold.rb
38
- - lib/active_record/acts/gold.rb
38
+ - test/acts_as_gold_test.rb
39
39
  has_rdoc: true
40
- homepage:
40
+ homepage: http://ariejan.net
41
41
  post_install_message:
42
42
  rdoc_options:
43
43
  - --line-numbers
@@ -65,5 +65,5 @@ rubygems_version: 1.2.0
65
65
  signing_key:
66
66
  specification_version: 2
67
67
  summary: acts_as_gold extends a model with Gold, Silver and Copper money.
68
- test_files: []
69
-
68
+ test_files:
69
+ - test/acts_as_gold_test.rb
@@ -1,132 +0,0 @@
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