csvp 0.1.0 → 0.1.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 +4 -4
  2. data/lib/csvp.rb +21 -11
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bd64e586de8ea735f1d88ad7c881bb4d6c637a4b
4
- data.tar.gz: 3034ebd73917f9a3f21b6a33e1b911672bbdac55
3
+ metadata.gz: 8cb41b2450b7bea60d05bdf52f1a13637d737f17
4
+ data.tar.gz: 38ca9ef1a7d02348cb936b27f6f537a2bcd9d23a
5
5
  SHA512:
6
- metadata.gz: 8877057a52f562f88d75b89b900dccb12e6a8dfaef081a09687930f47c2aad371a8a11dc3f96f0f6d22405c59f8e4f613e94af0ceb3b93c96e84dd634673897f
7
- data.tar.gz: 62c7420dfb8f263ce38ac7085600a6a64c76f3863be6b2c2d16b607245f631b530f1c94a9b39a01e6cd9725419e3582626438fbbbb46c7b737bf1f1b60912160
6
+ metadata.gz: 415435b52116bbe0e727457fa0e70307ff88be4c7276ef28959fd57884aa403c2c4764a5276900e09653f1985af2aaa83a99bc9072cfc2cef98c105cdb16ca9a
7
+ data.tar.gz: e89e3355c1b3bce8438ff5e36fd197c34f06786874da122300b735ed81bc2e556484af5729f0cd47963e2c40fbcd76ef8172764f91af05ce0cb1abacefbdd3e0
@@ -17,29 +17,39 @@ def csvp(enum, separator: ',', quote: "")
17
17
 
18
18
  add_quotes = proc { |str| "#{quote}#{str.gsub(quote, quote*2)}#{quote}" }
19
19
 
20
+ # Case rules encapsulated as procs.
21
+ open_struct = proc { |obj| defined?(OpenStruct) && OpenStruct === obj }
22
+ active_record_base = proc { |obj| defined?(ActiveRecord::Base) && ActiveRecord::Base === obj }
23
+ active_record_relation = proc { |obj| defined?(ActiveRecord::Relation) && ActiveRecord::Relation === obj }
24
+ not_enumerable = proc { |obj| !obj.is_a?(Enumerable) }
25
+
20
26
  # Vectorize scalar values into Enumerables to avoid multiple codepaths.
21
27
  enum = case enum
22
28
  when Struct then enum.to_h
23
- when OpenStruct then enum.to_h
24
- when -> (obj) { defined?(ActiveRecord::Base) && ActiveRecord::Base === obj } then enum.attributes
25
- when -> (obj) { defined?(ActiveRecord::Relation) && ActiveRecord::Relation === obj } then enum.to_a
26
- when -> (obj) { !obj.is_a?(Enumerable) } then [enum]
29
+ when open_struct then enum.to_h
30
+ when active_record_base then enum.attributes
31
+ when active_record_relation then enum.to_a
32
+ when not_enumerable then [enum]
27
33
  else enum
28
34
  end
29
35
 
30
- columns = case enum.first
31
- when Hash then enum.first.keys
32
- when Struct then enum.first.members
33
- when OpenStruct then enum.first.instance_variable_get("@table").keys
34
- when -> (obj) { defined?(ActiveRecord::Base) && ActiveRecord::Base === obj } then enum.first.attributes.keys
36
+ # Try to determine the header row.
37
+ first = enum.first
38
+
39
+ columns = case first
40
+ when Hash then first.keys
41
+ when Struct then first.members
42
+ when open_struct then first.instance_variable_get("@table").keys
43
+ when active_record_base then first.attributes.keys
35
44
  end
36
45
  puts columns.map(&:to_s).map(&add_quotes).join(separator) if columns
37
46
 
47
+ # Print out the rest of the CSV.
38
48
  enum.each do |row|
39
49
  values = case row
40
50
  when Hash then row.values
41
- when OpenStruct then row.instance_variable_get("@table").values
42
- when -> (obj) { defined?(ActiveRecord::Base) && ActiveRecord::Base === obj } then row.attributes.values
51
+ when open_struct then row.instance_variable_get("@table").values
52
+ when active_record_base then row.attributes.values
43
53
  when Enumerable then row
44
54
  else [row]
45
55
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csvp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Scavnicky
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-03 00:00:00.000000000 Z
11
+ date: 2016-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -94,7 +94,7 @@ dependencies:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0.1'
97
- description: Print enumerables as CSV effortlessly.
97
+ description: Effortlessly print enumerables as CSV.
98
98
  email: martin.scavnicky@gmail.com
99
99
  executables: []
100
100
  extensions: []