Csv-Processor 1.0.0 → 1.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.
- checksums.yaml +4 -4
- data/lib/csv_processor.rb +7 -9
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f5c7c9c32499c8bc1f7d962bd5d6cc8cd3dd8277
|
4
|
+
data.tar.gz: a0d507f746d79132e1d0584fe9c70a860082c24f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2cf42937e0513f292b1e1a8cd0c24b379d9ac330d0c06dd489619830e6625fdeade0614c6b1e7fee26c74edea18739e48c4091cd44b6d94a9571a03769140812
|
7
|
+
data.tar.gz: 432f9a5eb2c9a3404b3c6bd5b9066f66eb51dcef400ec497245e3b7f9a5ec88ac9432222f9e6888bccd8b6bc94c17a76da7c4f941a07c3cbe9b58420caff6d20
|
data/lib/csv_processor.rb
CHANGED
@@ -1,17 +1,15 @@
|
|
1
1
|
require 'csv'
|
2
2
|
module CsvProcessor
|
3
3
|
|
4
|
-
def import_from(csv_path, only =
|
4
|
+
def import_from(csv_path, only = [], except = [])
|
5
5
|
raise("Method does not accept only and except params at once") if !only.empty? && !except.empty?
|
6
6
|
csv_text = File.read(csv_path)
|
7
7
|
csv = CSV.parse(csv_text, :headers => true)
|
8
8
|
only_except_logic(csv, only, except)
|
9
|
-
csv.
|
10
|
-
self.create!(row.to_hash)
|
11
|
-
end
|
9
|
+
csv.map { |row| self.create!(row.to_hash) }
|
12
10
|
end
|
13
11
|
|
14
|
-
def to_csv(options = {}, only =
|
12
|
+
def to_csv(options = {}, only = [], except = [])
|
15
13
|
raise("Method does not accept only and except params at once") if !only.empty? && !except.empty?
|
16
14
|
CSV.generate(options) do |csv|
|
17
15
|
column_names = self.column_names
|
@@ -24,15 +22,15 @@ module CsvProcessor
|
|
24
22
|
end
|
25
23
|
|
26
24
|
private
|
27
|
-
def only_except_logic(
|
25
|
+
def only_except_logic(list, only, except)
|
28
26
|
if only.empty? && !except.empty?
|
29
27
|
except.each do |key|
|
30
|
-
|
28
|
+
list.delete(key.to_s)
|
31
29
|
end
|
32
30
|
elsif !only.empty? && except.empty?
|
33
|
-
|
31
|
+
list = []
|
34
32
|
only.each do |key|
|
35
|
-
|
33
|
+
list << key.to_s
|
36
34
|
end
|
37
35
|
else
|
38
36
|
end
|