dldinternet-mixlib-thor 0.9.1 → 0.9.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: e304facbf9df8b0d9f56707bb2fff2c05e7eeb33
4
- data.tar.gz: acf675f70dfd8c77efb5f58ceaee071802094762
3
+ metadata.gz: 5f544e9a0b1ea51a7d7ed48b61d597ccbae1b90e
4
+ data.tar.gz: 3c2c1ce128e9a9c2cdc914e8c1196aa1b22c2366
5
5
  SHA512:
6
- metadata.gz: 9981d254c00a9be82d1db0936dcd7222ad2dff0212ab1d01ed00486b71ab8d204a7d4ee893156dbaf6c7d01bc92c14ea9723c82531c3cd8797d020c7dc00ba56
7
- data.tar.gz: e65fada56e6859ec0b78f1a1e30b9e4e2864d9ba45a267d0159c6b053a5f070cb69d4a5959768f9155082d1686aba5f573720707662b5c67d6813aa4b35ae362
6
+ metadata.gz: 7e12856ffccc77fa6b111aec39a7577875d8c869f3c55514742823336177af361bab18c75cb82b6ee94bb3c4d34955b7b5bf7644d6faadcc1a16eb481d3b9ce8
7
+ data.tar.gz: dd653631cc637f053dfb7d9d30546576740279bff7c9238724a5db78c692bc34a84532a59f3c443536230abf30119f04cdd64e66b231c74b6e9d0d88d52bdc44
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dldinternet-mixlib-thor (0.9.1)
4
+ dldinternet-mixlib-thor (0.9.2)
5
5
  activesupport (~> 5.1, >= 5.1.4)
6
6
  awesome_print
7
7
  command_line_reporter (~> 3.3, >= 3.3.6)
@@ -76,4 +76,4 @@ DEPENDENCIES
76
76
  rubygems-tasks (~> 0.2)
77
77
 
78
78
  BUNDLED WITH
79
- 1.14.5
79
+ 1.15.4
@@ -44,6 +44,15 @@ module DLDInternet
44
44
  else
45
45
  object.to_s
46
46
  end
47
+ item = if item.is_a?(Hash)
48
+ if @columns.nil?
49
+ item.keys
50
+ else
51
+ @columns.keys
52
+ end
53
+ else
54
+ item
55
+ end
47
56
 
48
57
  format_item item, true
49
58
  end
@@ -110,26 +119,11 @@ module DLDInternet
110
119
 
111
120
  def format_item(item, header=false)
112
121
  fmt = format.to_s.downcase
113
- if /json|yaml|csv/.match?(fmt)
114
- item = if @columns.nil?
115
- item
116
- else
117
- item = item.dup
118
- if item.is_a?(Array)
119
- nitm = item.map do |itm|
120
- hitm = Hash[itm.map do |key, val|
121
- if @columns.keys.include?(key) && @columns[key].nil?
122
- [key,val]
123
- end
124
- end.select{ |e| !e.nil? }]
125
- end
126
- nitm
127
- elsif item.is_a?(Hash)
128
- else
129
- item
130
- end
131
- end
132
- end
122
+ # if /json|yaml|csv/.match?(fmt)
123
+ # item = if @columns.nil?
124
+ # item
125
+ # else
126
+ item = object_item(item, header)
133
127
  case fmt
134
128
  when 'json'
135
129
  JSON.pretty_generate(item)
@@ -153,7 +147,7 @@ module DLDInternet
153
147
  item
154
148
  end
155
149
  else
156
- @columns.keys
150
+ @columns.keys.map{ |k| subkeys(k) }
157
151
  end
158
152
  nitm = if item.is_a?(Array)
159
153
  item.map { |itm|
@@ -179,6 +173,7 @@ module DLDInternet
179
173
  }
180
174
  csv = CSV::Table.new(nitm).to_csv
181
175
  when 'none'
176
+ # columnize_item(item).to_s
182
177
  item
183
178
  else
184
179
  if item.is_a?(Array) # header ||
@@ -198,19 +193,70 @@ module DLDInternet
198
193
  nitm.join(" ")
199
194
  elsif item.is_a?(Hash)
200
195
  nitm = item.map do |key, val|
201
- if @columns.nil? || (@columns.keys.include?(key) && @columns[key].nil?)
202
- sprintf("%-#{widths[key]}s", val.to_s)
203
- elsif !key.index('.').nil?
196
+ if @columns.nil? || @columns.keys.include?(key) #&& @columns[key].nil?)
204
197
  sprintf("%-#{widths[key]}s", subcolumn(key, val))
198
+ # sprintf("%-#{widths[key]}s", val.to_s)
199
+ # elsif !key.index('.').nil?
200
+ # sprintf("%-#{widths[key]}s", subcolumn(key, val))
205
201
  end
