acts_as_money 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/README +21 -0
  2. data/lib/acts_as_money.rb +32 -0
  3. metadata +96 -0
data/README ADDED
@@ -0,0 +1,21 @@
1
+ Money
2
+ =====
3
+
4
+ acts_as_money is a fairly trivial plugin that makes it easier to work with the money gem.
5
+
6
+ class Product < ActiveRecord::Base
7
+ money :price
8
+ end
9
+
10
+ This assumes that there are 2 columns in the database, cents (integer) and currency (string). These fields can be changed by setting the :cents and :currency options. To use the default currency, you can simple set :currency to false
11
+
12
+ class Room < ActiveRecord::Base
13
+ money :rate, :cents => :rate_in_cents, :currency => :rate_currency
14
+ money :discount, :cents => :discount_in_cents, :currency => false
15
+ end
16
+
17
+ acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.
18
+
19
+ r = Room.new :rate => "100.00"
20
+ r.rate # returns <Money:0x249ef9c @currency="USD", @cents=10000>
21
+
@@ -0,0 +1,32 @@
1
+ require 'money'
2
+
3
+ class ActiveRecord::Base
4
+ class << self
5
+ def acts_as_money
6
+ include(ActsAsMoney)
7
+ end
8
+ end
9
+ end
10
+
11
+
12
+ module ActsAsMoney #:nodoc:
13
+ def self.included(base) #:nodoc:
14
+ base.extend ClassMethods
15
+ end
16
+
17
+ module ClassMethods
18
+ def money(name, options = {})
19
+ composed_of name, {
20
+ :class_name => 'Money',
21
+ :mapping => [
22
+ [(options[:cents] || :cents).to_s, 'cents'],
23
+ [(options[:currency] || :currency).to_s, 'currency_as_string']
24
+ ],
25
+ :constructor => lambda {|cents, currency| Money.new(cents || 0, currency || Money.default_currency)}
26
+ }
27
+ end
28
+ end
29
+ end
30
+
31
+
32
+
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_money
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
+ platform: ruby
12
+ authors:
13
+ - Tim Cowlishaw
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-21 00:00:00 +00:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: money
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: activerecord
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ description:
50
+ email: tim@timcowlishaw.co.uk
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files:
56
+ - README
57
+ files:
58
+ - README
59
+ - lib/acts_as_money.rb
60
+ has_rdoc: false
61
+ homepage: http://github.com/timcowlishaw/acts_as_money
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options:
66
+ - --main
67
+ - README
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ requirements: []
89
+
90
+ rubyforge_project:
91
+ rubygems_version: 1.3.7
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: A fairly trivial plugin allowing easy serialisation of Money values (from the money gem) as attributes on activerecord objects
95
+ test_files: []
96
+