class CSVModel::HeaderRow

Attributes

data[R]

Public Class Methods

new(data, options = {}) click to toggle source
# File lib/csv_model/header_row.rb, line 9
def initialize(data, options = {})
  @data = data
  @options = options
  validate_options
end

Public Instance Methods

column_count() click to toggle source
# File lib/csv_model/header_row.rb, line 19
def column_count
  columns.count
end
column_index(key) click to toggle source
# File lib/csv_model/header_row.rb, line 23
def column_index(key)
  column_keys.index(key.to_column_key)
end
columns() click to toggle source
# File lib/csv_model/header_row.rb, line 15
def columns
  @columns ||= data.collect { |x| Column.new(x) }
end
errors() click to toggle source
# File lib/csv_model/header_row.rb, line 27
def errors
  (duplicate_column_errors + illegal_column_errors + missing_column_errors + missing_key_column_errors).uniq
end
has_column?(key) click to toggle source
# File lib/csv_model/header_row.rb, line 31
def has_column?(key)
  !column_index(key).nil?
end
primary_key_columns() click to toggle source
# File lib/csv_model/header_row.rb, line 35
def primary_key_columns
  @primary_key_columns ||= begin
    if has_primary_key? && has_primary_key_columns?
      primary_primary_key_columns
    elsif has_alternate_primary_key? && has_alternate_primary_key_columns?
      alternate_primary_key_columns
    else
      []
    end
  end
end
valid?() click to toggle source
# File lib/csv_model/header_row.rb, line 47
def valid?
  has_required_columns? && has_required_key_columns? && !has_duplicate_columns? && !has_illegal_columns?
end

Protected Instance Methods

alternate_primary_key_column_keys() click to toggle source
# File lib/csv_model/header_row.rb, line 57
def alternate_primary_key_column_keys
  alternate_primary_key_column_names.collect { |x| x.to_column_key }
end
alternate_primary_key_column_names() click to toggle source
# File lib/csv_model/header_row.rb, line 61
def alternate_primary_key_column_names
  option(:alternate_primary_key, [])
end
alternate_primary_key_columns() click to toggle source
# File lib/csv_model/header_row.rb, line 53
def alternate_primary_key_columns
  columns.select { |x| alternate_primary_key_column_keys.include?(x.key) }
end
column_keys() click to toggle source
# File lib/csv_model/header_row.rb, line 65
def column_keys
  @column_keys ||= columns.collect { |x| x.key }
end
column_map() click to toggle source
# File lib/csv_model/header_row.rb, line 69
def column_map
  @column_map ||= Hash[columns.collect { |x| [x.key, x] }]
end
column_name(column_key) click to toggle source
# File lib/csv_model/header_row.rb, line 73
def column_name(column_key)
  data.find { |entry| entry.to_column_key == column_key }
end
duplicate_column_errors() click to toggle source
# File lib/csv_model/header_row.rb, line 77
def duplicate_column_errors
  duplicate_column_names.collect { |name| "Multiple columns found for #{name}, column headings must be unique" }
end
duplicate_column_names() click to toggle source
# File lib/csv_model/header_row.rb, line 81
def duplicate_column_names
  data.collect { |x| x.to_column_key }
    .inject(Hash.new(0)) { |counts, key| counts[key] += 1; counts }
    .select { |key, count| count > 1 }
    .collect { |key, count| column_name(key) }
end
has_alternate_primary_key?() click to toggle source
# File lib/csv_model/header_row.rb, line 88
def has_alternate_primary_key?
  alternate_primary_key_column_names.any?
end
has_alternate_primary_key_columns?() click to toggle source
# File lib/csv_model/header_row.rb, line 92
def has_alternate_primary_key_columns?
  missing_alternate_primary_key_column_keys.empty?
end
has_duplicate_columns?() click to toggle source
# File lib/csv_model/header_row.rb, line 96
def has_duplicate_columns?
  data.count != column_map.keys.count
end
has_illegal_columns?() click to toggle source
# File lib/csv_model/header_row.rb, line 100
def has_illegal_columns?
  illegal_column_keys.any?
end
has_primary_key?() click to toggle source
# File lib/csv_model/header_row.rb, line 112
def has_primary_key?
  primary_key_column_names.any?
end
has_primary_key_columns?() click to toggle source
# File lib/csv_model/header_row.rb, line 116
def has_primary_key_columns?
  missing_primary_key_column_keys.empty?
end
has_required_columns?() click to toggle source
# File lib/csv_model/header_row.rb, line 104
def has_required_columns?
  missing_column_keys.empty?
