Module: Tablesmith::HashRowsSource
- Includes:
- HashRowsBase
- Defined in:
- lib/tablesmith/hash_rows_source.rb
Instance Method Summary
collapse
#create_headers, #normalize_keys, #row_values, #sort_columns
Instance Method Details
#build_columns ⇒ Object
TODO: no support for deep
24
25
26
27
28
29
30
|
# File 'lib/tablesmith/hash_rows_source.rb', line 24
def build_columns
@columns ||= []
map do |hash_row|
@columns << hash_row.keys.map { |k| Tablesmith::Column.new(name: k) }
end
@columns.flatten!
end
|
#convert_item_to_hash_row(item) ⇒ Object
11
12
13
|
# File 'lib/tablesmith/hash_rows_source.rb', line 11
def convert_item_to_hash_row(item)
flatten_hash_to_row(item, columns)
end
|
#flatten_hash_to_row(deep_hash, columns) ⇒ Object
15
16
17
18
19
20
21
|
# File 'lib/tablesmith/hash_rows_source.rb', line 15
def flatten_hash_to_row(deep_hash, columns)
row = {}
columns.each do |col_or_hash|
value_from_hash(row, deep_hash, col_or_hash)
end
row
end
|
#hash_rows_to_text_table(hash_rows) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/tablesmith/hash_rows_source.rb', line 47
def hash_rows_to_text_table(hash_rows)
require 'text-table'
= hash_rows.first.keys
table = []
table <<
hash_rows.each do |hash_row|
row = []
.each do ||
row << hash_row[]
end
table << row
end
table.to_table(first_row_is_head: true)
end
|
#text_table ⇒ Object
6
7
8
9
|
# File 'lib/tablesmith/hash_rows_source.rb', line 6
def text_table
build_columns if columns.nil?
super
end
|
#value_from_hash(row, deep_hash, col_or_hash) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/tablesmith/hash_rows_source.rb', line 32
def value_from_hash(row, deep_hash, col_or_hash)
case col_or_hash
when Tablesmith::Column
row[col_or_hash.display_name] = deep_hash[col_or_hash.name].to_s
when Hash
col_or_hash.each_pair do |sub_hash_key, cols_or_hash|
[cols_or_hash].flatten.each do |inner_col_or_hash|
value_from_hash(row, deep_hash[sub_hash_key], inner_col_or_hash)
end
end
end
rescue StandardError => e
warn "#{e.message}: #{col_or_hash}" if @debug
end
|