tablesmith 0.1.0 → 0.1.2

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
  SHA1:
3
- metadata.gz: 0b2cd809d21c4144fbb28e472f12e4f77510817d
4
- data.tar.gz: 664d3bb41153050b8476ac761e1b4063594c7327
3
+ metadata.gz: 45818202ea467196502cd88dabbba91262f0476d
4
+ data.tar.gz: b6ece78a22c856daee3f1aab6b1eeb13cfe0201f
5
5
  SHA512:
6
- metadata.gz: 9ef5411a7defaefa9dd1246d985a6a063479832836d41a48a1f715aa9922833f049163ccb2e3cf44b5679a1acf212bdfa7bac9c6776eefe6f752de0ecb0367f0
7
- data.tar.gz: 972584036fd2a3a4208a4aa82c06e39686c5cab70c9e47b53594d95d52f9120f5da4ff2d6b98f14bd776ee681534062d77c0fc558d76695b1e7184545afdfd0c
6
+ metadata.gz: 43d69277e17bb8909aa962b96f97a6d02a14bdce203bacd37e4be3d07031fa8a8fa8adec62a58ca2b9ff0fa66a30cd0dba750ee5057c6b13157b264f0bf93ee9
7
+ data.tar.gz: b6345a15062ebcb554494558880bff872fba9f96424bc4ae1ddeb43003d3137da96afbce0ea1210a0cc0e0c1ed4334895bb43a117517c932aaeb51440d867db8
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  .bundle/
2
+ .idea/
2
3
  bin/
3
4
  pkg/
data/Gemfile CHANGED
@@ -1,5 +1,4 @@
1
1
  source 'https://rubygems.org'
2
- source "http://geminabox.lsqa.net"
3
2
 
4
3
  # Specify your gem's dependencies in tablesmith.gemspec
5
4
  gemspec
data/Gemfile.lock CHANGED
@@ -1,12 +1,11 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tablesmith (0.1.0)
4
+ tablesmith (0.1.2)
5
5
  text-table
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
- remote: http://geminabox.lsqa.net/
10
9
  specs:
11
10
  activemodel (3.2.22.5)
12
11
  activesupport (= 3.2.22.5)
@@ -64,4 +63,4 @@ DEPENDENCIES
64
63
  tablesmith!
65
64
 
66
65
  BUNDLED WITH
67
- 1.15.1
66
+ 1.15.3
data/lib/tablesmith.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'tablesmith/batch'
2
2
  require 'tablesmith/active_record_source'
3
+ require 'tablesmith/array_rows_source'
3
4
  require 'tablesmith/hash_rows_source'
4
5
  require 'tablesmith/version'
@@ -0,0 +1,68 @@
1
+ module Tablesmith::ArrayRowsSource
2
+ def text_table
3
+ build_columns if columns.nil?
4
+ super
5
+ end
6
+
7
+ def convert_item_to_hash_row(item)
8
+ item
9
+ end
10
+
11
+ # def flatten_hash_to_row(deep_hash, columns)
12
+ # row = ActiveSupport::OrderedHash.new
13
+ # columns.each do |col_or_hash|
14
+ # value_from_hash(row, deep_hash, col_or_hash)
15
+ # end
16
+ # row
17
+ # end
18
+
19
+ # TODO: no support for deep
20
+ def build_columns
21
+ @columns ||= []
22
+ self.map do |array_row|
23
+ @columns << array_row.map { |item| Tablesmith::Column.new(name: item) }
24
+ end
25
+ @columns.flatten!
26
+ end
27
+
28
+ def create_headers(rows)
29
+ column_names = rows.shift
30
+ grouped_headers(column_names) + [apply_column_aliases(column_names), :separator]
31
+ end
32
+
33
+ # def value_from_hash(row, deep_hash, col_or_hash)
34
+ # case col_or_hash
35
+ # when Tablesmith::Column
36
+ # row[col_or_hash.display_name] = deep_hash[col_or_hash.name]
37
+ # when Hash
38
+ # col_or_hash.each_pair do |sub_hash_key, cols_or_hash|
39
+ # [cols_or_hash].flatten.each do |inner_col_or_hash|
40
+ # value_from_hash(row, deep_hash[sub_hash_key], inner_col_or_hash)
41
+ # end
42
+ # end
43
+ # else
44
+ # nil
45
+ # end
46
+ # rescue => e
47
+ # $stderr.puts "#{e.message}: #{col_or_hash}" if @debug
48
+ # end
49
+
50
+ # def hash_rows_to_text_table(hash_rows)
51
+ # require 'text-table'
52
+ #
53
+ # header_row = hash_rows.first.keys
54
+ # table = []
55
+ # table << header_row
56
+ #
57
+ # hash_rows.each do |hash_row|
58
+ # row = []
59
+ # header_row.each do |header|
60
+ # row << hash_row[header]
61
+ # end
62
+ # table << row
63
+ # end
64
+ #
65
+ # # Array addition from text-table
66
+ # table.to_table(:first_row_is_head => true)
67
+ # end
68
+ end
@@ -33,43 +33,43 @@ module Tablesmith
33
33
 
