painless-table 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cb1d48891865bbf3fb0f5f177c18edba193e83a7
4
+ data.tar.gz: 123465a67339328d30adf9e247802c9cb4adc38f
5
+ SHA512:
6
+ metadata.gz: 23110c267332ad27d2a5ff3413042d7d2ab3bcda9eba2d42f8c39fdf2dd735c6676d91afa1755037bb2c92aa6c7d2d69b529db8ab3d6aacd5f7e9d1176aa207a
7
+ data.tar.gz: f78070abf47f51f849a6aae41f105fc1f333dc16e89d90a76763c4e252d6b23fcc53158186647531e0fd522f25b48d0f4b4776a0bc143ce09065017a44f2e734
@@ -0,0 +1,3 @@
1
+ %w(borderline table).each do |filename|
2
+ require_relative "painless-table/#{filename}"
3
+ end
@@ -0,0 +1,14 @@
1
+ module Painless
2
+ class Table
3
+ class Borderline
4
+ def initialize size
5
+ @size = size
6
+ end
7
+
8
+ def to_s
9
+ line = '-' * @size
10
+ line + "\n"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,93 @@
1
+ require 'active_support/inflector'
2
+
3
+ module Painless
4
+ class Table
5
+ attr_reader :heading, :objects, :separate_rows, :cell_sizes
6
+
7
+ def initialize parameters={}
8
+ @heading = parameters.fetch :heading, []
9
+ @objects = parameters.fetch :objects, []
10
+ end
11
+
12
+ def to_s
13
+ draw_heading + draw_body
14
+ end
15
+
16
+ private
17
+
18
+ def get_cell_widths
19
+ widths = @heading.map { |cell| cell.size }
20
+
21
+ get_table_values.each do |row|
22
+ row.each_with_index do |cell, column|
23
+ cell_size = cell.to_s.size
24
+ widths[column] = cell_size if widths[column] < cell_size
25
+ end
26
+ end
27
+
28
+ widths.map { |width| width + 2 }
29
+ end
30
+
31
+ def get_heading_titles
32
+ @heading.map { |cell| cell.to_s.humanize }
33
+ end
34
+
35
+ def get_table_values
36
+ rows = []
37
+
38
+ @objects.each do |object|
39
+ object_values = []
40
+
41
+ @heading.each do |attribute|
42
+ if object.methods.include? attribute.to_sym
43
+ object_values.push object.send(attribute.to_sym).to_s
44
+ elsif object.kind_of? Hash
45
+ object_values.push object[attribute.to_sym].to_s
46
+ else
47
+ object_values.push '-'
48
+ end
49
+ end
50
+
51
+ rows.push object_values
52
+ end
53
+
54
+ rows
55
+ end
56
+
57
+ def draw_borderline
58
+ border_length = get_cell_widths.reduce( :+ ) + get_cell_widths.count + 1
59
+ borderline = Borderline.new border_length
60
+ borderline.to_s
61
+ end
62
+
63
+ def draw_heading
64
+ heading = '|'
65
+
66
+ get_heading_titles.each_with_index do |title, column|
67
+ column_title = title.center(get_cell_widths[column]).concat '|'
68
+ heading.concat column_title
69
+ end
70
+ heading.concat "\n"
71
+
72
+ draw_borderline + heading + draw_borderline
73
+ end
74
+
75
+ def draw_body
76
+ body = ''
77
+
78
+ get_table_values.each do |row_with_values|
79
+ row = '|'
80
+
81
+ row_with_values.each_with_index do |value, column|
82
+ cell = ' ' + value.ljust(get_cell_widths[column]-1) + '|'
83
+ row.concat cell
84
+ end
85
+ row.concat "\n"
86
+
87
+ body.concat row
88
+ end
89
+
90
+ body + draw_borderline
91
+ end
92
+ end
93
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: painless-table
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dmytro Shevchuk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Library for data visualization in the terminal.
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/painless-table.rb
20
+ - lib/painless-table/borderline.rb
21
+ - lib/painless-table/table.rb
22
+ homepage:
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.6.8
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Painless Table for visualization of your data.
46
+ test_files: []