colorful_tables 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,4 +18,6 @@ Gem::Specification.new do |s|
18
18
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
+
22
+ s.add_dependency('colorize')
21
23
  end
@@ -1,3 +1,8 @@
1
1
  module ColorfulTables
2
- # Your code goes here...
3
- end
2
+ require 'colorize'
3
+ require 'object'
4
+ require 'enumerable'
5
+ require 'errors'
6
+ require 'table'
7
+ require 'column'
8
+ end
@@ -1,3 +1,3 @@
1
1
  module ColorfulTables
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/column.rb ADDED
@@ -0,0 +1,26 @@
1
+ module ColorfulTables
2
+
3
+ class Column
4
+
5
+ attr_accessor :header, :data, :options
6
+
7
+ attr_reader :width
8
+
9
+ def initialize(header, data, options = {})
10
+ @header = header
11
+ @data = data
12
+ @options = options
13
+ @width ||= calculate_width
14
+ end
15
+
16
+ private
17
+
18
+ def calculate_width
19
+ # We have to add the header in the calculation
20
+ data_and_header = self.data + [self.header]
21
+ data_and_header.map { |c| c.to_s.length }.max
22
+ end
23
+
24
+ end
25
+
26
+ end
data/lib/enumerable.rb ADDED
@@ -0,0 +1,6 @@
1
+ module Enumerable
2
+ def inject_with_index(injected)
3
+ each_with_index{ |obj, index| injected = yield(injected, obj, index) }
4
+ injected
5
+ end
6
+ end
data/lib/errors.rb ADDED
@@ -0,0 +1,3 @@
1
+ module ColorfulTables
2
+ class RuntimeError < ::RuntimeError; end
3
+ end
data/lib/object.rb ADDED
@@ -0,0 +1,14 @@
1
+ class Object
2
+
3
+ # From https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/object/try.rb
4
+ def try(*a, &b)
5
+ if a.empty? && block_given?
6
+ yield self
7
+ elsif !a.empty? && !respond_to?(a.first)
8
+ nil
9
+ else
10
+ __send__(*a, &b)
11
+ end
12
+ end
13
+
14
+ end
data/lib/table.rb ADDED
@@ -0,0 +1,51 @@
1
+ module ColorfulTables
2
+
3
+ class Table
4
+
5
+ attr_accessor :data, :options
6
+
7
+ attr_reader :columns_width
8
+
9
+ def initialize(data, options = {})
10
+ raise RuntimeError, "data must be an array of columns" unless is_an_array_of_columns?(data)
11
+ @data = data
12
+ @options = options
13
+ @columns_width ||= calculate_columns_width
14
+ end
15
+
16
+ def to_s
17
+ t = header
18
+ self.data.map(&:data).transpose.each do |row|
19
+ t += format(row)
20
+ end
21
+ t += separator
22
+ t
23
+ end
24
+
25
+ #private
26
+
27
+ def is_an_array_of_columns?(data)
28
+ array_of_booleans = data.try(:map) { |i| i.is_a? Column }
29
+ array_of_booleans.nil? ? false : !(array_of_booleans.include? false)
30
+ end
31
+
32
+ def calculate_columns_width
33
+ @data.map(&:width)
34
+ end
35
+
36
+ def separator
37
+ # + 2 is because we have to add an space at the begining and another at the end inside a cell
38
+ self.data.inject('+') { |s, column| s += (('-' * (column.width + 2)) + '+') } + "\n"
39
+ end
40
+
41
+ def header
42
+ separator + format(@data.map(&:header)) + separator
43
+ end
44
+
45
+ def format(row_elements)
46
+ row_elements.inject_with_index('|') { |s, element, index| s += " #{element.to_s.rjust(@columns_width[index])} |" } + "\n"
47
+ end
48
+
49
+ end
50
+
51
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: colorful_tables
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 0
9
8
  - 1
10
- version: 0.0.1
9
+ - 0
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Javier Vidal
@@ -15,10 +15,23 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-06 00:00:00 +02:00
18
+ date: 2011-10-09 00:00:00 +02:00
19
19
  default_executable:
20
- dependencies: []
21
-
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: colorize
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
22
35
  description: Print colorful tables
23
36
  email:
24
37
  - zanaguara@gmail.com
@@ -35,6 +48,11 @@ files:
35
48
  - colorful_tables.gemspec
36
49
  - lib/colorful_tables.rb
37
50
  - lib/colorful_tables/version.rb
51
+ - lib/column.rb
52
+ - lib/enumerable.rb
53
+ - lib/errors.rb
54
+ - lib/object.rb
55
+ - lib/table.rb
38
56
  has_rdoc: true
39
57
  homepage: ""
40
58
  licenses: []