34
34
  normalize_keys(rows)
35
35
 
36
- rows.map! do |row|
37
- # this sort gives preference to column_order then falls back to alphabetic for leftovers.
38
- # this is handy when columns auto-generate based on hash data.
39
- row.sort do |a, b|
40
- a_col_name, b_col_name = [a.first, b.first]
41
- a_col_index, b_col_index = [column_order.index(a_col_name), column_order.index(b_col_name)]
42
-
43
- if a_col_index.nil? && b_col_index.nil?
44
- a_col_name <=> b_col_name
45
- else
46
- (a_col_index || 999) <=> (b_col_index || 999)
47
- end
48
- end
49
- end
36
+ sort_columns(rows)
50
37
 
51
- rows = create_headers(rows) + (rows.map { |r| r.map(&:last) })
38
+ rows = create_headers(rows) + (rows.map { |row| row_values(row) })
52
39
  rows.to_text_table
53
40
  end
54
41
 
42
+ # override in subclass or mixin
43
+ def row_values(row)
44
+ row
45
+ end
46
+
47
+ # override in subclass or mixin
48
+ def sort_columns(rows)
49
+ end
50
+
55
51
  # override in subclass or mixin
56
52
  def convert_item_to_hash_row(item)
57
53
  item
58
54
  end
59
55
 
56
+ # override in subclass or mixin
57
+ def normalize_keys(rows)
58
+ end
59
+
60
60
  # override in subclass or mixin
61
61
  def column_order
62
62
  []
63
63
  end
64
64
 
65
-
66
65
  # TODO: resolve with column_order
67
66
  def columns
68
67
  @columns
69
68
  end
70
69
 
71
70
  def create_headers(rows)
72
- column_names = rows.first.map(&:first)
71
+ top_row = rows.first
72
+ column_names = top_row.first.is_a?(Array) ? top_row.map(&:first) : top_row
73
73
  grouped_headers(column_names) + [apply_column_aliases(column_names), :separator]
74
74
  end
75
75
 
@@ -100,12 +100,6 @@ module Tablesmith
100
100
  {:value => value, :align => :center}
101
101
  end
102
102
  end
103
-
104
- # not all resulting rows will have data in all columns, so make sure all rows pad out missing columns
105
- def normalize_keys(rows)
106
- all_keys = rows.map { |hash_row| hash_row.keys }.flatten.uniq
107
- rows.map { |hash_row| all_keys.each { |key| hash_row[key] ||= '' } }
108
- end
109
103
  end
110
104
 
111
105
  class Column
@@ -131,14 +125,20 @@ class Array
131
125
  def to_batch
132
126
  b = Tablesmith::Batch.new(self)
133
127
 
134
- if b.first && b.first.is_a?(ActiveRecord::Base)
135
- b.extend Tablesmith::ActiveRecordSource
128
+ if defined?(ActiveRecord) && defined?(ActiveRecord::Base)
129
+ if b.first && b.first.is_a?(ActiveRecord::Base)
130
+ b.extend Tablesmith::ActiveRecordSource
131
+ end
136
132
  end
137
133
 
138
134
  if b.first && b.first.is_a?(Hash)
139
135
  b.extend Tablesmith::HashRowsSource
140
136
  end
141
137
 
138
+ if b.first && b.first.is_a?(Array)
139
+ b.extend Tablesmith::ArrayRowsSource
140
+ end
141
+
142
142
  b
