honey_format 0.11.0 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 220883bb92b35808697b428d346d1f171d2d06d47d4a9ed79404b824a7e65781
4
- data.tar.gz: 9260762827e1fa50c1669696edfbc625a9e94ffc04773db81f27d6c8d9e7dd88
3
+ metadata.gz: 6d236d3a0eeee26825fc307504a6e65c66dbd30c5cbc8b4d190f5af391e73fc0
4
+ data.tar.gz: e5989adcda923101ccff44d34b15adffe7db4cbd1cabdfad3229dace94076c6b
5
5
  SHA512:
6
- metadata.gz: 245594826d582fdbbe02fb303a004197849f9ae17b184ecd4d57e833f98b220e28543bd3aaa1706221b9196b548d7adbd2b73f38ce96e24685f361645d86e307
7
- data.tar.gz: 3c907de2398718ae946aeb3984991b9273b359e667004c331e86a359e513c38e2d09836c52fba230a2a98e47cdb3e93a5e964b7583c48e6d43ce739018806bd8
6
+ metadata.gz: 5fa617674f689a27707c15a6a9d307181a8bda8662da9f40e5a3a689821c1269ca34762c4609511a7265cf75c04e551c1e1de8d5e2d0825826ff6a422aa77398
7
+ data.tar.gz: de36bd4aa3bd05e3e9f99d703b0ffe4674f894edaa85aecc3b51114f434dcdf9d6ea3b23c9b3b2b0c0c5319d63849c49532d46bcccb044ebc4190c77c9212eeb
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # v0.12.0
2
+
3
+ * Add `--[no-]only-header` option to CLI
4
+ * _[Bugfix]_: Handle missing --columns argument in CLI
5
+
1
6
  # v0.11.0
2
7
 
3
8
  * Add CLI: `honey_format` executable
data/README.md CHANGED
@@ -8,6 +8,7 @@ Convert CSV to an array of objects with with ease.
8
8
  - Convert column values with custom row builder
9
9
  - Convert header column names
10
10
  - Customize what columns and rows are included in CSV output
