Class: TablePrint::Fingerprinter
- Inherits:
-
Object
- Object
- TablePrint::Fingerprinter
- Defined in:
- lib/table_print/fingerprinter.rb
Instance Method Summary collapse
- #create_child_group(prefix, hash, target) ⇒ Object
- #display_method_to_nested_hash(display_method) ⇒ Object
- #display_methods_to_nested_hash(display_methods) ⇒ Object
- #handleable_columns(hash) ⇒ Object
- #hash_to_rows(prefix, hash, objects) ⇒ Object
- #lift(columns, object) ⇒ Object
- #passable_columns(hash) ⇒ Object
- #populate_row(prefix, hash, target) ⇒ Object
Instance Method Details
#create_child_group(prefix, hash, target) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/table_print/fingerprinter.rb', line 54 def create_child_group(prefix, hash, target) passable_columns(hash).collect do |name| recursing_prefix = "#{prefix}#{'.' unless prefix == ''}#{name}" group = RowGroup.new group.add_children hash_to_rows(recursing_prefix, hash[name], target.send(name)) group end end |
#display_method_to_nested_hash(display_method) ⇒ Object
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/table_print/fingerprinter.rb', line 82 def display_method_to_nested_hash(display_method) hash = {} return {display_method => {}} if display_method.is_a? Proc display_method.split(".").inject(hash) do |hash_level, method| hash_level[method] ||= {} end hash end |
#display_methods_to_nested_hash(display_methods) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/table_print/fingerprinter.rb', line 73 def display_methods_to_nested_hash(display_methods) extended_hash = {}.extend TablePrint::HashExtensions::ConstructiveMerge # turn each column chain into a nested hash and add it to the output display_methods.inject(extended_hash) do |hash, display_method| hash.constructive_merge!(display_method_to_nested_hash(display_method)) end end |
#handleable_columns(hash) ⇒ Object
63 64 65 66 |
# File 'lib/table_print/fingerprinter.rb', line 63 def handleable_columns(hash) # get the keys where the value is an empty hash hash.select { |k, v| v == {} }.collect { |k, v| k } end |
#hash_to_rows(prefix, hash, objects) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/table_print/fingerprinter.rb', line 12 def hash_to_rows(prefix, hash, objects) rows = [] # convert each object into its own row Array(objects).each do |target| row = populate_row(prefix, hash, target) rows << row # make a group and recurse for the columns we don't handle groups = create_child_group(prefix, hash, target) row.add_children(groups) unless groups.all? {|g| g.children.empty?} end rows end |
#lift(columns, object) ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/table_print/fingerprinter.rb', line 3 def lift(columns, object) @column_names_by_display_method = {} columns.each { |c| @column_names_by_display_method[c.display_method] = c.name } column_hash = display_methods_to_nested_hash(columns.collect(&:display_method)) hash_to_rows("", column_hash, object) end |
#passable_columns(hash) ⇒ Object
68 69 70 71 |
# File 'lib/table_print/fingerprinter.rb', line 68 def passable_columns(hash) # get the keys where the value is not an empty hash hash.select { |k, v| v != {} }.collect { |k, v| k } end |
#populate_row(prefix, hash, target) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/table_print/fingerprinter.rb', line 30 def populate_row(prefix, hash, target) row = TablePrint::Row.new() # populate a row with the columns we handle cells = {} handleable_columns(hash).each do |method| display_method = (prefix == "" ? method : "#{prefix}.#{method}") if method.is_a? Proc cell_value = method.call(target) elsif target.is_a? Hash and target.keys.include? method.to_sym cell_value = target[method.to_sym] elsif target.is_a? Hash and target.keys.include? method cell_value = target[method] elsif target.respond_to? method cell_value ||= target.send(method) else cell_value = "Method Missing" end cells[@column_names_by_display_method[display_method]] = cell_value end row.set_cell_values(cells) end |