143
143
  end
144
144
  end
@@ -60,4 +60,36 @@ module Tablesmith::HashRowsSource
60
60
  # Array addition from text-table
61
61
  table.to_table(:first_row_is_head => true)
62
62
  end
63
+
64
+ # not all resulting rows will have data in all columns, so make sure all rows pad out missing columns
65
+ def normalize_keys(rows)
66
+ all_keys = rows.map { |hash_row| hash_row.keys }.flatten.uniq
67
+ rows.map { |hash_row| all_keys.each { |key| hash_row[key] ||= '' } }
68
+ end
69
+
70
+ def sort_columns(rows)
71
+ rows.map! do |row|
72
+ # this sort gives preference to column_order then falls back to alphabetic for leftovers.
73
+ # this is handy when columns auto-generate based on hash data.
74
+ row.sort do |a, b|
75
+ a_col_name, b_col_name = [a.first, b.first]
76
+ a_col_index, b_col_index = [column_order.index(a_col_name), column_order.index(b_col_name)]
77
+
78
+ if a_col_index.nil? && b_col_index.nil?
79
+ a_col_name <=> b_col_name
80
+ else
81
+ (a_col_index || 999) <=> (b_col_index || 999)
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ def row_values(row)
88
+ row.map(&:last)
89
+ end
90
+
91
+ def create_headers(rows)
92
+ column_names = rows.first.map(&:first)
93
+ grouped_headers(column_names) + [apply_column_aliases(column_names), :separator]
94
+ end
63
95
  end
@@ -1,3 +1,3 @@
1
1
  module Tablesmith
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.2'
3
3
  end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Array Source' do
4
+ it 'just works in a console' do
5
+ expected = <<-TABLE
6
+ +---+---+---+
7
+ | a | b | c |
8
+ +---+---+---+
9
+ | d | e | f |
10
+ +---+---+---+
11
+ TABLE
12
+ [%w(a b c), %w(d e f)].to_batch.text_table.to_s.should == expected
13
+ end
14
+ end
data/spec/batch_spec.rb CHANGED
@@ -29,4 +29,17 @@ describe Batch do
29
29
  TEXT
30
30
  [].to_batch.text_table.to_s.should == expected
31
31
  end
32
+
33
+ it 'should handle a simple two row Array' do
34
+ a = [%w(a b c), %w(d e f)]
35
+ actual = a
36
+ expected = <<-TABLE
37
+ +---+---+---+
38
+ | a | b | c |
39
+ +---+---+---+
40
+ | d | e | f |
41
+ +---+---+---+
42
+ TABLE
43
+ actual.to_batch.text_table.to_s.should == expected
44
+ end
32
45
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tablesmith
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - chrismo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-28 00:00:00.000000000 Z
11
+ date: 2017-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: text-table
@@ -125,11 +125,12 @@ files:
125
125
  - Rakefile
126
126
  - lib/tablesmith.rb
127
127
  - lib/tablesmith/active_record_source.rb
128
+ - lib/tablesmith/array_rows_source.rb
128
129
  - lib/tablesmith/batch.rb
129
130
  - lib/tablesmith/hash_rows_source.rb
130
131
  - lib/tablesmith/version.rb
131
132
  - spec/active_record_batch_spec.rb
132
- - spec/array_spec.rb
133
+ - spec/array_batch_spec.rb
133
134
  - spec/batch_spec.rb
134
135
  - spec/fixtures.rb
135
136
  - spec/hash_rows_batch_spec.rb
@@ -155,13 +156,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
155
156
  version: '0'
156
157
  requirements: []
157
158
  rubyforge_project:
158
- rubygems_version: 2.6.12
159
+ rubygems_version: 2.5.2
159
160
  signing_key:
160
161
  specification_version: 4
161
162
  summary: Minimal console table
162
163
  test_files:
163
164
  - spec/active_record_batch_spec.rb
164
- - spec/array_spec.rb
165
+ - spec/array_batch_spec.rb
165
166
  - spec/batch_spec.rb
166
167
  - spec/fixtures.rb
167
168
  - spec/hash_rows_batch_spec.rb
data/spec/array_spec.rb DELETED
@@ -1 +0,0 @@
1
- # Want stuff to work with plain Arrays