206
202
  end
207
- nitm.select{|s| !s.nil?}.join(" ")
203
+ nitm = nitm.select{|s| !s.nil?}.join(" ")
204
+ nitm
208
205
  else
209
206
  item.to_s
210
207
  end
211
208
  end
212
209
  end
213
210
 
211
+ def object_item(item, header=false)
212
+ unless @columns.nil? || header
213
+ item = if item.is_a?(Array)
214
+ nitm = item.map do |itm|
215
+ map_item(itm)
216
+ end
217
+ nitm
218
+ elsif item.is_a?(Hash)
219
+ map_item(item)
220
+ else
221
+ item
222
+ end
223
+ # end
224
+ end
225
+ item
226
+ end
227
+
228
+ def map_item(item)
229
+ itm = columnize_item(item)
230
+ mitm = itm.map do |key, val|
231
+ k, v = [subkeys(key), subcolumn(key, val)]
232
+ v.nil? ? nil : [k, v]
233
+ # if @columns.keys.include?(key)
234
+ # if @columns[key].nil?
235
+ # [key,val]
236
+ # else
237
+ # raise "What now"
238
+ # end
239
+ # end
240
+ end
241
+ mitm = mitm.select {|e| !e.nil?} #&& !e[1].nil? }
242
+ Hash[mitm]
243
+ end
244
+
245
+ def columnize_item(item)
246
+ return item if @columns.nil?
247
+ if item.is_a?(Array)
248
+ itm = item.map { |obj|
249
+ columnize_item(obj)
250
+ }
251
+ else
252
+ itm = ::Hashie::Mash.new
253
+ @columns.map {|k, _|
254
+ itm[k] = item[k]
255
+ }
256
+ end
257
+ itm
258
+ end
259
+
214
260
  def submap(col, val, sub=nil)
215
261
  sub ||= @columns
216
262
  m = col.match(/^([^.]+)\.(.*)$/)
@@ -242,7 +288,7 @@ module DLDInternet
242
288
  def widths_hash(item=nil)
243
289
  item ||= @object
244
290
  item.each do |key, _|
245
- klen = key.to_s.length
291
+ klen = (@columns.nil? ? key : subkeys(key, @columns)).to_s.length
246
292
  wid = @widths[key]
247
293
  @widths[key] = klen if !wid || wid < klen
248
294
  end
@@ -257,45 +303,80 @@ module DLDInternet
257
303
 
258
304
  def obj_width(obj)
259
305
  obj.each do |key, val|
260
- set_width(key, val)
306
+ set_width(key, @columns.nil? ? val : subvalues(key, val, obj))
261
307
  end
262
308
  end
263
309
 
310
+ def column(text, options = {})
311
+ text
312
+ end
313
+
264
314
  def subcolumn(key, val, sub=nil)
265
- sub ||= @columns
266
- if sub[key]
267
- if sub[key].is_a?(Hash)
268
- vals = subvalues(key, val, sub)
269
- if vals.is_a?(Array)
270
- vals.flatten!
271
- # vals = vals[0] if vals.size == 1
315
+ if @columns.nil?
316
+ column(val.to_s)
317
+ else
318
+ sub ||= @columns
319
+ if sub[key]
320
+ if sub[key].is_a?(Hash)
321
+ vals = subvalues(key, val, sub)
322
+ if vals.is_a?(Array)
323
+ vals.flatten!
324
+ # vals = vals[0] if vals.size == 1
325
+ end
326
+ column(vals.to_s)
327
+ else
328
+ column(val.to_s) if sub.has_key?(key)# && sub[key].nil?
272
329
  end
273
- column(vals.to_s)
274
330
  else
275
- column(val.to_s) if sub.has_key?(m[1]) && sub[m[1]].nil?
331
+ column(val.to_s) if sub.has_key?(key) # && sub[key].nil?)
276
332
  end
277
- else
278
- column(val.to_s) if sub.has_key?(key) # && sub[key].nil?)
279
333
  end
280
334
  end
281
335
 
282
336
  def subvalues(key, val, sub)
