csv_importable 0.1.9 → 0.1.10
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.
- checksums.yaml +4 -4
- data/README.md +1 -0
- data/lib/csv_importable/csv_coercion.rb +5 -1
- data/lib/csv_importable/type_parser.rb +31 -30
- data/lib/csv_importable/type_parser/us_zip_type_parser.rb +18 -0
- data/lib/csv_importable/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 34ba9cd0036b05b38eb8d8f292d0e99cfc3f9d46
|
4
|
+
data.tar.gz: 06d42511e5f3664c1949166a51dd6aa2b2d3e293
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 350527c95380b0f646c6daf9cdb5ec7c1d525eb9b5132ccc509753c7983f5cdbbbd297dc5816b6e6bea18f5a1aa7e9dc95589f6c601a6225c2a035d9de66adfb
|
7
|
+
data.tar.gz: d5981a6587dff38495c75d997676abd7b42777a7c26ee333a8dae25c654d0379fdd635f27b529a8ec749eab2d617289f8b32559d458a04263ab6f811bc5e2a69
|
data/README.md
CHANGED
@@ -299,6 +299,7 @@ If the parser fails to coerce the data properly, it will add an error message to
|
|
299
299
|
- pull_date
|
300
300
|
- pull_float
|
301
301
|
- pull_integer
|
302
|
+
- pull_us_zip
|
302
303
|
- pull_select (e.g. `pull_select('color', options: ['Red', 'Green', 'Blue'])`)
|
303
304
|
|
304
305
|
Basic syntax: `pull_string(column_key, args)` where `column_key` is the CSV header string for the column and `args` is a hash with the following defaults: `{ required: false, row: row }`
|
@@ -28,7 +28,11 @@ module CSVImportable
|
|
28
28
|
CSVImportable::TypeParser::SelectTypeParser
|
29
29
|
end
|
30
30
|
|
31
|
-
|
31
|
+
def us_zip_type_class
|
32
|
+
CSVImportable::TypeParser::USZipTypeParser
|
33
|
+
end
|
34
|
+
|
35
|
+
[:string, :date, :boolean, :integer, :float, :percent, :select, :us_zip].each do |parser_type|
|
32
36
|
define_method("pull_#{parser_type}") do |key, options={}|
|
33
37
|
csv_row = options.fetch(:row, @row)
|
34
38
|
options = options.merge(row: csv_row)
|
@@ -27,44 +27,44 @@ module CSVImportable
|
|
27
27
|
|
28
28
|
private
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
30
|
+
def after_init(args)
|
31
|
+
# hook for subclasses
|
32
|
+
end
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
34
|
+
def pull_value_from_row
|
35
|
+
return nil unless row
|
36
|
+
# handle both caps and lowercase
|
37
|
+
row.field(key) || row.field(key.upcase) || row.field(key.downcase)
|
38
|
+
end
|
39
39
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
40
|
+
def parse_val
|
41
|
+
# hook for subclasses
|
42
|
+
fail 'parse_val is a required method for subclass'
|
43
|
+
end
|
44
44
|
|
45
|
-
|
46
|
-
|
47
|
-
|
45
|
+
def raise_parsing_error
|
46
|
+
raise error_message
|
47
|
+
end
|
48
48
|
|
49
|
-
|
50
|
-
|
51
|
-
|
49
|
+
def error_message
|
50
|
+
fail 'error_message is a requird method for subclass'
|
51
|
+
end
|
52
52
|
|
53
|
-
|
54
|
-
|
55
|
-
|
53
|
+
def raise_required_error
|
54
|
+
raise required_error_message
|
55
|
+
end
|
56
56
|
|
57
|
-
|
58
|
-
|
59
|
-
|
57
|
+
def required_error_message
|
58
|
+
"#{key} is blank"
|
59
|
+
end
|
60
60
|
|
61
|
-
|
62
|
-
|
63
|
-
|
61
|
+
def required?
|
62
|
+
required
|
63
|
+
end
|
64
64
|
|
65
|
-
|
66
|
-
|
67
|
-
|
65
|
+
def check_required
|
66
|
+
return raise_required_error if required?
|
67
|
+
end
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
@@ -75,3 +75,4 @@ require_relative './type_parser/integer_type_parser'
|
|
75
75
|
require_relative './type_parser/percent_type_parser'
|
76
76
|
require_relative './type_parser/select_type_parser'
|
77
77
|
require_relative './type_parser/string_type_parser'
|
78
|
+
require_relative './type_parser/us_zip_type_parser'
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module CSVImportable
|
2
|
+
class TypeParser::USZipTypeParser < TypeParser
|
3
|
+
def parse_val
|
4
|
+
val = value.delete('-')
|
5
|
+
not_digits unless val.count('0-9') == val.length
|
6
|
+
return value if val.length == 9
|
7
|
+
val.rjust(5, '0')
|
8
|
+
end
|
9
|
+
|
10
|
+
def not_digits
|
11
|
+
raise
|
12
|
+
end
|
13
|
+
|
14
|
+
def error_message
|
15
|
+
"Invalid value for column: #{key}. Value should contain only numbers or a dash."
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv_importable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Francis
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-12-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -97,6 +97,7 @@ files:
|
|
97
97
|
- lib/csv_importable/type_parser/percent_type_parser.rb
|
98
98
|
- lib/csv_importable/type_parser/select_type_parser.rb
|
99
99
|
- lib/csv_importable/type_parser/string_type_parser.rb
|
100
|
+
- lib/csv_importable/type_parser/us_zip_type_parser.rb
|
100
101
|
- lib/csv_importable/version.rb
|
101
102
|
homepage: https://github.com/LaunchPadLab/csv_importable
|
102
103
|
licenses: []
|