table_format 0.0.5 → 0.0.10

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cc7a2457aeea59874ff842029a0ddb07612b2ea9d4b933c758ad65ee5e6898cf
4
- data.tar.gz: 3cd187d8d8b08a9a1be678a1cb0c00f4f1fca434655037cbd4a1c354af790e2c
3
+ metadata.gz: 8155545d48fb554da7c2324bf13749019a31d814f0b56b8b90a0c11424b2ad35
4
+ data.tar.gz: 8073b769b478b30f3f87e494fad61a6f940f6cecbf6d8b006b23dbaa65626647
5
5
  SHA512:
6
- metadata.gz: b73b71faba2c2ab0725d46dcfc7a1a199d4c88d5ed400332334e53bf0dd89f9862426fbbba631703ca3ba29006b76c129ada2352b6f032559af1fdcc640151d1
7
- data.tar.gz: cdfc9c695aa40bdeadba4fb29674fd3c5f3fc9d3ddc41c22462d738b15eac59b0cd1f3e3c41ec37818df6d3ce453929cd1c13eecbf4d1c17f31037156c450a1b
6
+ metadata.gz: 1cbf7246e86137adcf7ce79622ff1c0108a65a72f72abd0ab14f345f74720940a417f01787b09f4f3b459c62dd20a1521b4bc2b2e7d05d5b6fb92f56afd2de06
7
+ data.tar.gz: a52c220de33351f05c0d71196293cc83dcda2062e10f13d808d2ef595aa4de22ca7877d851a27cd33bc258e381b2403d2852ccdc0a8d84b30304bc9aacc9f3ed
data/.travis.yml CHANGED
@@ -1,3 +1,3 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.5
3
+ - 2.6
data/README.org CHANGED
@@ -19,6 +19,32 @@ tp Object.constants.grep(/RUBY_/).map { |e| [e, Object.const_get(e)] }.to_h
19
19
  # >> | RUBY_ENGINE_VERSION | 2.5.0 |
20
20
  # >> | RUBY_COPYRIGHT | ruby - Copyright (C) 1993-2017 Yukihiro Matsumoto |
21
21
  # >> |---------------------+------------------------------------------------------------|