283
- if sub[key].is_a?(Hash)
284
- sub[key].keys.map {|k|
337
+ sk = sub[key]
338
+ if sk.is_a?(Hash)
339
+ sk.keys.map {|k|
285
340
  case val.class.name
286
- when /^Hash/
341
+ when /Hash$/
287
342
  val[k]
288
- when /^Array/
343
+ when /Array$/
289
344
  val.map {|v|
290
345
  subvalues(k, v[k], sub[key])
291
346
  }
292
347
  end
293
348
  }
349
+ elsif sk.is_a?(Array)
350
+ sk.map {|v|
351
+ h = @columns.nil? ? v : @columns[key]
352
+ h.keys.map { |k|
353
+ subvalues(k, v[k], v)
354
+ }
355
+ }.flatten!
294
356
  else
295
357
  val
296
358
  end
297
359
  end
298
360
 
361
+ def subkeys(key, sub=nil)
362
+ sub ||= @columns
363
+ if sub && sub[key]
364
+ if sub[key].is_a?(Hash)
365
+ keys = sub[key].map { |k,v|
366
+ key+'.'+subkeys(k, v ? v : {})
367
+ }
368
+ if keys.is_a?(Array)
369
+ keys = keys.flatten
370
+ end
371
+ keys.join('.')
372
+ else
373
+ [key,subkeys(key, sub[key])].flatten if sub.has_key?(key)
374
+ end
375
+ else
376
+ key.to_s
377
+ end
378
+ end
379
+
299
380
  end
300
381
  end
301
382
  end
@@ -15,6 +15,9 @@ module DLDInternet
15
15
  super
16
16
  # @object = [@object] unless @object.is_a?(Array)
17
17
  @values = Hashie::Mash.new
18
+ unless @columns.nil?
19
+ @object = columnize_item(@object)
20
+ end
18
21
  end
19
22
 
20
23
  def capture_output
@@ -22,7 +25,7 @@ module DLDInternet
22
25
  begin
23
26
  yield
24
27
  rescue Exception => e
25
- previous_stdout.write $stdout.string
28
+ @previous_stdout.write $stdout.string
26
29
  raise e
27
30
  end
28
31
 
@@ -38,8 +41,11 @@ module DLDInternet
38
41
 
39
42
  def restore_output
40
43
  string = $stdout.string
41
- $stdout = @previous_stdout
42
44
  string
45
+ rescue => e
46
+ # noop
47
+ ensure
48
+ $stdout = @previous_stdout if @previous_stdout
43
49
  end
44
50
 
45
51
  def run
@@ -64,23 +70,29 @@ module DLDInternet
64
70
  end
65
71
 
66
72
  def header_row
67
- list = @object.is_a?(Array) ? @object : [@object]
73
+ # list = if @columns.nil?
74
+ # @object.is_a?(Array) ? @object[0].keys : @object.keys
75
+ # else
76
+ # @columns.keys.map{ |k| subkeys(k) }
77
+ # end
78
+ list = @object.is_a?(Array) ? @object[0].keys : @object.keys
68
79
  row color: 'light_yellow', bold: true, encoding: :ascii do
69
- list[0].each do |key, _|
70
- column key.to_s, width: widths[key] if (@columns.nil? || @columns.keys.include?(key))
80
+ list.each do |key, _|
81
+ column subkeys(key).to_s, width: widths[key]
71
82
  end
72
83
  end
73
84
  end
74
85
 
86
+ def column(text, options = {})
87
+ super
88
+ text
89
+ end
90
+
75
91
  def obj_row(idx, obj)
76
92
  row color: 'white', bold: false do
77
93
  if obj.is_a? Hash
78
94
  obj.each do |key, val|
79
- if @columns.nil? || (@columns.keys.include?(key) && @columns[key].nil?)
80
- column val.to_s
81
- else
82
- subcolumn(key, val)
83
- end
95
+ subcolumn(key, val)
84
96
  end
85
97
  else
86
98
  column obj.to_s, width: widths[idx]
@@ -91,7 +103,7 @@ module DLDInternet
91
103
 
92
104
  def format_it(item=nil)
93
105
  if item
94
- @object = item
106
+ @object = columnize_item(item)
95
107
  @is_a_hash = @object.is_a?(Hash)
96
108
  @widths = nil
97
109
  end
@@ -343,8 +343,8 @@ module DLDInternet
343
343
 
344
344
  def output(obj, fmtr=nil, header=false)
345
345
  unless obj.nil?
346
- hash = obj.is_a?(Array) ? obj.map { |o| hash_it(o) } : (obj.is_a?(String) ? obj : hash_it(obj))
347
- str = string_it(hash, fmtr, header)
346
+ it = obj.is_a?(Array) ? obj.map { |o| hash_it(o) } : (obj.is_a?(String) ? obj : hash_it(obj))
347
+ str = string_it(it, fmtr, header)
348
348
  write str
349
349
  end
350
350
  end
@@ -1,7 +1,7 @@
1
1
  module Dldinternet
2
2
  module Mixlib
3
3
  module Thor
4
- VERSION = '0.9.1'
4
+ VERSION = '0.9.2'
5
5
 
6
6
  module Version # :nodoc: all
7
7
  MAJOR, MINOR, RELEASE, *OTHER = VERSION.split '.'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dldinternet-mixlib-thor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christo De Lange
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-04 00:00:00.000000000 Z
11
+ date: 2017-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor