Sha256: a22ad552c33b3c79c30ad9103afdd9fbbbc652687f493b24f654cda1dd2e0940

Contents?: true

Size: 1.16 KB

Versions: 5

Compression:

Stored size: 1.16 KB

Contents

# ActiveRecord and HashRowsSource share a lot, but not everything.
module Tablesmith::HashRowsBase
  # not all resulting rows will have data in all columns, so make sure all rows pad out missing columns
  def normalize_keys(rows)
    all_keys = rows.map(&:keys).flatten.uniq
    rows.map { |hash_row| all_keys.each { |key| hash_row[key] ||= '' } }
  end

  def sort_columns(rows)
    return if column_order.empty?
    rows.map! do |row|
      # this sort gives preference to column_order then falls back to alphabetic for leftovers.
      # this is handy when columns auto-generate based on hash data.
      row.sort do |a, b|
        a_col_name, b_col_name = [a.first, b.first]
        a_col_index, b_col_index = [column_order.index(a_col_name), column_order.index(b_col_name)]

        if a_col_index.nil? && b_col_index.nil?
          a_col_name <=> b_col_name
        else
          (a_col_index || 999) <=> (b_col_index || 999)
        end
      end
    end
  end

  def row_values(row)
    row.map(&:last)
  end

  def create_headers(rows)
    column_names = rows.first.map(&:first)
    grouped_headers(column_names) + [apply_column_aliases(column_names), :separator]
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
tablesmith-0.4.1 lib/tablesmith/hash_rows_base.rb
tablesmith-0.4.0 lib/tablesmith/hash_rows_base.rb
tablesmith-0.3.1 lib/tablesmith/hash_rows_base.rb
tablesmith-0.3.0 lib/tablesmith/hash_rows_base.rb
tablesmith-0.2.2 lib/tablesmith/hash_rows_base.rb