class Dill::TextTable
Attributes
table[RW]
Public Class Methods
Array(table)
click to toggle source
# File lib/dill/text_table.rb, line 9 def Array(table) new(table).to_a end
Hash(table)
click to toggle source
# File lib/dill/text_table.rb, line 13 def Hash(table) new(table).to_h end
map(name, options = {}, &block)
click to toggle source
# File lib/dill/text_table.rb, line 17 def map(name, options = {}, &block) case name when :* set_default_mapping options, &block else set_mapping name, options, &block end end
mappings()
click to toggle source
# File lib/dill/text_table.rb, line 26 def mappings @mappings ||= Hash. new { |h, k| h[k] = Mapping.new }. merge(with_parent_mappings) end
new(table)
click to toggle source
# File lib/dill/text_table.rb, line 74 def initialize(table) self.table = table end
skip(name)
click to toggle source
# File lib/dill/text_table.rb, line 32 def skip(name) case name when :* set_default_mapping VoidMapping else raise ArgumentError, "can't convert #{name.inspect} to name" end end
Private Class Methods
set_default_mapping(options, &block)
click to toggle source
# File lib/dill/text_table.rb, line 43 def set_default_mapping(options, &block) case options when Hash @mappings = Hash. new { |h, k| h[k] = Mapping.new(key_transformer: options[:to], value_transformer: block) }. merge(mappings) when Class @mappings = Hash.new { |h, k| h[k] = options.new }.merge(mappings) else raise ArgumentError, "can't convert #{options.inspect} to mapping" end end
set_mapping(name, options, &block)
click to toggle source
# File lib/dill/text_table.rb, line 58 def set_mapping(name, options, &block) mappings[name] = Mapping. new(key: options[:to], value_transformer: block) end
with_parent_mappings()
click to toggle source
# File lib/dill/text_table.rb, line 63 def with_parent_mappings if superclass.respond_to?(:mappings) superclass.send(:mappings).dup else {} end end
Public Instance Methods
each(&block)
click to toggle source
# File lib/dill/text_table.rb, line 78 def each(&block) rows.each(&block) end
rows()
click to toggle source
# File lib/dill/text_table.rb, line 82 def rows @rows ||= table.hashes.map { |h| new_row(h) } end
Also aliased as: to_a
single_row()
click to toggle source
# File lib/dill/text_table.rb, line 86 def single_row @single_row ||= new_row(table.rows_hash) end
Also aliased as: to_h
Private Instance Methods
mapping_for(header)
click to toggle source
# File lib/dill/text_table.rb, line 103 def mapping_for(header) mappings[header] end
new_row(hash)
click to toggle source
# File lib/dill/text_table.rb, line 97 def new_row(hash) hash.each_with_object({}) { |(k, v), h| mapping_for(k).set(self, h, k, CellText.new(v)) } end