hash_to_csv 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,50 @@
1
+ require 'csv'
2
+ require 'hash_flattener'
3
+
4
+ module HashToCsv
5
+ class HashToCsv
6
+
7
+ def initialize(hashes, options={})
8
+ @hashes = [hashes].flatten.map { |h| HashFlattener.flatten h }
9
+ @include = options[:include] || []
10
+ @exclude = options[:exclude] || []
11
+ @sort = options[:sort] || true
12
+ end
13
+
14
+ def to_csv
15
+ CSV.generate do |csv|
16
+ csv << column_names
17
+ rows.each { |row| csv << values_for_row(row) }
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def column_names
24
+ return @column_names if @column_names
25
+ keys = all_keys
26
+ keys.select! { |key| @include.index { |pattern| column_matches_pattern? key, pattern } }
27
+ keys.reject! { |key| @exclude.index { |pattern| column_matches_pattern? key, pattern } }
28
+ keys.sort! if @sort
29
+ @column_names = keys
30
+ end
31
+
32
+ def rows
33
+ @hashes
34
+ end
35
+
36
+ def values_for_row(row)
37
+ columns.map { |column| row[column] }
38
+ end
39
+
40
+ def all_keys
41
+ keys = Set.new
42
+ @hashes.each { |h| keys += h.keys }
43
+ end
44
+
45
+ def column_matches_pattern?(column, pattern)
46
+ pattern.kind_of?(Regexp) ? column =~ pattern : column == pattern
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,9 @@
1
+ require_relative 'hash_to_csv/hash_to_csv'
2
+
3
+ module HashToCsv
4
+
5
+ def self.to_csv(hash, options={})
6
+ HashToCsv.new(hash, options).to_csv
7
+ end
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash_to_csv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matthew Riley
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: hash_flattener
16
+ requirement: &70290319751300 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - =
20
+ - !ruby/object:Gem::Version
21
+ version: 0.1.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70290319751300
25
+ description:
26
+ email: matthew-github@matthewriley.name
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files: []
30
+ files:
31
+ - lib/hash_to_csv/hash_to_csv.rb
32
+ - lib/hash_to_csv.rb
33
+ homepage: http://rubygems.org/gems/hash_to_csv
34
+ licenses: []
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 1.8.15
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: Hash to CSV is a Ruby Gem that flattens an array of hashes to CSV.
57
+ test_files: []