class Bumblebee::ColumnSet

Maintains a list (dictionary) of columns where each header can only exist once. If columns with same header are re-added, then they are overwritten. This class also provides a factory interface through add for adding columns

Constants

FACTORY_ADD_METHODS

Attributes

column_hash[R]

Public Class Methods

new(columns = nil) click to toggle source
# File lib/bumblebee/column_set.rb, line 28
def initialize(columns = nil)
  @column_hash = {}

  add(columns)
end

Public Instance Methods

add(*args) click to toggle source
# File lib/bumblebee/column_set.rb, line 42
def add(*args)
  args.flatten.compact.each { |arg| factory_add(arg) }

  self
end
column(header, opts = {}) click to toggle source
# File lib/bumblebee/column_set.rb, line 34
def column(header, opts = {})
  column = Column.new(header, normalize_opts(opts))

  column_hash[column.header] = column

  self
end

Private Instance Methods

add_header_column_hash(hash) click to toggle source
# File lib/bumblebee/column_set.rb, line 58
def add_header_column_hash(hash)
  hash.each_pair do |header, opts|
    column = Column.new(header, normalize_opts(opts))

    assign(column)
  end

  self
end
assign(column) click to toggle source
# File lib/bumblebee/column_set.rb, line 52
def assign(column)
  column_hash[column.header] = column

  self
end
factory_add(arg) click to toggle source
# File lib/bumblebee/column_set.rb, line 68
def factory_add(arg)
  FACTORY_ADD_METHODS.each do |factory_add_method|
    class_constant = factory_add_method.first
    method_sym = factory_add_method.last

    if arg.is_a?(class_constant)
      send(method_sym, arg)
      return self
    end
  end

  # Base case, use arg as the header
  column(arg)
end
normalize_opts(opts) click to toggle source

This is so we can support Header => Column Property direct assignment columns for simple object-to-csv directives

# File lib/bumblebee/column_set.rb, line 85
def normalize_opts(opts)
  opts.is_a?(Hash) ? opts.symbolize_keys : { property: opts }
end