csv_hasher 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/csv_hasher.rb +23 -6
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dcfc86d8bae5e36ee0d70da76d0f59d58f8f8e57
4
- data.tar.gz: ee29fc3cd31842c4d8f009618fec53f79bc78409
3
+ metadata.gz: c074f0c7b94510f3db7eab69b612f97ca564519f
4
+ data.tar.gz: 921fe312e1ceb3d381b290d7635daa9125b26e5a
5
5
  SHA512:
6
- metadata.gz: a40ce51ab5a91c26f8a5fcbb20c05cec89bbc97190add3ce8a78ff9f594bbe6b4b98767ff28eb65bf1104bba463bbb59a20ee3a360a44e2575e777efc99e8051
7
- data.tar.gz: 25533da8d98534605d70081681d657b7bd6ccedb8310e23d27b09cd7e39b90c8438dff548624f3b31867be827434c0fbc17f029b24186d4c7008048986cd6fb9
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
- def self.hashify(path_to_csv, options = {})
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[:keys] || col_keys(csv_arrs[0])
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.2
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-25 00:00:00.000000000 Z
11
+ date: 2015-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec