csvp 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/csvp.rb +21 -11
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8cb41b2450b7bea60d05bdf52f1a13637d737f17
|
4
|
+
data.tar.gz: 38ca9ef1a7d02348cb936b27f6f537a2bcd9d23a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 415435b52116bbe0e727457fa0e70307ff88be4c7276ef28959fd57884aa403c2c4764a5276900e09653f1985af2aaa83a99bc9072cfc2cef98c105cdb16ca9a
|
7
|
+
data.tar.gz: e89e3355c1b3bce8438ff5e36fd197c34f06786874da122300b735ed81bc2e556484af5729f0cd47963e2c40fbcd76ef8172764f91af05ce0cb1abacefbdd3e0
|
data/lib/csvp.rb
CHANGED
@@ -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
|
24
|
-
when
|
25
|
-
when
|
26
|
-
when
|
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
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
when
|
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
|
42
|
-
when
|
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.
|
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-
|
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:
|
97
|
+
description: Effortlessly print enumerables as CSV.
|
98
98
|
email: martin.scavnicky@gmail.com
|
99
99
|
executables: []
|
100
100
|
extensions: []
|