csv_hasher 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/csv_hasher.rb +31 -0
  3. metadata +58 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8eb01a0ad15f32ec2aff01b76bb3c1d2c495fd04
4
+ data.tar.gz: df60c950f7332163b6a16c17e092ebb3a4263675
5
+ SHA512:
6
+ metadata.gz: 506b131d0391a274c586cf9d295d8e05c316f7b149fa897ed06e9d10076294724798723b42f00921bcddd115a05195723547fcf2f8d5f65fee2e8430e6f0c136
7
+ data.tar.gz: 753d7d5df449ac851e09b2ae1c92f25275ea34e4b6753ed83a9964f09ea4edfaac758fbb987f04fd8254756836fa45c8390641e59975a87e68544279a8f44289
data/lib/csv_hasher.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'csv'
2
+
3
+ class CSVHasher
4
+
5
+ # get arrays of hashes from a CSV file instead of arrays of arrays
6
+ #
7
+ # Example:
8
+ # >> CSVHasher.hashify('path/to/csv/file')
9
+ # => [{:col_1=> '..', :col_2=> '..'},...]
10
+ #
11
+ #
12
+ # Arguments:
13
+ # path_to_csv: (String)
14
+ # options: (Hash)
15
+ def self.hashify(path_to_csv, options = {})
16
+ 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
19
+ csv_arrs[start_index..-1].map {|a| Hash[keys.zip(a)] }
20
+ end
21
+
22
+ private
23
+
24
+ # this method converts the header of the CSV into an array of sym keys
25
+ def self.col_keys(keys)
26
+ keys.map{ |k| k.gsub(' ',' ').gsub(' ','_').downcase.to_sym }
27
+ end
28
+
29
+ end
30
+
31
+
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csv_hasher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Gaurav Singha Roy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: A simple gem to return arrays of hashes for a CSV file
28
+ email: neogauravsvnit@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/csv_hasher.rb
34
+ homepage: http://rubygems.org/gems/csv_hasher
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.4.3
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: CSV Hasher
58
+ test_files: []