11
+ - [CLI](#cli)
11
12
  - Has no dependencies other than Ruby stdlib
12
13
  - Supports Ruby >= 2.3
13
14
 
@@ -185,6 +186,18 @@ You can see all [available errors here](https://www.rubydoc.info/gems/honey_form
185
186
 
186
187
  If you want to see more usage examples check out the `spec/` directory.
187
188
 
189
+ ## CLI
190
+
191
+ ```
192
+ Usage: honey_format [file.csv] [options]
193
+ --csv=input.csv CSV file
194
+ --columns=id,name Select columns.
195
+ --output=output.csv CSV output (STDOUT otherwise)
196
+ --delimiter=, CSV delimiter (default: ,)
197
+ -h, --help How to use
198
+ --version Show version
199
+ ```
200
+
188
201
  ## Benchmark
189
202
 
190
203
  _Note_: This gem, adds some overhead to parsing a CSV string. I've included some benchmarks below, your mileage may vary..
data/bin/benchmark CHANGED
@@ -4,16 +4,72 @@ require 'honey_format'
4
4
 
5
5
  require 'benchmark/ips'
6
6
  require 'csv'
7
+ require 'optparse'
7
8
 
8
- path = ARGV.first || fail(ArgumentError, '<path_to_csv> argument must be provided')
9
- csv = File.read(path)
9
+ input_path = nil
10
+ benchmark_time = 30
11
+ benchmark_warmup = 5
12
+ lines_multipliers = [1]
10
13
 
11
- Benchmark.ips do |x|
12
- x.time = 30
13
- x.warmup = 5
14
+ OptionParser.new do |parser|
15
+ parser.banner = "Usage: bin/benchmark [file.csv] [options]"
16
+ parser.default_argv = ARGV
14
17
 
15
- x.report('stdlib CSV') { CSV.parse(csv) }
16
- x.report('HoneyFormat::CSV') { HoneyFormat::CSV.new(csv).rows }
18
+ parser.on("--csv=file1.csv", String, "CSV file(s)") do |value|
19
+ input_path = value
20
+ end
17
21
 
18
- x.compare!
22
+ parser.on("--lines-multipliers=[1,10,50]", Array, "Multiply the rows in the CSV file (default: 1)") do |value|
23
+ lines_multipliers = value.map do |v|
24
+ Integer(v).tap do |int|
25
+ unless int >= 1
26
+ raise(ArgumentError, '--lines-multiplier must be 1 or greater')
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ parser.on("--time=[30]", String, "Benchmark time (default: 30)") do |value|
33
+ benchmark_time = Integer(value)
34
+ end
35
+
36
+ parser.on("--warmup=[30]", String, "Benchmark warmup (default: 30)") do |value|
37
+ benchmark_warmup = Integer(value)
38
+ end
39
+
40
+ parser.on("-h", "--help", "How to use") do
41
+ puts parser
42
+ exit
43
+ end
44
+
45
+ # No argument, shows at tail. This will print an options summary.
46
+ parser.on_tail("-h", "--help", "Show this message") do
47
+ puts parser
48
+ exit
49
+ end
50
+ end.parse!
51
+
52
+ csv = File.read(input_path)
53
+
54
+ lines_multipliers.each do |lines_multiplier|
55
+ if lines_multiplier > 1
56
+ orignial_csv_lines = csv.lines
57
+ rows = orignial_csv_lines[1..-1] * lines_multiplier
58
+ csv = orignial_csv_lines.first + rows.join
59
+ end
60
+
61
+ line_count = csv.lines.length
62
+
63
+ puts "== [START] Benchmark for #{line_count} lines =="
64
+ Benchmark.ips do |x|
65
+ x.time = benchmark_time
66
+ x.warmup = benchmark_warmup
67
+
68
+ x.report('stdlib CSV no options') { CSV.parse(csv) }
69
+ x.report('stdlib CSV with header') { CSV.parse(csv, headers: true) }
70
+ x.report('HoneyFormat::CSV') { HoneyFormat::CSV.new(csv).rows }
71
+
72
+ x.compare!
73
+ end
74
+ puts "== [END] Benchmark for #{line_count} lines =="
19
75
  end
data/exe/honey_format CHANGED
@@ -9,6 +9,7 @@ input_path = ARGV.first
9
9
  columns = nil
10
10
  output_path = nil
11
11
  delimiter = ','
12
+ header_only = false
12
13
 
13
14
  require 'optparse'
14
15
 
@@ -20,6 +21,10 @@ OptionParser.new do |parser|
20
21
  input_path = value
21
22
  end
22
23
 
24
+ parser.on("--[no-]only-header=[output.csv]", "Print only the header") do |value|
25
+ header_only = value
26
+ end
27
+
23
28
  parser.on("--columns=id,name", Array, "Select columns.") do |value|
24
29
  columns = value
25
30
  end
@@ -50,7 +55,12 @@ OptionParser.new do |parser|
50
55
  end.parse!
51
56
 
52
57
  csv = HoneyFormat::CSV.new(File.read(input_path), delimiter: delimiter)
53
- csv_string = csv.to_csv(columns: columns.map(&:to_sym))
58
+ if header_only
59
+ puts csv.columns.join(',')
60
+ exit
61
+ end
62
+
63
+ csv_string = csv.to_csv(columns: columns&.map(&:to_sym))
54
64
  if output_path
55
65
  File.write(output_path, csv_string)
56
66
  else
@@ -6,6 +6,7 @@ require 'honey_format/header'
6
6
  module HoneyFormat
7
7
  # Represents CSV.
8
8
  class CSV
9
+ # Instantiate CSV.
9
10
  # @return [CSV] a new instance of CSV.
10
11
  # @param [String] csv the CSV string
11
12
  # @param [String] delimiter the CSV delimiter
@@ -1,4 +1,4 @@
1
1
  module HoneyFormat
2
2
  # Gem version
3
- VERSION = '0.11.0'
3
+ VERSION = '0.12.0'
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: honey_format
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jacob Burenstam
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-06-09 00:00:00.000000000 Z
11
+ date: 2018-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler