table_formatter_simple 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8969cfafcf688e638aa96f324816e8fb9ae5f414
4
+ data.tar.gz: c47971fcdbef331f3bf12a71610c9cec2d46cba1
5
+ SHA512:
6
+ metadata.gz: 06ef740a0955ce973d541555aeebfd12346ae3c238b4beaba4049111a7117882dd71ed1d89b2d3e35b01d9fbd36d90dc76d1890793e9977bf69b4536ec7ff540
7
+ data.tar.gz: 8f41eb368525aeb6d1f31a40fbdf9212882f244721c1c00fcb510d5a02e7be9c9250c744ed584113fe3f0bb0712dee224dab5a08f08244277f5bbe8dbb578825
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 2.0.0
6
+ matrix:
7
+ allow_failures:
8
+ - rvm: 2.0.0
9
+
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in table_formatter_simple.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rspec'
8
+ gem 'rspec-core'
9
+ gem 'rake'
10
+ end
11
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 SpringMT
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ # TableFormatterSimple [![Build Status](https://travis-ci.org/SpringMT/table_formatter_simple)](https://travis-ci.org/SpringMT/table_formatter_simple)
2
+
3
+ Format a table in Teminal.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'table_formatter_simple'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install table_formatter_simple
18
+
19
+ ## Usage
20
+
21
+ ```
22
+ [{foo: 'aaa', bar: 'bbbb'}, {foo: 'ccc', bar: 'dddd'}]
23
+ ```
24
+
25
+ To convert below.
26
+
27
+ ```
28
+ +-----+------+
29
+ | foo | bar |
30
+ +-----+------+
31
+ | aaa | bbbb |
32
+ | ccc | dddd |
33
+ +-----+------+
34
+ ```
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create new Pull Request
43
+
@@ -0,0 +1,12 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ task :default => :test
4
+
5
+ task :test do
6
+ require 'rspec/core'
7
+ require 'rspec/core/rake_task'
8
+ RSpec::Core::RakeTask.new(:test) do |spec|
9
+ spec.pattern = FileList['spec/**/*_spec.rb']
10
+ end
11
+ end
12
+
@@ -0,0 +1,62 @@
1
+ class TableFormatterSimple
2
+ def initialize
3
+ @space_num = 1
4
+ @padstr = ' '
5
+ end
6
+
7
+ def to_table(data)
8
+ return if data.empty?
9
+
10
+ data2hash = {}
11
+ length_data = {}
12
+ formatted_data = ''
13
+
14
+ data.each do |d|
15
+ d.keys.each do |k|
16
+ data2hash[k] ||= []
17
+ data2hash[k] << d[k]
18
+ end
19
+ end
20
+ data2hash.keys.each do |k|
21
+ length_data[k] = (data2hash[k].max_by { |f| f.to_s.length }).to_s.length + @space_num * 2
22
+ end
23
+
24
+ key_list = data2hash.keys
25
+
26
+ cover = ''
27
+ key_list.each do |k|
28
+ cover << '+'
29
+ cover << '-' * length_data[k]
30
+ end
31
+ cover << "+\n"
32
+ formatted_data << cover
33
+
34
+ index = ''
35
+ key_list.each do |k|
36
+ index << '|'
37
+ index << @padstr * @space_num
38
+ index << k.to_s.ljust(length_data[k] - @space_num, @padstr)
39
+ end
40
+ index << "|\n"
41
+ formatted_data << index
42
+
43
+ formatted_data << cover
44
+
45
+ rows = ''
46
+ data.each do |d|
47
+ row = ''
48
+ key_list.each do |k|
49
+ row << '|'
50
+ row << @padstr * @space_num
51
+ row << d[k].to_s.ljust(length_data[k] - @space_num, @padstr)
52
+ end
53
+ row << "|\n"
54
+ rows << row
55
+ end
56
+ formatted_data << rows
57
+ formatted_data << cover
58
+
59
+ formatted_data
60
+ end
61
+
62
+ end
@@ -0,0 +1,13 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ Bundler.setup(:default, :test)
6
+ Bundler.require(:default, :test)
7
+
8
+ require 'rspec'
9
+
10
+ $TESTING=true
11
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
12
+ require 'table_formatter_simple'
13
+
@@ -0,0 +1,53 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.dirname(__FILE__) + '/spec_helper'
4
+
5
+ describe TableFormatterSimple do
6
+ describe :to_table do
7
+
8
+ context 'valid all' do
9
+ subject { TableFormatterSimple.new.to_table([{foo: 'aaa', bar: 'bbbb'}, {foo: 'ccc', bar: 'dddd'}]) }
10
+ it do
11
+ should eql(<<"TABLE")
12
+ +-----+------+
13
+ | foo | bar |
14
+ +-----+------+
15
+ | aaa | bbbb |
16
+ | ccc | dddd |
17
+ +-----+------+
18
+ TABLE
19
+ end
20
+ end
21
+
22
+ context 'valid partical key' do
23
+ subject { TableFormatterSimple.new.to_table([{foo: 'aaa', bar: 'bbbb'}, {foo: 'ccc', hoge: 'dddd'}]) }
24
+ it do
25
+ should eql(<<"TABLE")
26
+ +-----+------+------+
27
+ | foo | bar | hoge |
28
+ +-----+------+------+
29
+ | aaa | bbbb | |
30
+ | ccc | | dddd |
31
+ +-----+------+------+
32
+ TABLE
33
+ end
34
+ end
35
+
36
+ context 'valid all' do
37
+ subject { TableFormatterSimple.new.to_table([{foo: 111, bar: 2222}, {foo: 333, bar: 444444444444}]) }
38
+ it do
39
+ should eql(<<"TABLE")
40
+ +-----+--------------+
41
+ | foo | bar |
42
+ +-----+--------------+
43
+ | 111 | 2222 |
44
+ | 333 | 444444444444 |
45
+ +-----+--------------+
46
+ TABLE
47
+ end
48
+ end
49
+
50
+ end
51
+ end
52
+
53
+
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "table_formatter_simple"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["SpringMT"]
9
+ spec.email = ["today.is.sky.blue.sky@gmail.com"]
10
+ spec.summary = %q{Format a table in Teminal}
11
+ spec.homepage = "https://github.com/SpringMT/table_formatter_simple"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files`.split($/)
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.3"
20
+ spec.add_development_dependency "rake"
21
+
22
+ spec.description = <<description
23
+ Format a table in Teminal.
24
+ description
25
+ end
26
+
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: table_formatter_simple
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - SpringMT
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: |
42
+ Format a table in Teminal.
43
+ email:
44
+ - today.is.sky.blue.sky@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - .gitignore
50
+ - .travis.yml
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - lib/table_formatter_simple.rb
56
+ - spec/spec_helper.rb
57
+ - spec/table_formatter_simple_spec.rb
58
+ - table_formatter_simple.gemspec
59
+ homepage: https://github.com/SpringMT/table_formatter_simple
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.0.2
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Format a table in Teminal
83
+ test_files:
84
+ - spec/spec_helper.rb
85
+ - spec/table_formatter_simple_spec.rb