Skip to content

Commit f38c4d1

Browse files
author
Max Rukomoynikov
committed
Console printer
1 parent 9ac6ef7 commit f38c4d1

11 files changed

Lines changed: 124 additions & 9 deletions

File tree

.rubocop_todo.yml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config --auto-gen-only-exclude`
3-
# on 2022-05-23 08:35:12 UTC using RuboCop version 1.29.1.
3+
# on 2022-06-21 14:52:48 UTC using RuboCop version 1.29.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -13,6 +13,12 @@ Gemspec/RequiredRubyVersion:
1313
Exclude:
1414
- 'tabled.gemspec'
1515

16+
# Offense count: 1
17+
# Configuration parameters: AllowComments.
18+
Lint/EmptyFile:
19+
Exclude:
20+
- 'lib/parsers/csv_parser.rb'
21+
1622
# Offense count: 3
1723
# Configuration parameters: IgnoredMethods, CountRepeatedAttributes, Max.
1824
Metrics/AbcSize:
@@ -29,11 +35,10 @@ Metrics/BlockLength:
2935
- 'spec/framed/tabled_spec.rb'
3036
- 'spec/non_framed/tabled_spec.rb'
3137

32-
# Offense count: 2
38+
# Offense count: 1
3339
# Configuration parameters: IgnoredMethods, Max.
3440
Metrics/CyclomaticComplexity:
3541
Exclude:
36-
- 'lib/content_shaper.rb'
3742
- 'lib/helpers.rb'
3843

3944
# Offense count: 4
@@ -44,11 +49,10 @@ Metrics/MethodLength:
4449
- 'lib/helpers.rb'
4550
- 'spec/factories/factory_data.rb'
4651

47-
# Offense count: 2
52+
# Offense count: 1
4853
# Configuration parameters: IgnoredMethods, Max.
4954
Metrics/PerceivedComplexity:
5055
Exclude:
51-
- 'lib/content_shaper.rb'
5256
- 'lib/helpers.rb'
5357

5458
# Offense count: 1
@@ -57,12 +61,13 @@ RSpec/ExampleLength:
5761
Exclude:
5862
- 'spec/framed/tabled_spec.rb'
5963

60-
# Offense count: 6
64+
# Offense count: 8
6165
# Configuration parameters: AllowedConstants.
6266
Style/Documentation:
6367
Exclude:
6468
- 'spec/**/*'
6569
- 'test/**/*'
70+
- 'bin/tabled'
6671
- 'lib/content_shaper.rb'
6772
- 'lib/helpers.rb'
6873
- 'lib/tabled.rb'

Gemfile.lock

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
PATH
22
remote: .
33
specs:
4-
tabled (0.0.3)
4+
tabled (0.0.4)
5+
dry-cli
56

67
GEM
78
remote: http://rubygems.org/
@@ -10,6 +11,7 @@ GEM
1011
byebug (11.1.3)
1112
diff-lcs (1.5.0)
1213
docile (1.4.0)
14+
dry-cli (0.7.0)
1315
parallel (1.22.1)
1416
parser (3.1.2.0)
1517
ast (~> 2.4.1)

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
[![Gem Version](https://badge.fury.io/rb/tabled.svg)](https://badge.fury.io/rb/tabled) ![CI is pasing for ruby 2.6 - 3.0](https://github.com/rukomoynikov/tabled/actions/workflows/linters.yml/badge.svg) ![Downloads](https://badgen.net/rubygems/dt/tabled)
23

34

@@ -58,6 +59,12 @@ Result
5859
----------------------------------------------------------------
5960
```
6061

62+
# Printing CSV files to console
63+
As a part of the gem `tabled` binary is included. So, you can basicaly run it like:
64+
```shell
65+
tabled print path_to_csv_file
66+
```
67+
6168
# Contributing
6269
1. Fork it ( http://github.com/rukomoynikov/tabled/fork )
6370
2. Create your feature branch (git checkout -b my-new-feature)

bin/tabled

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require_relative '../lib/tabled'
5+
require_relative '../lib/parsers/csv_parser'
6+
require 'dry/cli'
7+
require 'pathname'
8+
9+
module TabledCli
10+
module CLI
11+
module Commands
12+
extend Dry::CLI::Registry
13+
14+
class Print < Dry::CLI::Command
15+
desc 'Printing CSV to console'
16+
17+
argument :input, desc: 'Input file to print'
18+
19+
def call(input: nil, **)
20+
return p 'File must be provided' if input.nil?
21+
return p 'File doesn\'t exist' unless File.exist?(Pathname.new(input))
22+
23+
Tabled.new(Tabled::CSVParser.parse(Pathname.new(input))).print_to_console
24+
end
25+
end
26+
27+
register 'print', Print, aliases: ['p', '-p', '--print']
28+
end
29+
end
30+
end
31+
32+
Dry::CLI.new(TabledCli::CLI::Commands).call

lib/parsers/csv_parser.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'csv'
2+
class Tabled
3+
class CSVParser
4+
def self.parse(file_path)
5+
::CSV.read(file_path)
6+
end
7+
end
8+
end

spec/binary_reader/binary_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
describe 'Running tabled from console to print CSV file', type: :aruba do
4+
context 'when called without file argument' do
5+
specify do
6+
expect do
7+
system('./bin/tabled print')
8+
end.to output(/File must be provided/).to_stdout_from_any_process
9+
end
10+
end
11+
12+
context 'when file doesn\'t exist' do
13+
specify do
14+
expect do
15+
system('./bin/tabled print file.csv')
16+
end.to output(/File doesn't exist/).to_stdout_from_any_process
17+
end
18+
end
19+
20+
context 'when file exist' do
21+
specify do
22+
expect do
23+
system('./bin/tabled print spec/fixtures/starwars.csv')
24+
end.to output(Factories::ProcessedData::Framed.star_wars_list).to_stdout_from_any_process
25+
end
26+
end
27+
end

spec/factories/factory_data.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,28 @@ def raw
1515
end
1616

1717
class ProcessedData
18+
class Framed
19+
class << self
20+
def star_wars_list
21+
['------------------------------',
22+
'| name height mass |',
23+
'------------------------------',
24+
'| Luke Skywalker 172 11 |',
25+
'------------------------------',
26+
'| C-3PO 167 75 |',
27+
'------------------------------',
28+
'| R2-D2 96 32 |',
29+
'------------------------------',
30+
'| Darth Vader 202 136 |',
31+
'------------------------------',
32+
'| Leia Organa 150 49 |',
33+
'------------------------------',
34+
'| Owen Lars 178 120 |',
35+
'------------------------------'].join("\n")
36+
end
37+
end
38+
end
39+
1840
class << self
1941
def without_row_separator
2042
[

spec/fixtures/starwars.csv

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name,height,mass
2+
Luke Skywalker,172, 11
3+
C-3PO,167,75
4+
R2-D2,96,32
5+
Darth Vader,202,136
6+
Leia Organa,150,49
7+
Owen Lars,178,120

spec/framed/tabled_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
require 'tabled'
34

45
describe Tabled do

spec/non_framed/tabled_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
require 'tabled'
34

45
describe Tabled do

0 commit comments

Comments
 (0)