org_tp 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +3 -0
- data/Gemfile +4 -0
- data/README.org +101 -0
- data/Rakefile +6 -0
- data/examples/0100_example.rb +51 -0
- data/examples/0110_active_record.rb +37 -0
- data/examples/0120_default_options.rb +24 -0
- data/lib/org_tp/core_ext.rb +47 -0
- data/lib/org_tp/org_tp.rb +183 -0
- data/lib/org_tp/railtie.rb +13 -0
- data/lib/org_tp/version.rb +3 -0
- data/lib/org_tp.rb +28 -0
- data/org_tp.gemspec +30 -0
- data/spec/active_record_spec.rb +38 -0
- data/spec/org_tp_spec.rb +99 -0
- data/spec/spec_helper.rb +8 -0
- metadata +150 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8a83f8237762908daa19602bd9081127d14400a6
|
4
|
+
data.tar.gz: cce28c651cbbacf2efaa215209135bc1e963337b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 91391f554d786d9138edb2226bf8eec232c894cc109f854f658b96b9a585a0df75bdf114414bcfbcf8c0f43881725571dc1e842149a82fe3cea603b1652e353f
|
7
|
+
data.tar.gz: cb22bf2cbc811a43cbb1a19f23d728060443621139faecca02c30fc109a83dcce3502a3d7b2c8526343f623bffa215e866c2f47727381ac2881fe47be53bbd9e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.org
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
* OrgTp
|
2
|
+
|
3
|
+
OrgTp shows ascii table like emacs org-table for easy reading.
|
4
|
+
|
5
|
+
** Install
|
6
|
+
|
7
|
+
#+BEGIN_SRC shell
|
8
|
+
gem install org_tp
|
9
|
+
#+END_SRC
|
10
|
+
|
11
|
+
** Examples
|
12
|
+
|
13
|
+
*** Hash of Array
|
14
|
+
|
15
|
+
#+BEGIN_SRC ruby
|
16
|
+
tp [{id: 1, name: "alice"}, {id: 2, name: "bob"}]
|
17
|
+
# >> |----+-------|
|
18
|
+
# >> | id | name |
|
19
|
+
# >> |----+-------|
|
20
|
+
# >> | 1 | alice |
|
21
|
+
# >> | 2 | bob |
|
22
|
+
# >> |----+-------|
|
23
|
+
#+END_SRC
|
24
|
+
|
25
|
+
*** Hash
|
26
|
+
|
27
|
+
#+BEGIN_SRC ruby
|
28
|
+
tp({id: 1, name: "alice"})
|
29
|
+
# >> |------+-------|
|
30
|
+
# >> | id | 1 |
|
31
|
+
# >> | name | alice |
|
32
|
+
# >> |------+-------|
|
33
|
+
#+END_SRC
|
34
|
+
|
35
|
+
*** Integer, String, Symbol
|
36
|
+
|
37
|
+
#+BEGIN_SRC ruby
|
38
|
+
tp 1
|
39
|
+
# >> |---|
|
40
|
+
# >> | 1 |
|
41
|
+
# >> |---|
|
42
|
+
#+END_SRC
|
43
|
+
|
44
|
+
#+BEGIN_SRC ruby
|
45
|
+
tp "foo"
|
46
|
+
# >> |-----|
|
47
|
+
# >> | foo |
|
48
|
+
# >> |-----|
|
49
|
+
#+END_SRC
|
50
|
+
|
51
|
+
#+BEGIN_SRC ruby
|
52
|
+
tp :foo
|
53
|
+
# >> |-----|
|
54
|
+
# >> | foo |
|
55
|
+
# >> |-----|
|
56
|
+
#+END_SRC
|
57
|
+
|
58
|
+
*** ActiveRecord
|
59
|
+
|
60
|
+
#+BEGIN_SRC ruby
|
61
|
+
["alice", "bob"].each {|e| User.create!(name: e) }
|
62
|
+
#+END_SRC
|
63
|
+
|
64
|
+
#+BEGIN_SRC ruby
|
65
|
+
tp User
|
66
|
+
# >> |----+-------|
|
67
|
+
# >> | id | name |
|
68
|
+
# >> |----+-------|
|
69
|
+
# >> | 1 | alice |
|
70
|
+
# >> | 2 | bob |
|
71
|
+
# >> |----+-------|
|
72
|
+
#+END_SRC
|
73
|
+
|
74
|
+
#+BEGIN_SRC ruby
|
75
|
+
tp User.limit(1)
|
76
|
+
# >> |----+-------|
|
77
|
+
# >> | id | name |
|
78
|
+
# >> |----+-------|
|
79
|
+
# >> | 1 | alice |
|
80
|
+
# >> |----+-------|
|
81
|
+
#+END_SRC
|
82
|
+
|
83
|
+
#+BEGIN_SRC ruby
|
84
|
+
tp User.first
|
85
|
+
# >> |------+-------|
|
86
|
+
# >> | id | 1 |
|
87
|
+
# >> | name | alice |
|
88
|
+
# >> |------+-------|
|
89
|
+
#+END_SRC
|
90
|
+
|
91
|
+
*** How to table as string
|
92
|
+
|
93
|
+
#+BEGIN_SRC ruby
|
94
|
+
puts [{id: 1, name: "alice"}, {id: 2, name: "bob"}].to_t
|
95
|
+
# >> |----+-------|
|
96
|
+
# >> | id | name |
|
97
|
+
# >> |----+-------|
|
98
|
+
# >> | 1 | alice |
|
99
|
+
# >> | 2 | bob |
|
100
|
+
# >> |----+-------|
|
101
|
+
#+END_SRC
|
data/Rakefile
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
$LOAD_PATH << "../lib"
|
2
|
+
require "org_tp"
|
3
|
+
|
4
|
+
tp 1
|
5
|
+
tp "foo"
|
6
|
+
tp :foo
|
7
|
+
tp [:a, :b]
|
8
|
+
tp({:id => 1, :name => "alice"})
|
9
|
+
tp [{:id => 1, :name => "alice"}, {:id => 2, :name => "bob"}]
|
10
|
+
puts [{:id => 1, :name => "alice"}, {:id => 2, :name => "bob"}].to_t
|
11
|
+
puts [{"a" => ["a"]}].to_t
|
12
|
+
puts [{"a" => {"a" => 1}}].to_t
|
13
|
+
# >> |---|
|
14
|
+
# >> | 1 |
|
15
|
+
# >> |---|
|
16
|
+
# >> |-----|
|
17
|
+
# >> | foo |
|
18
|
+
# >> |-----|
|
19
|
+
# >> |-----|
|
20
|
+
# >> | foo |
|
21
|
+
# >> |-----|
|
22
|
+
# >> |---|
|
23
|
+
# >> | a |
|
24
|
+
# >> | b |
|
25
|
+
# >> |---|
|
26
|
+
# >> |------+-------|
|
27
|
+
# >> | id | 1 |
|
28
|
+
# >> | name | alice |
|
29
|
+
# >> |------+-------|
|
30
|
+
# >> |----+-------|
|
31
|
+
# >> | id | name |
|
32
|
+
# >> |----+-------|
|
33
|
+
# >> | 1 | alice |
|
34
|
+
# >> | 2 | bob |
|
35
|
+
# >> |----+-------|
|
36
|
+
# >> |----+-------|
|
37
|
+
# >> | id | name |
|
38
|
+
# >> |----+-------|
|
39
|
+
# >> | 1 | alice |
|
40
|
+
# >> | 2 | bob |
|
41
|
+
# >> |----+-------|
|
42
|
+
# >> |-------|
|
43
|
+
# >> | a |
|
44
|
+
# >> |-------|
|
45
|
+
# >> | ["a"] |
|
46
|
+
# >> |-------|
|
47
|
+
# >> |----------|
|
48
|
+
# >> | a |
|
49
|
+
# >> |----------|
|
50
|
+
# >> | {"a"=>1} |
|
51
|
+
# >> |----------|
|
@@ -0,0 +1,37 @@
|
|
1
|
+
$LOAD_PATH << "../lib"
|
2
|
+
require "active_record"
|
3
|
+
require "org_tp"
|
4
|
+
|
5
|
+
ActiveRecord::Base.include(OrgTp::ActiveRecord)
|
6
|
+
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
|
7
|
+
ActiveRecord::Migration.verbose = false
|
8
|
+
ActiveRecord::Schema.define do
|
9
|
+
create_table :users do |t|
|
10
|
+
t.string :name
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class User < ActiveRecord::Base
|
15
|
+
end
|
16
|
+
|
17
|
+
["alice", "bob", "carol"].each { |e| User.create!(name: e) }
|
18
|
+
|
19
|
+
tp User
|
20
|
+
tp User.first
|
21
|
+
tp User.limit(1)
|
22
|
+
# >> |----+-------|
|
23
|
+
# >> | id | name |
|
24
|
+
# >> |----+-------|
|
25
|
+
# >> | 1 | alice |
|
26
|
+
# >> | 2 | bob |
|
27
|
+
# >> | 3 | carol |
|
28
|
+
# >> |----+-------|
|
29
|
+
# >> |------+-------|
|
30
|
+
# >> | id | 1 |
|
31
|
+
# >> | name | alice |
|
32
|
+
# >> |------+-------|
|
33
|
+
# >> |----+-------|
|
34
|
+
# >> | id | name |
|
35
|
+
# >> |----+-------|
|
36
|
+
# >> | 1 | alice |
|
37
|
+
# >> |----+-------|
|
@@ -0,0 +1,24 @@
|
|
1
|
+
$LOAD_PATH << "../lib"
|
2
|
+
require "org_tp"
|
3
|
+
|
4
|
+
tp OrgTp::Generator.default_options
|
5
|
+
OrgTp::Generator.default_options.update(intersection_both: "+")
|
6
|
+
tp OrgTp::Generator.default_options
|
7
|
+
# >> |-------------------+-------|
|
8
|
+
# >> | header | |
|
9
|
+
# >> | vertical | | |
|
10
|
+
# >> | intersection | + |
|
11
|
+
# >> | intersection_both | | |
|
12
|
+
# >> | horizon | - |
|
13
|
+
# >> | padding | |
|
14
|
+
# >> | in_code | UTF-8 |
|
15
|
+
# >> |-------------------+-------|
|
16
|
+
# >> +-------------------+-------+
|
17
|
+
# >> | header | |
|
18
|
+
# >> | vertical | | |
|
19
|
+
# >> | intersection | + |
|
20
|
+
# >> | intersection_both | + |
|
21
|
+
# >> | horizon | - |
|
22
|
+
# >> | padding | |
|
23
|
+
# >> | in_code | UTF-8 |
|
24
|
+
# >> +-------------------+-------+
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'active_support/core_ext/kernel/concern'
|
2
|
+
|
3
|
+
module OrgTp
|
4
|
+
concern :ActiveRecord do
|
5
|
+
class_methods do
|
6
|
+
def to_t(**options)
|
7
|
+
OrgTp.generate(all.collect(&:attributes), options)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_t(**options)
|
12
|
+
OrgTp.generate(attributes, options)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
Kernel.class_eval do
|
18
|
+
private
|
19
|
+
|
20
|
+
def tp(object, **options)
|
21
|
+
if object.respond_to?(:to_t)
|
22
|
+
object.to_t(options).display
|
23
|
+
else
|
24
|
+
OrgTp.generate([{object.class.name => object}], {header: false}.merge(options)).display
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
Array.class_eval do
|
30
|
+
def to_t(**options)
|
31
|
+
OrgTp.generate(self, options)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
Hash.class_eval do
|
36
|
+
def to_t(**options)
|
37
|
+
OrgTp.generate(self, options)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
[Symbol, String, Numeric].each do |klass|
|
42
|
+
klass.class_eval do
|
43
|
+
def to_t(**options)
|
44
|
+
OrgTp.generate([{self.class.name => self}], {header: false}.merge(options))
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,183 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'active_support/core_ext/string' # for blank?
|
3
|
+
require 'active_support/core_ext/class/attribute' # for class_attribute
|
4
|
+
require 'kconv'
|
5
|
+
|
6
|
+
module OrgTp
|
7
|
+
def self.generate(*args, &block)
|
8
|
+
Generator.new(*args, &block).generate
|
9
|
+
end
|
10
|
+
|
11
|
+
class Generator
|
12
|
+
class_attribute :default_options
|
13
|
+
self.default_options = {
|
14
|
+
header: nil,
|
15
|
+
vertical: '|',
|
16
|
+
intersection: '+',
|
17
|
+
intersection_both: '|',
|
18
|
+
horizon: '-',
|
19
|
+
padding: ' ',
|
20
|
+
in_code: Kconv::UTF8,
|
21
|
+
}
|
22
|
+
|
23
|
+
def initialize(rows, **options)
|
24
|
+
@options = default_options.merge(options)
|
25
|
+
@rows = rows
|
26
|
+
@column_names = nil
|
27
|
+
end
|
28
|
+
|
29
|
+
def generate
|
30
|
+
if @rows.blank?
|
31
|
+
return ''
|
32
|
+
end
|
33
|
+
|
34
|
+
pre_processes.each do |e|
|
35
|
+
if e[:_case][@rows]
|
36
|
+
@rows = e[:process][@rows]
|
37
|
+
if @options[:header].nil?
|
38
|
+
@options[:header] = e[:header]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
table_rows_build
|
44
|
+
|
45
|
+
out = []
|
46
|
+
out << separater
|
47
|
+
if @column_names
|
48
|
+
out << header
|
49
|
+
out << separater
|
50
|
+
end
|
51
|
+
out << body
|
52
|
+
out << separater
|
53
|
+
out.flatten * "\n" + "\n"
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def table_rows_build
|
59
|
+
columns = @rows.inject([]) { |a, e| a | e.keys }
|
60
|
+
if @options[:header]
|
61
|
+
@column_names = columns
|
62
|
+
end
|
63
|
+
@table_rows = @rows.collect { |e| e.values_at(*columns) }
|
64
|
+
end
|
65
|
+
|
66
|
+
def column_widths
|
67
|
+
@column_widths ||= ([@column_names] + @table_rows).compact.transpose.collect do |vertical_values|
|
68
|
+
vertical_values.collect { |e| str_width(e) }.max
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def separater
|
73
|
+
@separater ||= _separater
|
74
|
+
end
|
75
|
+
|
76
|
+
def _separater
|
77
|
+
s = column_widths.collect { |e|
|
78
|
+
@options[:horizon] * (padding.size + e + padding.size)
|
79
|
+
}
|
80
|
+
s = s.join(@options[:intersection])
|
81
|
+
[@options[:intersection_both], s, @options[:intersection_both]].join
|
82
|
+
end
|
83
|
+
|
84
|
+
def header
|
85
|
+
@header ||= line_str(@column_names)
|
86
|
+
end
|
87
|
+
|
88
|
+
def body
|
89
|
+
@table_rows.collect { |row| line_str(row) }
|
90
|
+
end
|
91
|
+
|
92
|
+
def line_str(row)
|
93
|
+
s = row.collect.with_index {|e, i|
|
94
|
+
padding + just(e, column_widths[i]) + padding
|
95
|
+
}
|
96
|
+
s = s.join(@options[:vertical])
|
97
|
+
[@options[:vertical], s, @options[:vertical]].join
|
98
|
+
end
|
99
|
+
|
100
|
+
def padding
|
101
|
+
@options[:padding]
|
102
|
+
end
|
103
|
+
|
104
|
+
def just(value, max_width)
|
105
|
+
align = Float(value) && :right rescue :left
|
106
|
+
space = ' ' * (max_width - str_width(value))
|
107
|
+
lspace = ''
|
108
|
+
rspace = ''
|
109
|
+
if align == :right
|
110
|
+
lspace = space
|
111
|
+
else
|
112
|
+
rspace = space
|
113
|
+
end
|
114
|
+
# If value is `[]`,
|
115
|
+
# executing to_s, it becomes `[]`.
|
116
|
+
# not executing to_s, it becomes empty string
|
117
|
+
[lspace, value.to_s, rspace].join
|
118
|
+
end
|
119
|
+
|
120
|
+
def str_width(str)
|
121
|
+
str.to_s.kconv(Kconv::EUC, @options[:in_code]).bytesize
|
122
|
+
end
|
123
|
+
|
124
|
+
def pre_processes
|
125
|
+
[
|
126
|
+
{
|
127
|
+
_case: -> e { e.kind_of?(Hash) },
|
128
|
+
header: false,
|
129
|
+
process: -> e {
|
130
|
+
e.collect do |k, v|
|
131
|
+
{'(key)' => k.to_s, '(value)' => v.to_s}
|
132
|
+
end
|
133
|
+
},
|
134
|
+
},
|
135
|
+
{
|
136
|
+
_case: -> e { e.kind_of?(Array) && e.any? { |e| !e.kind_of?(Hash) } },
|
137
|
+
header: false,
|
138
|
+
process: -> e {
|
139
|
+
e.collect do |e|
|
140
|
+
if e.kind_of? Hash
|
141
|
+
e
|
142
|
+
else
|
143
|
+
{'(array_value)' => e}
|
144
|
+
end
|
145
|
+
end
|
146
|
+
},
|
147
|
+
},
|
148
|
+
{
|
149
|
+
_case: -> e { e.kind_of?(Array) && e.all? { |e| e.kind_of?(Hash) } },
|
150
|
+
header: true,
|
151
|
+
process: -> e { e },
|
152
|
+
},
|
153
|
+
]
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
if $0 == __FILE__
|
159
|
+
rows = [
|
160
|
+
{id: 1, name: 'alice', description: '0123456789'},
|
161
|
+
{id: 2, name: 'bob', description: 'あいうえお'},
|
162
|
+
{id: 3, name: 'carol'},
|
163
|
+
]
|
164
|
+
print OrgTp.generate({a: []})
|
165
|
+
print OrgTp.generate([])
|
166
|
+
print OrgTp.generate(rows)
|
167
|
+
print OrgTp.generate({a: 1, b: 2}, header: false)
|
168
|
+
end
|
169
|
+
# >> +---+----+
|
170
|
+
# >> | a | [] |
|
171
|
+
# >> +---+----+
|
172
|
+
# >>
|
173
|
+
# >> +----+-------+-------------+
|
174
|
+
# >> | id | name | description |
|
175
|
+
# >> +----+-------+-------------+
|
176
|
+
# >> | 1 | alice | 0123456789 |
|
177
|
+
# >> | 2 | bob | あいうえお |
|
178
|
+
# >> | 3 | carol | |
|
179
|
+
# >> +----+-------+-------------+
|
180
|
+
# >> +---+---+
|
181
|
+
# >> | a | 1 |
|
182
|
+
# >> | b | 2 |
|
183
|
+
# >> +---+---+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module OrgTp
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
initializer 'org_tp' do
|
4
|
+
ActiveSupport.on_load(:active_record) do
|
5
|
+
include OrgTp::ActiveRecord
|
6
|
+
end
|
7
|
+
|
8
|
+
if defined?(Mongoid::Document)
|
9
|
+
Mongoid::Document.include(OrgTp::ActiveRecord)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/org_tp.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'org_tp/version'
|
2
|
+
require 'org_tp/org_tp'
|
3
|
+
require 'org_tp/core_ext'
|
4
|
+
|
5
|
+
if defined?(Rails)
|
6
|
+
require 'org_tp/railtie'
|
7
|
+
else
|
8
|
+
if defined?(ActiveSupport)
|
9
|
+
ActiveSupport.on_load(:active_record) do
|
10
|
+
include OrgTp::ActiveRecord
|
11
|
+
end
|
12
|
+
end
|
13
|
+
if defined?(Mongoid::Document)
|
14
|
+
Mongoid::Document.include(OrgTp::ActiveRecord)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
if defined?(Tapp)
|
19
|
+
module Tapp::Printer
|
20
|
+
class Tp < Base
|
21
|
+
def print(*args)
|
22
|
+
tp *args
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
register :tp, Tp
|
27
|
+
end
|
28
|
+
end
|
data/org_tp.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'org_tp/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "org_tp"
|
8
|
+
spec.version = OrgTp::VERSION
|
9
|
+
spec.author = "akicho8"
|
10
|
+
spec.email = "akicho8@gmail.com"
|
11
|
+
spec.homepage = "https://github.com/akicho8/org_tp"
|
12
|
+
spec.summary = "OrgTp shows ascii table like emacs org-table for easy reading."
|
13
|
+
spec.description = "OrgTp shows ascii table like emacs org-table for easy reading."
|
14
|
+
spec.platform = Gem::Platform::RUBY
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
spec.rdoc_options = ["--line-numbers", "--inline-source", "--charset=UTF-8", "--diagram", "--image-format=jpg"]
|
21
|
+
|
22
|
+
spec.add_dependency "activesupport"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
|
28
|
+
spec.add_development_dependency "activerecord"
|
29
|
+
spec.add_development_dependency "sqlite3"
|
30
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
require "active_record"
|
3
|
+
|
4
|
+
ActiveRecord::Base.send(:include, OrgTp::ActiveRecord)
|
5
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
6
|
+
ActiveRecord::Migration.verbose = false
|
7
|
+
|
8
|
+
ActiveRecord::Schema.define do
|
9
|
+
create_table :users do |t|
|
10
|
+
t.string :name
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class User < ActiveRecord::Base
|
15
|
+
end
|
16
|
+
|
17
|
+
describe OrgTp::ActiveRecord do
|
18
|
+
before do
|
19
|
+
2.times { |i| User.create!(name: i) }
|
20
|
+
end
|
21
|
+
|
22
|
+
it do
|
23
|
+
User.to_t.should == <<~EOT
|
24
|
+
|----+------|
|
25
|
+
| id | name |
|
26
|
+
|----+------|
|
27
|
+
| 1 | 0 |
|
28
|
+
| 2 | 1 |
|
29
|
+
|----+------|
|
30
|
+
EOT
|
31
|
+
User.first.to_t.should == <<~EOT
|
32
|
+
|------+---|
|
33
|
+
| id | 1 |
|
34
|
+
| name | 0 |
|
35
|
+
|------+---|
|
36
|
+
EOT
|
37
|
+
end
|
38
|
+
end
|
data/spec/org_tp_spec.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
|
3
|
+
describe OrgTp do
|
4
|
+
before do
|
5
|
+
@rows = [
|
6
|
+
{id: 1, name: "alice", description: "0123456789"},
|
7
|
+
{id: 2, name: "bob", description: "あいうえお"},
|
8
|
+
{id: 3, name: "bob"},
|
9
|
+
]
|
10
|
+
end
|
11
|
+
|
12
|
+
it "空の場合" do
|
13
|
+
OrgTp.generate([]).should == ""
|
14
|
+
end
|
15
|
+
|
16
|
+
it "フォーマット指定なし" do
|
17
|
+
OrgTp.generate(@rows).should == <<~EOT
|
18
|
+
|----+-------+-------------|
|
19
|
+
| id | name | description |
|
20
|
+
|----+-------+-------------|
|
21
|
+
| 1 | alice | 0123456789 |
|
22
|
+
| 2 | bob | あいうえお |
|
23
|
+
| 3 | bob | |
|
24
|
+
|----+-------+-------------|
|
25
|
+
EOT
|
26
|
+
end
|
27
|
+
|
28
|
+
it "ヘッダーなし" do
|
29
|
+
OrgTp.generate(@rows, header: false).should == <<~EOT
|
30
|
+
|---+-------+------------|
|
31
|
+
| 1 | alice | 0123456789 |
|
32
|
+
| 2 | bob | あいうえお |
|
33
|
+
| 3 | bob | |
|
34
|
+
|---+-------+------------|
|
35
|
+
EOT
|
36
|
+
end
|
37
|
+
|
38
|
+
it "パディングなし" do
|
39
|
+
OrgTp.generate(@rows, padding: "").should == <<~EOT
|
40
|
+
|--+-----+-----------|
|
41
|
+
|id|name |description|
|
42
|
+
|--+-----+-----------|
|
43
|
+
| 1|alice| 0123456789|
|
44
|
+
| 2|bob |あいうえお |
|
45
|
+
| 3|bob | |
|
46
|
+
|--+-----+-----------|
|
47
|
+
EOT
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "組み込み" do
|
51
|
+
it "ハッシュの配列はいままで通り" do
|
52
|
+
[{a: 1}].to_t.should == <<~EOT
|
53
|
+
|---|
|
54
|
+
| a |
|
55
|
+
|---|
|
56
|
+
| 1 |
|
57
|
+
|---|
|
58
|
+
EOT
|
59
|
+
end
|
60
|
+
|
61
|
+
it "ハッシュのみの場合は縦に表示" do
|
62
|
+
{a: 1}.to_t.should == <<~EOT
|
63
|
+
|---+---|
|
64
|
+
| a | 1 |
|
65
|
+
|---+---|
|
66
|
+
EOT
|
67
|
+
end
|
68
|
+
|
69
|
+
it "文字列の配列は縦に並べて表示" do
|
70
|
+
["a", "b"].to_t.should == <<~EOT
|
71
|
+
|---|
|
72
|
+
| a |
|
73
|
+
| b |
|
74
|
+
|---|
|
75
|
+
EOT
|
76
|
+
end
|
77
|
+
|
78
|
+
it "その他" do
|
79
|
+
1.to_t.should be_present
|
80
|
+
"1".to_t.should be_present
|
81
|
+
Module.new.should be_present
|
82
|
+
{[:a] => []}.to_t.should == <<~EOT
|
83
|
+
|------+----|
|
84
|
+
| [:a] | [] |
|
85
|
+
|------+----|
|
86
|
+
EOT
|
87
|
+
end
|
88
|
+
|
89
|
+
it "ハッシュの配列で値が配列のとき幅がおかしくならない" do
|
90
|
+
OrgTp.generate([{"a" => ["a"]}]).should == <<~EOT
|
91
|
+
|-------|
|
92
|
+
| a |
|
93
|
+
|-------|
|
94
|
+
| ["a"] |
|
95
|
+
|-------|
|
96
|
+
EOT
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: org_tp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- akicho8
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-08-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activerecord
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: sqlite3
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: OrgTp shows ascii table like emacs org-table for easy reading.
|
98
|
+
email: akicho8@gmail.com
|
99
|
+
executables: []
|
100
|
+
extensions: []
|
101
|
+
extra_rdoc_files: []
|
102
|
+
files:
|
103
|
+
- ".gitignore"
|
104
|
+
- Gemfile
|
105
|
+
- README.org
|
106
|
+
- Rakefile
|
107
|
+
- examples/0100_example.rb
|
108
|
+
- examples/0110_active_record.rb
|
109
|
+
- examples/0120_default_options.rb
|
110
|
+
- lib/org_tp.rb
|
111
|
+
- lib/org_tp/core_ext.rb
|
112
|
+
- lib/org_tp/org_tp.rb
|
113
|
+
- lib/org_tp/railtie.rb
|
114
|
+
- lib/org_tp/version.rb
|
115
|
+
- org_tp.gemspec
|
116
|
+
- spec/active_record_spec.rb
|
117
|
+
- spec/org_tp_spec.rb
|
118
|
+
- spec/spec_helper.rb
|
119
|
+
homepage: https://github.com/akicho8/org_tp
|
120
|
+
licenses: []
|
121
|
+
metadata: {}
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options:
|
124
|
+
- "--line-numbers"
|
125
|
+
- "--inline-source"
|
126
|
+
- "--charset=UTF-8"
|
127
|
+
- "--diagram"
|
128
|
+
- "--image-format=jpg"
|
129
|
+
require_paths:
|
130
|
+
- lib
|
131
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: '0'
|
141
|
+
requirements: []
|
142
|
+
rubyforge_project:
|
143
|
+
rubygems_version: 2.6.11
|
144
|
+
signing_key:
|
145
|
+
specification_version: 4
|
146
|
+
summary: OrgTp shows ascii table like emacs org-table for easy reading.
|
147
|
+
test_files:
|
148
|
+
- spec/active_record_spec.rb
|
149
|
+
- spec/org_tp_spec.rb
|
150
|
+
- spec/spec_helper.rb
|