csvp 0.1.1 → 0.2.0

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 +18 -21
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8cb41b2450b7bea60d05bdf52f1a13637d737f17
4
- data.tar.gz: 38ca9ef1a7d02348cb936b27f6f537a2bcd9d23a
3
+ metadata.gz: a766e9348e13295b12e40d73354c12515ff00dd3
4
+ data.tar.gz: 96a31740723ee4b08f1f3e07172b516ea57e8606
5
5
  SHA512:
6
- metadata.gz: 415435b52116bbe0e727457fa0e70307ff88be4c7276ef28959fd57884aa403c2c4764a5276900e09653f1985af2aaa83a99bc9072cfc2cef98c105cdb16ca9a
7
- data.tar.gz: e89e3355c1b3bce8438ff5e36fd197c34f06786874da122300b735ed81bc2e556484af5729f0cd47963e2c40fbcd76ef8172764f91af05ce0cb1abacefbdd3e0
6
+ metadata.gz: 6c615a14d1214a5463e578c46e8a1dda086572f8f6269989c93fcb70915a7881c7d0ceb4fd539a94e083bf1abc11328f857c499584c22864d34e4a43d05ac977
7
+ data.tar.gz: 33e6899cd903fc0142ef00717c3d0d52a118411d665805c557756acd0f1d02a74d8c187e1492dd32ecf1488c98abd6efcdeafdf64336d2fb02d975877043dd36
@@ -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 separator [String]
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
- # Print out the rest of the CSV.
48
- enum.each do |row|
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
- puts values.map(&:to_s).map(&add_quotes).join(separator)
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, separator: "\t"
61
+ csvp enum, col_sep: "\t"
65
62
  end
66
63
 
67
64
  def qcsvp(enum)
68
- csvp enum, quote: "\""
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.1.1
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-05 00:00:00.000000000 Z
11
+ date: 2016-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec