has_money 0.0.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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Sam Vincent
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,10 @@
1
+ = has_money
2
+ In your ActiveRecord model:
3
+ class Order < ActiveRecord::Base
4
+ has_money :default_price
5
+ end
6
+
7
+ Defines methods :default_price_in_dollars and :default_price_in_dollars= which make life easier when building forms.
8
+
9
+ > Order.new :default_price_in_dollars => '10.00'
10
+ => #<Order default_price: 1000>
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "has_money"
8
+ gem.summary = "Parsing the various ways people enter dollar amounts so you don't have to."
9
+ gem.description = "Parsing the various ways people enter dollar amounts so you don't have to. Store values in cents as integer."
10
+ gem.email = "sam.vincent@mac.com"
11
+ gem.homepage = "http://github.com/samvincent/has_money"
12
+ gem.authors = ["Greg Bell", "Sam Vincent", "Howard Yeh"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "awesome #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # Inlcude in all AR Models
2
+ ActiveRecord::Base.send :include, HasMoney
data/lib/has_money.rb ADDED
@@ -0,0 +1,37 @@
1
+ require 'bigdecimal'
2
+
3
+ module HasMoney
4
+
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+
11
+ def has_money(*attributes)
12
+ attributes.each do |attribute|
13
+ class_eval <<-EOS
14
+ def #{attribute}_in_dollars
15
+ self.class.calculate_dollars_from_cents(#{attribute})
16
+ end
17
+
18
+ def #{attribute}_in_dollars=(dollars)
19
+ self.#{attribute} = self.class.calculate_cents_from_dollars(dollars)
20
+ end
21
+ EOS
22
+ end
23
+ end
24
+
25
+ def calculate_dollars_from_cents(cents)
26
+ return nil if cents.nil? || cents == ''
27
+ (BigDecimal(cents.to_s) / 100).to_f
28
+ end
29
+
30
+ def calculate_cents_from_dollars(dollars)
31
+ return nil if dollars.nil? || dollars == ''
32
+ dollars.gsub!(/[$,]/, "") if dollars.class == String
33
+ (BigDecimal(dollars.to_s).round(2) * 100).to_i
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,83 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/has_money')
2
+
3
+ describe HasMoney do
4
+
5
+ describe "including has money" do
6
+ it "should extend the class with class methods when included" do
7
+ klass = Class.new
8
+ klass.send :include, HasMoney
9
+ klass.should respond_to(:has_money)
10
+ end
11
+ end
12
+
13
+ describe "working with money" do
14
+ before(:each) do
15
+ @klass = Class.new
16
+ @klass.class_eval do
17
+ include HasMoney
18
+ attr_accessor :price
19
+ has_money :price
20
+ end
21
+ @product = @klass.new
22
+ @product.price = 2999
23
+ end
24
+
25
+ it "should create an accessor method named in_dollars for the attribute" do
26
+ @product.should respond_to(:price_in_dollars)
27
+ end
28
+
29
+ it "should create a setter method named in_dollars for the attribute" do
30
+ @product.should respond_to(:price_in_dollars=)
31
+ end
32
+
33
+ it "should return the amount in dollars" do
34
+ @product.price_in_dollars.should == 29.99
35
+ end
36
+
37
+ it "should set the amount in dollars with a float" do
38
+ @product.price_in_dollars = 39.99
39
+ @product.price.should == 3999
40
+ end
41
+
42
+ it "should set the amount in dollars with a string" do
43
+ @product.price_in_dollars = '39.99'
44
+ @product.price.should == 3999
45
+ end
46
+
47
+ it "should set the amount in dollars with a dollar sign" do
48
+ @product.price_in_dollars = "$39.99"
49
+ @product.price.should == 3999
50
+ end
51
+
52
+ it "should set the amount in dollars with commas" do
53
+ @product.price_in_dollars = "2,500.99"
54
+ @product.price.should == 250099
55
+ end
56
+
57
+ it "should round the amount up to the nearest cent" do
58
+ @product.price_in_dollars = '39.998'
59
+ @product.price.should == 4000
60
+ end
61
+
62
+ it "should round the amount down to the nearest cent" do
63
+ @product.price_in_dollars = '39.992'
64
+ @product.price.should == 3999
65
+ end
66
+
67
+ it "should set nil if nil is passed as dollars" do
68
+ @product.price_in_dollars = nil
69
+ @product.price.should == nil
70
+ end
71
+
72
+ it "should set nil if an empty string is passed in as dollars" do
73
+ @product.price_in_dollars = ''
74
+ @product.price.should == nil
75
+ end
76
+
77
+ it "should return nil if the attribute is nil" do
78
+ @product.price = nil
79
+ @product.price_in_dollars.should == nil
80
+ end
81
+ end
82
+
83
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_money
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 0
9
+ version: 0.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Greg Bell
13
+ - Sam Vincent
14
+ - Howard Yeh
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-04-30 00:00:00 -07:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: rspec
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ segments:
30
+ - 1
31
+ - 2
32
+ - 9
33
+ version: 1.2.9
34
+ type: :development
35
+ version_requirements: *id001
36
+ description: Parsing the various ways people enter dollar amounts so you don't have to. Store values in cents as integer.
37
+ email: sam.vincent@mac.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - LICENSE
44
+ - README.rdoc
45
+ files:
46
+ - .document
47
+ - .gitignore
48
+ - LICENSE
49
+ - README.rdoc
50
+ - Rakefile
51
+ - VERSION
52
+ - init.rb
53
+ - lib/has_money.rb
54
+ - spec/has_money_spec.rb
55
+ has_rdoc: true
56
+ homepage: http://github.com/samvincent/has_money
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options:
61
+ - --charset=UTF-8
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.3.6
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Parsing the various ways people enter dollar amounts so you don't have to.
85
+ test_files:
86
+ - spec/has_money_spec.rb