currency_magic 0.1.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.
@@ -0,0 +1,19 @@
1
+ module CurrencyMagic
2
+ module ClassMethods
3
+
4
+ def currency_magic(currency, *methods)
5
+ class_eval "include CurrencyMagic"
6
+ methods.each do |method|
7
+ class_eval <<-EOD
8
+ def #{method}_#{currency}
9
+ to_dollars self.#{method}
10
+ end
11
+ def #{method}_#{currency}=(#{method})
12
+ self.#{method} = to_cents(#{method})
13
+ end
14
+ EOD
15
+ end
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,10 @@
1
+ require 'active_record'
2
+ require 'active_record/base'
3
+
4
+ module CurrencyMagic
5
+ class Railtie < Rails::Railtie
6
+ initializer "currency_magic.configure_rails_initialization" do
7
+ ActiveRecord::Base.class.send(:include, CurrencyMagic::ClassMethods)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,36 @@
1
+ require 'currency_magic/class_methods'
2
+ require 'currency_magic/railtie' if defined?(Rails)
3
+
4
+ module CurrencyMagic
5
+
6
+ # Converts 'currency' to a Float
7
+ #
8
+ # Pretty much just strips out non-numeric characters and calls Object#to_f
9
+ #
10
+ # @param [Object] currency The amount of currency to be converted
11
+ # @return [Float] the amount converted to float
12
+ def currency_to_f(currency)
13
+
14
+ return nil if currency.nil?
15
+ return currency.to_f if currency.is_a?(Fixnum)
16
+
17
+ currency.to_s.gsub(/[^0-9.]/, '').to_f
18
+ end
19
+
20
+ private
21
+ # Converts dollars to cents by multiplying by 100
22
+ #
23
+ # @param [Float] dollars the amount as dollars
24
+ # @return [Integer] the amount as cents
25
+ def to_cents(dollars)
26
+ (currency_to_f(dollars) * 100).round
27
+ end
28
+
29
+ # Converts cents to dollars by dividing by 100
30
+ #
31
+ # @param [Integer] cents the amount as cents
32
+ # @return [Float] the amount as dollars
33
+ def to_dollars(an_int)
34
+ an_int.to_f / 100
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: currency_magic
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Jason Edwards
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-01-20 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: Converts currency to/from cents
22
+ email: jtanium@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/currency_magic.rb
31
+ - lib/currency_magic/class_methods.rb
32
+ - lib/currency_magic/railtie.rb
33
+ homepage: https://github.com/jtanium/currency_magic
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 3
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.8.15
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: ""
66
+ test_files: []
67
+
68
+ has_rdoc: