ar_strip_commas 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +11 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +30 -0
- data/Rakefile +23 -0
- data/VERSION +1 -0
- data/lib/ar_strip_commas.rb +47 -0
- data/spec/ar_strip_commas_spec.rb +62 -0
- data/spec/spec_helper.rb +14 -0
- metadata +109 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "bundler", "~> 1.0.0"
|
10
|
+
gem "jeweler", "~> 1.5.2"
|
11
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Jason Tillery
|
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,30 @@
|
|
1
|
+
= ar_strip_commas
|
2
|
+
|
3
|
+
This gem adds two class methods to ActiveRecord::Base for automatically handling the conversion of strings with commas ("1,203") to the intended value on numeric columns.
|
4
|
+
|
5
|
+
**self.strip_commas_from(*columns)**
|
6
|
+
|
7
|
+
Remove commas on the specified column. e.g.
|
8
|
+
|
9
|
+
class Widget < ActiveRecord::Base
|
10
|
+
strip_commas_from :price
|
11
|
+
end
|
12
|
+
Widget.new(:price => "1,200").price == 1200 # true
|
13
|
+
|
14
|
+
**self.strip_commas_from_all_numbers**
|
15
|
+
|
16
|
+
Remove commas from all numeric columns. e.g.
|
17
|
+
|
18
|
+
class Widget < ActiveRecord::Base
|
19
|
+
strip_commas_from_all_numbers
|
20
|
+
end
|
21
|
+
widget = Widget.new(:price => "1,200", :weight => "1,872.0")
|
22
|
+
widget.price == 1200 # true
|
23
|
+
widget.weight == 1872.0 # true
|
24
|
+
|
25
|
+
|
26
|
+
= the right way?
|
27
|
+
|
28
|
+
I'm surprised I couldn't find anything built into rails for handling this. If someone knows the "right way" to handle this, send me an email and I'll replace this project with a readme describing it.
|
29
|
+
|
30
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "ar_strip_commas"
|
16
|
+
gem.homepage = "http://github.com/tilleryj/ar_strip_commas"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{ActiveRecord extension for handling numeric column input with commas}
|
19
|
+
gem.description = %Q{An ActiveRecord extension for properly parsing strings with commas as input to numeric columns. }
|
20
|
+
gem.email = "tilleryj@thinklinkr.com"
|
21
|
+
gem.authors = ["Jason Tillery"]
|
22
|
+
end
|
23
|
+
Jeweler::RubygemsDotOrgTasks.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
class Base
|
3
|
+
|
4
|
+
def self.strip_commas_from(*columns)
|
5
|
+
unless (self.respond_to?(:write_attribute_without_strip_commas))
|
6
|
+
class_eval do
|
7
|
+
def write_attribute_with_strip_commas(f, v)
|
8
|
+
if (self.class.strip_commas_fields.include?(f.to_sym) && v.is_a?(String))
|
9
|
+
write_attribute_without_strip_commas(f, v.gsub(",", ""))
|
10
|
+
else
|
11
|
+
write_attribute_without_strip_commas(f, v)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
alias_method_chain :write_attribute, :strip_commas
|
16
|
+
cattr_accessor :strip_commas_fields
|
17
|
+
self.strip_commas_fields = []
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
columns.each do |column|
|
22
|
+
self.strip_commas_fields << column.to_sym
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.strip_commas_from_all_numbers
|
27
|
+
class_eval do
|
28
|
+
def convert_number_column_value(value)
|
29
|
+
if value == false
|
30
|
+
0
|
31
|
+
elsif value == true
|
32
|
+
1
|
33
|
+
elsif value.is_a?(String)
|
34
|
+
if (value.blank?)
|
35
|
+
nil
|
36
|
+
else
|
37
|
+
value.gsub(",", "")
|
38
|
+
end
|
39
|
+
else
|
40
|
+
value
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "ArStripCommas" do
|
4
|
+
before(:each) do
|
5
|
+
class Widget < ActiveRecord::Base
|
6
|
+
def self.columns
|
7
|
+
@columns ||= [];
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.column(name, sql_type = nil, default = nil, null = true)
|
11
|
+
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Override the save method to prevent exceptions.
|
15
|
+
def save(validate = true)
|
16
|
+
validate ? valid? : true
|
17
|
+
end
|
18
|
+
|
19
|
+
column :price, :integer
|
20
|
+
column :weight, :float
|
21
|
+
column :name, :string
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
after(:each) do
|
26
|
+
Object.send(:remove_const, :Widget)
|
27
|
+
end
|
28
|
+
|
29
|
+
context "should include class method .strip_commas_from_all_numbers" do
|
30
|
+
before(:each) do
|
31
|
+
Widget.strip_commas_from_all_numbers
|
32
|
+
@widget = Widget.new(:price => "2,810,010", :weight => "1,175.2", :name => "Awesome, Something")
|
33
|
+
end
|
34
|
+
|
35
|
+
it "that removes commas from strings on integer columns" do
|
36
|
+
@widget.price.should == 2810010
|
37
|
+
end
|
38
|
+
|
39
|
+
it "that removes commas from strings on float columns" do
|
40
|
+
@widget.weight.should == 1175.2
|
41
|
+
end
|
42
|
+
|
43
|
+
it "that does not remove commas from non-numeric columns" do
|
44
|
+
@widget.name.should == "Awesome, Something"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context "should include class method .strip_commas_from method" do
|
49
|
+
before(:each) do
|
50
|
+
Widget.strip_commas_from :price
|
51
|
+
@widget = Widget.new(:price => "2,810,010", :weight => "1,175.2", :name => "Awesome, Something")
|
52
|
+
end
|
53
|
+
|
54
|
+
it "that removes commas for the specified in column(s)" do
|
55
|
+
@widget.price.should == 2810010
|
56
|
+
end
|
57
|
+
|
58
|
+
it "that does not remove commas from unspecified columns" do
|
59
|
+
@widget.weight.should == 1.0
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'spec'
|
4
|
+
require 'rubygems'
|
5
|
+
require 'active_record'
|
6
|
+
require 'ar_strip_commas'
|
7
|
+
|
8
|
+
# Requires supporting files with custom matchers and macros, etc,
|
9
|
+
# in ./support/ and its subdirectories.
|
10
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
11
|
+
|
12
|
+
Spec::Runner.configure do |config|
|
13
|
+
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ar_strip_commas
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Jason Tillery
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-02-10 00:00:00 -06:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 23
|
28
|
+
segments:
|
29
|
+
- 1
|
30
|
+
- 0
|
31
|
+
- 0
|
32
|
+
version: 1.0.0
|
33
|
+
type: :development
|
34
|
+
name: bundler
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 7
|
44
|
+
segments:
|
45
|
+
- 1
|
46
|
+
- 5
|
47
|
+
- 2
|
48
|
+
version: 1.5.2
|
49
|
+
type: :development
|
50
|
+
name: jeweler
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: *id002
|
53
|
+
description: "An ActiveRecord extension for properly parsing strings with commas as input to numeric columns. "
|
54
|
+
email: tilleryj@thinklinkr.com
|
55
|
+
executables: []
|
56
|
+
|
57
|
+
extensions: []
|
58
|
+
|
59
|
+
extra_rdoc_files:
|
60
|
+
- LICENSE.txt
|
61
|
+
- README.rdoc
|
62
|
+
files:
|
63
|
+
- .document
|
64
|
+
- .rspec
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.rdoc
|
68
|
+
- Rakefile
|
69
|
+
- VERSION
|
70
|
+
- lib/ar_strip_commas.rb
|
71
|
+
- spec/ar_strip_commas_spec.rb
|
72
|
+
- spec/spec_helper.rb
|
73
|
+
has_rdoc: true
|
74
|
+
homepage: http://github.com/tilleryj/ar_strip_commas
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
hash: 3
|
88
|
+
segments:
|
89
|
+
- 0
|
90
|
+
version: "0"
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
hash: 3
|
97
|
+
segments:
|
98
|
+
- 0
|
99
|
+
version: "0"
|
100
|
+
requirements: []
|
101
|
+
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 1.3.7
|
104
|
+
signing_key:
|
105
|
+
specification_version: 3
|
106
|
+
summary: ActiveRecord extension for handling numeric column input with commas
|
107
|
+
test_files:
|
108
|
+
- spec/ar_strip_commas_spec.rb
|
109
|
+
- spec/spec_helper.rb
|