Sha256: 7eaf16010ebcf2f2b60a5b8e6ce3db7285109c83c3697aaa0784f19db573ba25
Contents?: true
Size: 1.47 KB
Versions: 3
Compression:
Stored size: 1.47 KB
Contents
# frozen_string_literal: true module Tablesmith::HashRowsSource include Tablesmith::HashRowsBase def text_table build_columns if columns.nil? super end def convert_item_to_hash_row(item) flatten_hash_to_row(item, columns) end 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 # TODO: no support for deep def build_columns @columns ||= [] map do |hash_row| @columns << hash_row.keys.map { |k| Tablesmith::Column.new(name: k) } end @columns.flatten! end 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 def hash_rows_to_text_table(hash_rows) require 'text-table' header_row = hash_rows.first.keys table = [] table << header_row hash_rows.each do |hash_row| row = [] header_row.each do |header| row << hash_row[header] end table << row end # Array addition from text-table table.to_table(first_row_is_head: true) end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
tablesmith-0.6.2 | lib/tablesmith/hash_rows_source.rb |
tablesmith-0.6.0 | lib/tablesmith/hash_rows_source.rb |
tablesmith-0.5.0 | lib/tablesmith/hash_rows_source.rb |