spook_and_puff_money 0.5 → 0.5.5

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.
data/README.md CHANGED
@@ -5,3 +5,92 @@ This is yet another Ruby class made to represent money. This one is dead simple.
5
5
  It also provides a few other conveniences like formatting.
6
6
 
7
7
  It is primarily intended for our own projects, but is offered here as an open sourced project, because.
8
+
9
+ ## Install It
10
+
11
+ ### As a Gem
12
+
13
+ Install via RubyGems.
14
+
15
+ ```
16
+ $ gem install spook_and_puff_money
17
+ ```
18
+
19
+ Then inside your application, require it like so:
20
+
21
+ ```
22
+ require 'spook_and_puff/money'
23
+ ```
24
+
25
+ ### With Bundler
26
+
27
+ Update your Gemfile with the dependency.
28
+
29
+ ```
30
+ gem 'spook_and_puff_money', '~> 0.5.5', :require => 'spook_and_puff/money'
31
+ ```
32
+
33
+ Then update your bundle like usual.
34
+
35
+ ```
36
+ $ bundle install
37
+ ```
38
+
39
+ ## Getting Started
40
+
41
+ Once you've installed and required the gem, using it is straight forward. It can only be initialized with a BigDecimal or a String. This is to prevent mistakes like passing in a Float.
42
+
43
+ ```
44
+ money = SpookAndPuff::Money.new('10.01')
45
+ ```
46
+
47
+ If you need to print out a formatted currency string, it's simple.
48
+
49
+ ```
50
+ money.to_s #=> '$10.01'
51
+ ```
52
+
53
+ The raw BigDecimal is available via an accessor.
54
+
55
+ ```
56
+ money.raw #=> #<BigDecimal:7feb0c115c70,'0.1001E2',18(18)>
57
+ ```
58
+
59
+ Money can be added together, subtracted and multiplied. Be careful of the order and types of the operands. For the `+` and `-` operators, both operands must be money instances. For most other methods, any numeric can be used, but they will be coerced into BigDecimal in order to preserve the precision.
60
+
61
+ The argument types of each method is documented, so don't forget to have a look at them.
62
+
63
+ # SpookAndPuff::MoneyAttributes
64
+
65
+ This is a module provided as a convenience for working with ActiveRecord models. It is not required by default when using this gem, instead you must require it separately.
66
+
67
+ Assuming you have the gem installed:
68
+
69
+ ```
70
+ require 'spook_and_puff/money_attributes'
71
+ ```
72
+
73
+ Or if you are using Bundler:
74
+
75
+ ```
76
+ gem 'spook_and_puff_money', '~> 0.5.5', :require 'spook_and_puff/money_attributes'
77
+ ```
78
+
79
+ The above implicitly requires the `SpookAndPuff::Money` class as well, so you don't have to worry about that.
80
+
81
+ ## Usage
82
+
83
+ Extend your models with the module and use the ::attr_money method to define which attributes you want to have coerced into Money instances. Currently these attributes _must_ map to DB columns.
84
+
85
+ ```
86
+ class Derp < ActiveRecord::Base
87
+ extend SpookAndPuff::MoneyAttributes
88
+ attr_money :foo, :bar
89
+ end
90
+ ```
91
+
92
+ You can now use the `foo` and `bar` attributes like any other, with a few restrictions.
93
+
94
+ You can only pass the writers instances of `BigDecimal`, `String` and `NilClass`. Anything else will raise a `TypeError`.
95
+
96
+ Most importantly, you should not access the attributes using the `[]` method, since this will return the raw value, not an instance of `SpookAndPuff::Money`.
@@ -0,0 +1,44 @@
1
+ require 'spook_and_puff/money'
2
+
3
+ module SpookAndPuff
4
+ # A module for extending ActiveRecord::Base subclasses. It provides a macro
5
+ # for defining money accessors; it produces writers which coerce the input
6
+ # into SpookAndPuff::Money instances.
7
+ module MoneyAttributes
8
+ # A class macro which defines serialization and attribute writers for the
9
+ # specified attributes. This ensures that the specified attributes will
10
+ # always be instances of SpookAndPuff::Money.
11
+ #
12
+ # @param Array<Symbol>
13
+ #
14
+ # @return nil
15
+ def attr_money(*attrs)
16
+ attrs.each do |attr|
17
+ class_eval %{
18
+ def #{attr}
19
+ if self[:#{attr}]
20
+ @#{attr} ||= SpookAndPuff::Money.new(self[:#{attr}])
21
+ end
22
+ end
23
+
24
+ def #{attr}=(value)
25
+ if value.nil?
26
+ @#{attr}, self[:#{attr}] = nil
27
+ else
28
+ @#{attr} = case value
29
+ when SpookAndPuff::Money then value
30
+ else SpookAndPuff::Money.new(value)
31
+ end
32
+
33
+ self[:#{attr}] = @#{attr}.raw
34
+ end
35
+
36
+ @#{attr}
37
+ end
38
+ }
39
+ end
40
+
41
+ nil
42
+ end
43
+ end
44
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spook_and_puff_money
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.5'
4
+ version: 0.5.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-05-10 00:00:00.000000000 Z
13
+ date: 2013-05-13 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
@@ -68,6 +68,7 @@ extensions: []
68
68
  extra_rdoc_files: []
69
69
  files:
70
70
  - lib/spook_and_puff/money.rb
71
+ - lib/spook_and_puff/money_attributes.rb
71
72
  - MIT-LICENSE
72
73
  - README.md
73
74
  homepage: http://spookandpuff.com