hash_to_csv 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/hash_to_csv/hash_to_csv.rb +60 -56
  2. metadata +18 -8
@@ -1,57 +1,61 @@
1
- require 'csv'
2
- require 'hash_flattener'
3
-
4
- module HashToCsv
5
- class HashToCsv
6
-
7
- def initialize(hash_flattener)
8
- @hash_flattener = hash_flattener
9
- end
10
-
11
- def to_csv(hashes, options={}, &block)
12
- @hashes = [hashes].flatten.map { |h| @hash_flattener.flatten h }
13
- @include = options[:include]
14
- @exclude = options[:exclude]
15
- @sort = options[:sort] == true
16
-
17
- CSV.generate do |csv|
18
- csv << column_names
19
- rows.each { |row| csv << values_for_row(row, &block) }
20
- end
21
- end
22
-
23
- private
24
-
25
- def column_names
26
- return @column_names if @column_names
27
- keys = all_keys
28
- keys.select! { |key| @include.index { |pattern| column_matches_pattern? key, pattern } } if @include
29
- keys.reject! { |key| @exclude.index { |pattern| column_matches_pattern? key, pattern } } if @exclude
30
- keys.sort! if @sort
31
- @column_names = keys
32
- end
33
-
34
- def rows
35
- @hashes
36
- end
37
-
38
- def values_for_row(row, &block)
39
- column_names.map do |column_name|
40
- value = row[column_name]
41
- value = block.call value, column_name if block
42
- value
43
- end
44
- end
45
-
46
- def all_keys
47
- keys = Set.new
48
- @hashes.each { |h| keys += h.keys }
49
- keys.to_a
50
- end
51
-
52
- def column_matches_pattern?(column, pattern)
53
- pattern.kind_of?(Regexp) ? column =~ pattern : column == pattern.to_s
54
- end
55
-
56
- end
1
+ require 'csv'
2
+ require 'hash_flattener'
3
+
4
+ module HashToCsv
5
+ class HashToCsv
6
+
7
+ def initialize(hash_flattener)
8
+ @hash_flattener = hash_flattener
9
+ end
10
+
11
+ def to_csv(hashes, options={}, &block)
12
+ @hashes = [hashes].flatten.map { |h| @hash_flattener.flatten h }
13
+ @include = options[:include]
14
+ @exclude = options[:exclude]
15
+ @sort = options[:sort] == true
16
+
17
+ CSV.generate do |csv|
18
+ csv << column_names
19
+ rows.each { |row| csv << values_for_row(row, &block) }
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def column_names
26
+ return @column_names if @column_names
27
+ keys = all_keys
28
+
29
+ keys = @include.map do |include_pattern|
30
+ keys.select { |key| column_matches_pattern? key, include_pattern }
31
+ end.flatten if @include
32
+
33
+ keys.reject! { |key| @exclude.index { |pattern| column_matches_pattern? key, pattern } } if @exclude
34
+ keys.sort! if @sort
35
+ @column_names = keys
36
+ end
37
+
38
+ def rows
39
+ @hashes
40
+ end
41
+
42
+ def values_for_row(row, &block)
43
+ column_names.map do |column_name|
44
+ value = row[column_name]
45
+ value = block.call value, column_name if block
46
+ value
47
+ end
48
+ end
49
+
50
+ def all_keys
51
+ keys = Set.new
52
+ @hashes.each { |h| keys += h.keys }
53
+ keys.to_a
54
+ end
55
+
56
+ def column_matches_pattern?(column, pattern)
57
+ pattern.kind_of?(Regexp) ? column =~ pattern : column == pattern.to_s
58
+ end
59
+
60
+ end
57
61
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_to_csv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,22 +9,27 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-18 00:00:00.000000000 Z
12
+ date: 2013-05-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: hash_flattener
16
- requirement: &70331448723380 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - =
19
+ - - '='
20
20
  - !ruby/object:Gem::Version
21
21
  version: 0.2.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70331448723380
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.2.0
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: rspec
27
- requirement: &70331448722960 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,7 +37,12 @@ dependencies:
32
37
  version: '0'
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *70331448722960
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  description:
37
47
  email: matthew-github@matthewriley.name
38
48
  executables: []
@@ -61,7 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
71
  version: '0'
62
72
  requirements: []
63
73
  rubyforge_project:
64
- rubygems_version: 1.8.15
74
+ rubygems_version: 1.8.24
65
75
  signing_key:
66
76
  specification_version: 3
67
77
  summary: Hash to CSV is a Ruby Gem that flattens an array of hashes to CSV.