gci-class-extensions 1.1.0 → 1.1.2

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.
@@ -42,3 +42,42 @@ module Enumerable
42
42
  counts
43
43
  end
44
44
  end
45
+
46
+ class String
47
+ def is_valid_dollar_amount?
48
+ # Optional parentheses or negative sign before or after the optional dollar sign, any number of digits and commas, followed by optional . and 1 or 2 digits, and optional close parentheses, with nothing on either side
49
+ strip =~ /^[\(-]?\$?[\(-]?([\d,]+(\.\d{1,2})?|\.\d{1,2})\)?$/ ? true : false
50
+ end
51
+
52
+ def scrubbed_dollar_amount
53
+ # first strip out dollar signs and commas, then convert "(12.34)" to "-12.34"
54
+ strip.gsub(/[\$,]/,"").gsub(/^\((.*)\)$/, '-\1')
55
+ end
56
+
57
+ def scrubbed_dollar_amount_if_valid
58
+ is_valid_dollar_amount? ? scrubbed_dollar_amount : self
59
+ end
60
+
61
+ # replaces ' with '' and \\ (two backslashes) with \\\\ (two escaped backslashes)
62
+ def escape_sql
63
+ self.gsub(/\\/, '\&\&').gsub(/'/, "''")
64
+ end
65
+
66
+ def to_dollar_amount
67
+ is_valid_dollar_amount? ? scrubbed_dollar_amount.to_f.round(2) : 0.0
68
+ end
69
+ end
70
+
71
+ # TODO write a spec
72
+ class Float
73
+ def to_percentage(precision=2)
74
+ (self * 100).round(precision)
75
+ end
76
+ end
77
+
78
+ class Fixnum
79
+ # we ignore the precision and just return itself as a float, since Float#to_f accepts this parameter
80
+ def round(precision=2)
81
+ to_f
82
+ end
83
+ end
@@ -86,3 +86,102 @@ describe Enumerable, "counting itself" do
86
86
  end
87
87
  end
88
88
 
89
+ describe String, "knowing whether it contains a valid dollar amount" do
90
+ valid_amounts = [
91
+ # less than a dollar
92
+ ".50", "0.50", ".25",
93
+ # simple numbers with dollar signs
94
+ "$123", "$123.45", "$123,456", "$123,456.78",
95
+ # simple numbers without dollar signs
96
+ "123", "123.45", "123,456", "123,456.78",
97
+ # simple negatives with dollar signs
98
+ "-$123", "-$123.45", "-$123,456", "-$123,456.78",
99
+ "$-123", "$-123.45", "$-123,456", "$-123,456.78",
100
+ # simple negatives without dollar signs
101
+ "-123", "-123.45", "-123,456", "-123,456.78",
102
+ # accounting negatives with dollar signs
103
+ "($123)", "($123.45)", "($123,456)", "($123,456.78)",
104
+ "$(123)", "$(123.45)", "$(123,456)", "$(123,456.78)",
105
+ # accounting negatives without dollar signs
106
+ "(123)", "(123.45)", "(123,456)", "(123,456.78)"
107
+ ]
108
+
109
+ invalid_amounts = [
110
+ "", "$", "123.AB", "a", "123 456 789", "1-1-1",
111
+ # more than 2 digits after the period
112
+ "$123.456789"
113
+ ]
114
+
115
+ valid_amounts.each do |valid_amount|
116
+ it "should accept #{valid_amount}" do
117
+ valid_amount.is_valid_dollar_amount?.should be_true
118
+ end
119
+ end
120
+
121
+ invalid_amounts.each do |invalid_amount|
122
+ it "should not accept #{invalid_amount}" do
123
+ invalid_amount.is_valid_dollar_amount?.should be_false
124
+ end
125
+ end
126
+ end
127
+
128
+ describe String, "when scrubbing the dollar amounts to only the digits" do
129
+ it "should remove the dollar sign and commas" do
130
+ "$123,456.78".scrubbed_dollar_amount.should == "123456.78"
131
+ end
132
+
133
+ it "should convert account negatives from using () to using -" do
134
+ "(123.45)".scrubbed_dollar_amount.should == "-123.45"
135
+ end
136
+
137
+ it "should leave traditional negatives intact" do
138
+ "-$123.45".scrubbed_dollar_amount.should == "-123.45"
139
+ end
140
+
141
+ it "should leave the string alone if has no dollar sign or commas" do
142
+ "123".scrubbed_dollar_amount.should == "123"
143
+ end
144
+
145
+ it "should convert the string to a dollar with two decimals" do
146
+ '123.12'.to_dollar_amount.should == 123.12
147
+ end
148
+
149
+ it "should return 0.0 if the string has > 2 decimal places" do
150
+ '123.1234'.to_dollar_amount.should == 0.0
151
+ end
152
+
153
+ it "should return 0.0 if the string is not a valid $ amount" do
154
+ "xcv".to_dollar_amount.should == 0.0
155
+ end
156
+
157
+ end
158
+
159
+ describe Fixnum, "accepting a parameter to round as float does" do
160
+ it "should ignore the parameter and just return itself" do
161
+ 1.round(2).should == 1.0
162
+ 7.round(9).should == 7.0
163
+ 0.round(123456789).should == 0.0
164
+ end
165
+
166
+ it "should accept no parameter" do
167
+ 1.round.should == 1.0
168
+ 7.round.should == 7.0
169
+ 0.round.should == 0.0
170
+ end
171
+ end
172
+
173
+ describe Float, "knowing its value as a percentage" do
174
+ it "should convert itself with the default precision" do
175
+ # when it rounds
176
+ (0.123456).to_percentage.should == 12.35
177
+ # when it doesn't round
178
+ (6.543212).to_percentage.should == 654.32
179
+ end
180
+
181
+ it "should accept an optional precision parameter" do
182
+ # when it rounds
183
+ (0.123456).to_percentage(3).should == 12.346
184
+ # when it doesn't round
185
+ (6.543212).to_percentage(3).should == 654.321
186
+ end
187
+ end
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gci-class-extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 1
9
+ - 2
10
+ version: 1.1.2
5
11
  platform: ruby
6
12
  authors:
7
13
  - Patrick Byrne
@@ -9,29 +15,36 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2010-07-08 00:00:00 -05:00
13
- default_executable:
18
+ date: 2012-03-31 00:00:00 Z
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: active_support
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
20
25
  requirements:
21
26
  - - ">="
22
27
  - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
23
31
  version: "0"
24
- version:
32
+ type: :runtime
33
+ version_requirements: *id001
25
34
  - !ruby/object:Gem::Dependency
26
35
  name: valid-date
27
- type: :runtime
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
30
39
  requirements:
31
40
  - - ">="
32
41
  - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
33
45
  version: "0"
34
- version:
46
+ type: :runtime
47
+ version_requirements: *id002
35
48
  description:
36
49
  email: code@patrickbyrne.net
37
50
  executables: []
@@ -43,7 +56,7 @@ extra_rdoc_files:
43
56
  files:
44
57
  - lib/gci-class-extensions.rb
45
58
  - README
46
- has_rdoc: true
59
+ - spec/gci-class-extensions_spec.rb
47
60
  homepage: http://bitbucket.org/pbyrne/common-ruby-class-extensions/
48
61
  licenses: []
49
62
 
@@ -53,21 +66,27 @@ rdoc_options: []
53
66
  require_paths:
54
67
  - lib
55
68
  required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
56
70
  requirements:
57
71
  - - ">="
58
72
  - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
59
76
  version: "0"
60
- version:
61
77
  required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
62
79
  requirements:
63
80
  - - ">="
64
81
  - !ruby/object:Gem::Version
82
+ hash: 3
83
+ segments:
84
+ - 0
65
85
  version: "0"
66
- version:
67
86
  requirements: []
68
87
 
69
88
  rubyforge_project:
70
- rubygems_version: 1.3.5
89
+ rubygems_version: 1.8.6
71
90
  signing_key:
72
91
  specification_version: 3
73
92
  summary: A gem used by our internal team for common class extensions, like not_blank? and not_nil? counterparts to blank? and nil?.