end
has_required_key_columns?() click to toggle source
# File lib/csv_model/header_row.rb, line 108
def has_required_key_columns?
  !has_primary_key? || (has_primary_key? && has_primary_key_columns?) || (has_alternate_primary_key? && has_alternate_primary_key_columns?)
end
illegal_column_errors() click to toggle source
# File lib/csv_model/header_row.rb, line 120
def illegal_column_errors
  illegal_column_names.collect { |name| "Unknown column #{name}" }
end
illegal_column_keys() click to toggle source
# File lib/csv_model/header_row.rb, line 124
def illegal_column_keys
  legal_column_names.any? ? column_keys - legal_column_keys : []    
end
illegal_column_names() click to toggle source
# File lib/csv_model/header_row.rb, line 128
def illegal_column_names
  illegal_column_keys.collect { |key| column_name(key) }
end
missing_alternate_primary_key_column_keys() click to toggle source
# File lib/csv_model/header_row.rb, line 140
def missing_alternate_primary_key_column_keys
  alternate_primary_key_column_keys - column_keys
end
missing_column_errors() click to toggle source
# File lib/csv_model/header_row.rb, line 144
def missing_column_errors
  missing_column_names.collect { |name| "Missing column #{name}" }
end
missing_column_keys() click to toggle source
# File lib/csv_model/header_row.rb, line 148
def missing_column_keys
  required_column_keys - column_keys
end
missing_column_names() click to toggle source
# File lib/csv_model/header_row.rb, line 152
def missing_column_names
  missing_column_keys.collect { |key| required_column_name(key) }
end
missing_key_column_errors() click to toggle source
# File lib/csv_model/header_row.rb, line 164
def missing_key_column_errors
  has_alternate_primary_key? && has_alternate_primary_key_columns? ? [] : primary_key_column_errors
end
missing_primary_key_column_keys() click to toggle source
# File lib/csv_model/header_row.rb, line 156
def missing_primary_key_column_keys
  primary_key_column_keys - column_keys
end
missing_primary_key_column_names() click to toggle source
# File lib/csv_model/header_row.rb, line 160
def missing_primary_key_column_names
  missing_primary_key_column_keys.collect { |key| primary_key_column_name(key) }
end
primary_key_column_errors() click to toggle source
# File lib/csv_model/header_row.rb, line 172
def primary_key_column_errors
  missing_primary_key_column_names.collect { |name| "Missing column #{name}" }
end
primary_key_column_keys() click to toggle source
# File lib/csv_model/header_row.rb, line 176
def primary_key_column_keys
  primary_key_column_names.collect { |x| x.to_column_key }
end
primary_key_column_name(column_key) click to toggle source
# File lib/csv_model/header_row.rb, line 180
def primary_key_column_name(column_key)
  primary_key_column_names.find { |entry| entry.to_column_key == column_key }
end
primary_key_column_names() click to toggle source
# File lib/csv_model/header_row.rb, line 184
def primary_key_column_names
  option(:primary_key, [])
end
primary_primary_key_columns() click to toggle source
# File lib/csv_model/header_row.rb, line 168
def primary_primary_key_columns
  columns.select { |x| primary_key_column_keys.include?(x.key) }
end
required_column_keys() click to toggle source
# File lib/csv_model/header_row.rb, line 188
def required_column_keys
  required_column_names.collect { |x| x.to_column_key }
end
required_column_name(column_key) click to toggle source
# File lib/csv_model/header_row.rb, line 192
def required_column_name(column_key)
  required_column_names.find { |entry| entry.to_column_key == column_key }
end
required_column_names() click to toggle source
# File lib/csv_model/header_row.rb, line 196
def required_column_names
  option(:required_columns, [])
end
validate_options() click to toggle source
# File lib/csv_model/header_row.rb, line 200
def validate_options
  if !option(:primary_key).nil? && primary_key_column_keys.empty?
    raise ArgumentError.new("The primary_key cannot be be empty.")
  end

  if legal_column_keys.any? && (primary_key_column_keys - legal_column_keys).any?
    raise ArgumentError.new("The primary_key cannot contain columns that are not included in legal_column_keys.")
  end

  if primary_key_column_names.empty? && alternate_primary_key_column_names.any?
    raise ArgumentError.new("The alternate_primary_key cannot be specified if no primary_key is specified.")
  end

  if !option(:alternate_primary_key).nil? && primary_key_column_keys == alternate_primary_key_column_keys
    raise ArgumentError.new("The alternate_primary_key cannot be identical to the primary_key.")
  end

  if legal_column_keys.any? && (alternate_primary_key_column_keys - legal_column_keys).any?
    raise ArgumentError.new("The alternate_primary_key cannot contain columns that are not included in legal_column_keys.")
  end
end