csv_hasher 0.0.2 → 0.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_hasher.rb +23 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c074f0c7b94510f3db7eab69b612f97ca564519f
|
4
|
+
data.tar.gz: 921fe312e1ceb3d381b290d7635daa9125b26e5a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ddc1bb47b321091c2797571f41eb0c08711b7155aee54354ed52a7731da0a98d824450583bd22c0067b0deed3f7eb9424742838ba7cd9916339b9142d60b3337
|
7
|
+
data.tar.gz: b5cb85953b77404d0f2c60073a957aa6f3487f14862334a69d06d2ef8db04e12f357618784ea56ba7f697f2bc661bd783a355bd9cc5c20351e5ffd1e97292fc3
|
data/lib/csv_hasher.rb
CHANGED
@@ -7,21 +7,38 @@ class CSVHasher
|
|
7
7
|
# Example:
|
8
8
|
# >> CSVHasher.hashify('path/to/csv/file')
|
9
9
|
# => [{:col_1=> '..', :col_2=> '..'},...]
|
10
|
-
#
|
10
|
+
#
|
11
11
|
#
|
12
12
|
# Arguments:
|
13
13
|
# path_to_csv: (String)
|
14
14
|
# options: (Hash)
|
15
|
-
|
15
|
+
# original_col_as_keys: (Boolean)
|
16
|
+
# if original_col_as_keys is true then keys are exactly the col headers
|
17
|
+
# if original_col_as_keys is false then keys are spidercase symbols from col headers
|
18
|
+
# keys: (Array) Custom keys for the returned hashes
|
19
|
+
#
|
20
|
+
#
|
21
|
+
def self.hashify(path_to_csv, options = { original_col_as_keys: false })
|
16
22
|
csv_arrs = CSV.read(path_to_csv)
|
17
|
-
keys = options
|
18
|
-
start_index = options[:keys].nil? || options[:include_headers] ? 1 : 0
|
23
|
+
keys = csv_keys options, csv_arrs
|
24
|
+
start_index = options[:keys].nil? || options[:include_headers] ? 1 : 0
|
19
25
|
csv_arrs[start_index..-1].map {|a| Hash[keys.zip(a)] }
|
20
26
|
end
|
21
27
|
|
22
|
-
private
|
28
|
+
private
|
29
|
+
|
30
|
+
#This method gets the csv_keys based on the options and csv_arrs
|
31
|
+
def self.csv_keys(options, csv_arrs)
|
32
|
+
if options[:keys]
|
33
|
+
options[:keys]
|
34
|
+
elsif options[:original_col_as_keys]
|
35
|
+
csv_arrs[0]
|
36
|
+
else
|
37
|
+
col_keys(csv_arrs[0])
|
38
|
+
end
|
39
|
+
end
|
23
40
|
|
24
|
-
# this method converts the header of the CSV into an array of sym keys
|
41
|
+
# this method converts the header of the CSV into an array of sym keys
|
25
42
|
def self.col_keys(keys)
|
26
43
|
keys.map{ |k| k.gsub(' ',' ').gsub(' ','_').downcase.to_sym }
|
27
44
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv_hasher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gaurav Singha Roy
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-05-
|
11
|
+
date: 2015-05-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|