dollar_column 1.0.1

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 (2) hide show
  1. data/lib/active_record/dollar_column.rb +41 -0
  2. metadata +49 -0
@@ -0,0 +1,41 @@
1
+ require 'bigdecimal'
2
+
3
+ module ActiveRecord # :nodoc:
4
+ module DollarColumn
5
+ def self.included(base) # :nodoc:
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def dollar_column(*attr_names)
11
+ if attr_names.size == 1 && attr_names[0].is_a?(Symbol)
12
+ generate_dollar_methods(attr_names[0])
13
+ elsif attr_names.flatten != attr_names
14
+ dollar_column(*attr_names.flatten)
15
+ else
16
+ attr_names.each do |attr_each|
17
+ generate_dollar_methods(attr_each.to_s)
18
+ end
19
+ end
20
+ end
21
+
22
+ def generate_dollar_methods(attr_name)
23
+ define_method(attr_name) do
24
+ (BigDecimal.new(send("#{attr_name}_in_cents").to_s) / 100).to_f
25
+ end
26
+
27
+ define_method("decimal_#{attr_name}") do
28
+ BigDecimal.new(send("#{attr_name}_in_cents").to_s) / 100
29
+ end
30
+
31
+ define_method("formatted_#{attr_name}") do
32
+ sprintf "$%.2f", send(attr_name)
33
+ end
34
+
35
+ define_method("#{attr_name}=") do |value|
36
+ write_attribute("#{attr_name}_in_cents", (BigDecimal.new(value) * 100).to_f)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dollar_column
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Daniel Ballenger
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-24 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Provides helper methods so you can store dollar amounts in cents as integers
15
+ (or floats if you need to store fractional pennies) but still do your operations
16
+ in dollars
17
+ email: dballenger@denetron.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/active_record/dollar_column.rb
23
+ homepage: https://github.com/denetron/dollar_column
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.15
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: Provides helper methods so you can store dollar amounts in cents as integers
47
+ (or floats if you need to store fractional pennies) but still do your operations
48
+ in dollars
49
+ test_files: []