csvp 0.1.1 → 0.2.0
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 +18 -21
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a766e9348e13295b12e40d73354c12515ff00dd3
|
4
|
+
data.tar.gz: 96a31740723ee4b08f1f3e07172b516ea57e8606
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c615a14d1214a5463e578c46e8a1dda086572f8f6269989c93fcb70915a7881c7d0ceb4fd539a94e083bf1abc11328f857c499584c22864d34e4a43d05ac977
|
7
|
+
data.tar.gz: 33e6899cd903fc0142ef00717c3d0d52a118411d665805c557756acd0f1d02a74d8c187e1492dd32ecf1488c98abd6efcdeafdf64336d2fb02d975877043dd36
|
data/lib/csvp.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
|
+
require 'csv'
|
2
|
+
|
1
3
|
# Prints Enumerable in CSV format on the best effort basis.
|
2
4
|
#
|
3
|
-
# @param enum [Enumerable]
|
4
|
-
# @param
|
5
|
-
# @param quote [String]
|
5
|
+
# @param enum [Enumerable] Enumberable to print.
|
6
|
+
# @param options [Hash] Options described in Ruby CSV module.
|
6
7
|
#
|
7
8
|
# Example:
|
8
9
|
# ?> csvp [{a: 1, b: 2}, {a: 3, b: 4} ]
|
@@ -11,12 +12,9 @@
|
|
11
12
|
# 3,4
|
12
13
|
#
|
13
14
|
# This implementation attempts to follow https://tools.ietf.org/html/rfc4180
|
14
|
-
|
15
|
-
def csvp(enum, separator: ',', quote: "")
|
15
|
+
def csvp(enum, options = {})
|
16
16
|
return if enum.nil?
|
17
17
|
|
18
|
-
add_quotes = proc { |str| "#{quote}#{str.gsub(quote, quote*2)}#{quote}" }
|
19
|
-
|
20
18
|
# Case rules encapsulated as procs.
|
21
19
|
open_struct = proc { |obj| defined?(OpenStruct) && OpenStruct === obj }
|
22
20
|
active_record_base = proc { |obj| defined?(ActiveRecord::Base) && ActiveRecord::Base === obj }
|
@@ -35,35 +33,34 @@ def csvp(enum, separator: ',', quote: "")
|
|
35
33
|
|
36
34
|
# Try to determine the header row.
|
37
35
|
first = enum.first
|
38
|
-
|
39
36
|
columns = case first
|
40
37
|
when Hash then first.keys
|
41
38
|
when Struct then first.members
|
42
39
|
when open_struct then first.instance_variable_get("@table").keys
|
43
40
|
when active_record_base then first.attributes.keys
|
44
41
|
end
|
45
|
-
puts columns.map(&:to_s).map(&add_quotes).join(separator) if columns
|
46
42
|
|
47
|
-
|
48
|
-
|
49
|
-
values = case row
|
50
|
-
when Hash then row.values
|
51
|
-
when open_struct then row.instance_variable_get("@table").values
|
52
|
-
when active_record_base then row.attributes.values
|
53
|
-
when Enumerable then row
|
54
|
-
else [row]
|
55
|
-
end
|
43
|
+
CSV($stdout, options) do |csv|
|
44
|
+
csv << columns if columns
|
56
45
|
|
57
|
-
|
46
|
+
enum.each do |row|
|
47
|
+
csv << case row
|
48
|
+
when Hash then row.values
|
49
|
+
when open_struct then row.instance_variable_get("@table").values
|
50
|
+
when active_record_base then row.attributes.values
|
51
|
+
when Enumerable then row
|
52
|
+
else [row]
|
53
|
+
end
|
54
|
+
end
|
58
55
|
end
|
59
56
|
|
60
57
|
nil
|
61
58
|
end
|
62
59
|
|
63
60
|
def tsvp(enum)
|
64
|
-
csvp enum,
|
61
|
+
csvp enum, col_sep: "\t"
|
65
62
|
end
|
66
63
|
|
67
64
|
def qcsvp(enum)
|
68
|
-
csvp enum,
|
65
|
+
csvp enum, quote_char: "\"", force_quotes: true
|
69
66
|
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.
|
4
|
+
version: 0.2.0
|
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-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|