22
+ #+END_SRC
23
+
24
+ In the case of Rails
25
+
26
+ #+BEGIN_SRC ruby
27
+ class ApplicationController < ActionController::Base
28
+ if Rails.env.development?
29
+ before_action do
30
+ logger.debug params.to_unsafe_h.to_t(truncate: 40)
31
+ end
32
+ end
33
+ end
34
+ #+END_SRC
35
+
36
+ #+BEGIN_SRC shell
37
+ % cat log/development.log
38
+ Started POST "/api/xy_master/time_records" for ::1 at 2021-11-18 13:52:25 +0900
39
+ Processing by Api::XyMaster::TimeRecordsController#create as JSON
40
+ Parameters: (snip)
41
+ |-------------+---------------------------------------------|
42
+ | scope_key | scope_today |
43
+ | time_record | {"rule_key"=>"rule100t", "spent_sec"=>0,... |
44
+ | format | json |
45
+ | controller | api/xy_master/time_records |
46
+ | action | create |
47
+ |-------------+---------------------------------------------|
22
48
  #+END_SRC
23
49
 
24
50
  ** Installation
@@ -19,6 +19,7 @@ tp TableFormat.default_options
19
19
  # >> | intersection_both | | |
20
20
  # >> | horizon | - |
21
21
  # >> | padding | |
22
+ # >> | truncate | 256 |
22
23
  # >> | in_code | UTF-8 |
23
24
  # >> |-------------------+-------|
24
25
  # >> +-------------------+-------+
@@ -30,5 +31,6 @@ tp TableFormat.default_options
30
31
  # >> | intersection_both | + |
31
32
  # >> | horizon | - |
32
33
  # >> | padding | |
34
+ # >> | truncate | 256 |
33
35
  # >> | in_code | UTF-8 |
34
36
  # >> +-------------------+-------+
@@ -31,12 +31,10 @@ puts
31
31
  # set global options
32
32
  TableFormat.default_options.update(markdown: true)
33
33
  tp array
34
- # >> |----+-------|
35
34
  # >> | id | name |
36
- # >> |----+-------|
35
+ # >> |----|-------|
37
36
  # >> | 1 | alice |
38
37
  # >> | 2 | bob |
39
- # >> |----+-------|
40
38
  # >>
41
39
  # >> | id | name |
42
40
  # >> |----|-------|
@@ -0,0 +1,61 @@
1
+ $LOAD_PATH << '../lib'
2
+ require 'table_format'
3
+ require 'ostruct'
4
+
5
+ class C1
6
+ extend Enumerable
7
+ def self.each(&block)
8
+ [:a, :b].each(&block)
9
+ end
10
+ end
11
+
12
+ class C2
13
+ extend Enumerable
14
+ def self.each(&block)
15
+ [[:a, :b]].each(&block)
16
+ end
17
+ end
18
+
19
+ class C3
20
+ extend Enumerable
21
+ def self.each(&block)
22
+ [
23
+ {id: 1},
24
+ {id: 2},
25
+ ].each(&block)
26
+ end
27
+ end
28
+
29
+ class C4
30
+ extend Enumerable
31
+ def self.each(&block)
32
+ [
33
+ OpenStruct.new(attributes: {id: 1}),
34
+ OpenStruct.new(attributes: {id: 2}),
35
+ ].each(&block)
36
+ end
37
+ end
38
+
39
+ OpenStruct.new(attributes: {id: 1}).respond_to?(:attributes) # => true
40
+
41
+ tp C1
42
+ tp C2
43
+ tp C3
44
+ tp C4
45
+ # >> |---|
46
+ # >> | a |
47
+ # >> | b |
48
+ # >> |---|
49
+ # >> |----------|
50
+ # >> | [:a, :b] |
51
+ # >> |----------|
52
+ # >> |----|
53
+ # >> | id |
54
+ # >> |----|
55
+ # >> | 1 |
56
+ # >> | 2 |
57
+ # >> |----|
58
+ # >> |-----------------------------------|
59
+ # >> | #<OpenStruct attributes={:id=>1}> |
60
+ # >> | #<OpenStruct attributes={:id=>2}> |
61
+ # >> |-----------------------------------|
@@ -3,32 +3,38 @@ require 'active_support/core_ext/kernel/concern'
3
3
  module TableFormat
4
4
  concern :ActiveRecord do
5
5
  class_methods do
6
- def to_t(**options)
6
+ def to_t(options = {})
7
7
  TableFormat.generate(all.collect(&:attributes), options)
8
8
  end
9
9
  end
10
10
 
11
- def to_t(**options)
11
+ def to_t(options = {})
12
12
  TableFormat.generate(attributes, options)
13
13
  end
14
14
  end
15
15
 
16
16
  concern :ActiveRecordResult do
17
- def to_t(**options)
17
+ def to_t(options = {})
18
18
  TableFormat.generate(collect(&:to_h), options)
19
19
  end
20
20
  end
21
21
  end
22
22
 
23
23
  Object.class_eval do
24
- def to_t(**options)
24
+ def to_t(options = {})
25
25
  case
26
- when respond_to?(:to_h)
27
- to_h.to_t(options)
28
26
  when respond_to?(:to_a)
27
+ if false
28
+ if to_a.first.respond_to?(:attributes)
29
+ return to_a.collect(&:attributes).to_t(options)
30
+ end
31
+ end
32
+
29
33
  to_a.to_t(options)
34
+ when respond_to?(:to_h)
35
+ to_h.to_t(options)
30
36
  else
31
- TableFormat.generate([{self.class.name => self}], {header: false}.merge(options))
37
+ TableFormat.generate([{self.class.name => self}], **{header: false}.merge(options))
32
38
  end
33
39
  end
34
40
  end
@@ -36,32 +42,32 @@ end
36
42
  Kernel.class_eval do
37
43
  private
38
44
 
39
- def tp(object, **options)
45
+ def tp(object, options = {})
40
46
  object.tap do
41
47
  table_format(object, options).display
42
48
  end
43
49
  end
44
50
 
45
- def table_format(object, **options)
51
+ def table_format(object, options = {})
46
52
  object.to_t(options)
47
53
  end
48
54
  end
49
55
 
50
56
  Array.class_eval do
51
- def to_t(**options)
57
+ def to_t(options = {})
52
58
  TableFormat.generate(self, options)
53
59
  end
54
60
  end
55
61
 
56
62
  Hash.class_eval do
57
- def to_t(**options)
63
+ def to_t(options = {})
58
64
  TableFormat.generate(self, options)
59
65
  end
60
66
  end
61
67
 
62
68
  [Symbol, String, Numeric].each do |klass|
63
69
  klass.class_eval do
64
- def to_t(**options)
70
+ def to_t(options = {})
65
71
  TableFormat.generate([{self.class.name => self}], {header: false}.merge(options))
66
72
  end
67
73
  end
@@ -19,12 +19,12 @@ module TableFormat
19
19
  }
20
20
  end
21
21
 
22
- def self.generate(*args, &block)
23
- Generator.new(*args, &block).generate
22
+ def self.generate(rows, options = {})
23
+ Generator.new(rows, options).generate
24
24
  end
25
25
 
26
26
  class Generator
27
- def initialize(rows, **options)
27
+ def initialize(rows, options = {})
28
28
  @options = TableFormat.default_options.merge(options)
29
29
 
30
30
  if @options[:markdown]
@@ -6,7 +6,7 @@ module TableFormat
6
6
  ::ActiveRecord::Result.include TableFormat::ActiveRecordResult
7
7
 
8
8
  ::ActiveRecord::Relation.class_eval do
9
- def to_t(**options)
9
+ def to_t(options = {})
10
10
  TableFormat.generate(to_a.collect(&:attributes), options)
11
11
  end
12
12
  end
@@ -1,3 +1,3 @@
1
1
  module TableFormat
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.10'
3
3
  end
data/lib/table_format.rb CHANGED
@@ -11,7 +11,7 @@ else
11
11
  ::ActiveRecord::Result.include(TableFormat::ActiveRecordResult)
12
12
 
13
13
  ::ActiveRecord::Relation.class_eval do
14
- def to_t(**options)
14
+ def to_t(options = {})
15
15
  TableFormat.generate(to_a.collect(&:attributes), options)
16
16
  end
17
17
  end
@@ -0,0 +1,69 @@
1
+ require_relative 'spec_helper'
2
+ require "ostruct"
3
+
4
+ describe TableFormat do
5
+ class C1
6
+ extend Enumerable
7
+ def self.each(&block)
8
+ [:a, :b].each(&block)
9
+ end
10
+ end
11
+
12
+ class C2
13
+ extend Enumerable
14
+ def self.each(&block)
15
+ [[:a, :b]].each(&block)
16
+ end
17
+ end
18
+
19
+ class C3
20
+ extend Enumerable
21
+ def self.each(&block)
22
+ [
23
+ {id: 1},
24
+ {id: 2},
25
+ ].each(&block)
26
+ end
27
+ end
28
+
29
+ class C4
30
+ extend Enumerable
31
+ def self.each(&block)
32
+ [
33
+ OpenStruct.new(attributes: {id: 1}),
34
+ OpenStruct.new(attributes: {id: 2}),
35
+ ].each(&block)
36
+ end
37
+ end
38
+
39
+ it do
40
+ C1.to_t.should == <<~EOT
41
+ |---|
42
+ | a |
43
+ | b |
44
+ |---|
45
+ EOT
46
+
47
+ C2.to_t.should == <<~EOT
48
+ |----------|
49
+ | [:a, :b] |
50
+ |----------|
51
+ EOT
52
+
53
+ C3.to_t.should == <<~EOT
54
+ |----|
55
+ | id |
56
+ |----|
57
+ | 1 |
58
+ | 2 |
59
+ |----|
60
+ EOT
61
+
62
+ C4.to_t.should == <<~EOT
63
+ |-----------------------------------|
64
+ | #<OpenStruct attributes={:id=>1}> |
65
+ | #<OpenStruct attributes={:id=>2}> |
66
+ |-----------------------------------|
67
+ EOT
68
+ end
69
+ end
data/table_format.gemspec CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.add_dependency 'activesupport'
23
23
 
24
- spec.add_development_dependency 'bundler', '~> 1.3'
24
+ spec.add_development_dependency 'bundler'
25
25
  spec.add_development_dependency 'rake'
26
26
  spec.add_development_dependency 'rspec'
27
27
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table_format
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - akicho8
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-30 00:00:00.000000000 Z
11
+ date: 2021-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '1.3'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '1.3'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -113,6 +113,7 @@ files:
113
113
  - examples/0150_line_break.rb
114
114
  - examples/0160_truncate.rb
115
115
  - examples/0170_emoji.rb
116
+ - examples/0180_object_spec.rb
116
117
  - lib/table_format.rb
117
118
  - lib/table_format/core_ext.rb
118
119
  - lib/table_format/generator.rb
@@ -120,13 +121,14 @@ files:
120
121
  - lib/table_format/version.rb
121
122
  - spec/active_record_spec.rb
122
123
  - spec/core_ext_spec.rb
124
+ - spec/object_spec.rb
123
125
  - spec/spec_helper.rb
124
126
  - spec/table_format_spec.rb
125
127
  - table_format.gemspec
126
128
  homepage: https://github.com/akicho8/table_format
127
129
  licenses: []
128
130
  metadata: {}
129
- post_install_message:
131
+ post_install_message:
130
132
  rdoc_options:
131
133
  - "--line-numbers"
132
134
  - "--inline-source"
@@ -146,12 +148,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
148
  - !ruby/object:Gem::Version
147
149
  version: '0'
148
150
  requirements: []
149
- rubygems_version: 3.0.3
150
- signing_key:
151
+ rubygems_version: 3.2.31
152
+ signing_key:
151
153
  specification_version: 4
152
154
  summary: TableFormat shows text table like emacs org-table for easy reading.
153
155
  test_files:
154
156
  - spec/active_record_spec.rb
155
157
  - spec/core_ext_spec.rb
158
+ - spec/object_spec.rb
156
159
  - spec/spec_helper.rb
157
160
  - spec/table_format_spec.rb