class BerkeleyLibrary::TIND::Export::Table

Attributes

column_groups[R]

Accessors

Public Class Methods

from_records(records, freeze: false, exportable_only: false) click to toggle source

Returns a new Table for the provided MARC records.

@param records [Enumerable<MARC::Record>] the records @param freeze [Boolean] whether to freeze the table @param exportable_only [Boolean] whether to include only exportable fields @return [Table] the table

# File lib/berkeley_library/tind/export/table.rb, line 40
def from_records(records, freeze: false, exportable_only: false)
  Table.new(exportable_only: exportable_only).tap do |table|
    records.each { |r| table << r }
    table.freeze if freeze
  end
end
new(exportable_only: false) click to toggle source

Initializes a new Table

@param exportable_only [Boolean] whether to filter out non-exportable fields @see Tags

# File lib/berkeley_library/tind/export/table.rb, line 26
def initialize(exportable_only: false)
  @column_groups = ColumnGroupList.new(exportable_only: exportable_only)
end

Public Instance Methods

<<(marc_record) click to toggle source

Adds the specified record

@param marc_record [MARC::Record] the record to add

# File lib/berkeley_library/tind/export/table.rb, line 122
def <<(marc_record)
  raise FrozenError, "can't modify frozen MARCTable" if frozen?

  logger.warn('MARC record is not frozen') unless marc_record.frozen?
  column_groups.add_data_fields(marc_record, marc_records.size)
  marc_records << marc_record
  log_record_added(marc_record)

  self
end
column_count() click to toggle source
# File lib/berkeley_library/tind/export/table.rb, line 75
def column_count
  columns.size
end
columns() click to toggle source

The columns

@return [Array<Column>] the columns.

# File lib/berkeley_library/tind/export/table.rb, line 70
def columns
  # NOTE: this isn't ||= because we only cache on #freeze
  @columns || column_groups.map(&:columns).flatten
end
each_row() { |row| ... } click to toggle source

@yieldparam row [Row] each row

# File lib/berkeley_library/tind/export/table.rb, line 92
def each_row
  return to_enum(:each_row) unless block_given?

  (0...row_count).each { |row| yield Row.new(columns, row) }
end
empty?() click to toggle source
# File lib/berkeley_library/tind/export/table.rb, line 112
def empty?
  marc_records.empty?
end
freeze() click to toggle source
# File lib/berkeley_library/tind/export/table.rb, line 141
def freeze
  [marc_records, column_groups].each(&:freeze)
  @columns ||= columns.freeze
  @rows ||= rows.freeze
  self
end
frozen?() click to toggle source

Object overrides

# File lib/berkeley_library/tind/export/table.rb, line 136
def frozen?
  [marc_records, column_groups].all?(&:frozen?) &&
    [@rows, @columns].all? { |d| !d.nil? && d.frozen? }
end
headers() click to toggle source

The column headers

@return [Array<String>] the column headers

# File lib/berkeley_library/tind/export/table.rb, line 63
def headers
  columns.map(&:header)
end
marc_records() click to toggle source

The MARC records

@return [Array<MARC::Record>] the records

# File lib/berkeley_library/tind/export/table.rb, line 108
def marc_records
  @marc_records ||= []
end
row_count() click to toggle source

The number of rows (records)

@return [Integer] the number of rows

# File lib/berkeley_library/tind/export/table.rb, line 101
def row_count
  marc_records.size
end
rows() click to toggle source

The rows

@return [Array<Row>] the rows

# File lib/berkeley_library/tind/export/table.rb, line 85
def rows
  # NOTE: this isn't ||= because we only cache on #freeze
  # noinspection RubyYardReturnMatch
  @rows || each_row.to_a
end
to_csv(out = nil) click to toggle source

TODO: move to BerkeleyLibrary::TIND::Export::CSVExporter

# File lib/berkeley_library/tind/export/table.rb, line 152
def to_csv(out = nil)
  return write_csv(out) if out

  StringIO.new.tap { |io| write_csv(io) }.string
end
value_at(row, col) click to toggle source

Cell accessors

# File lib/berkeley_library/tind/export/table.rb, line 51
def value_at(row, col)
  return unless (column = columns[col])

  column.value_at(row)
end

Private Instance Methods

log_record_added(marc_record) click to toggle source

Private methods

# File lib/berkeley_library/tind/export/table.rb, line 163
def log_record_added(marc_record)
  return logger.debug("Added record no. #{marc_record.record_id}: #{row_count} records total") if marc_record
end
write_csv(out) click to toggle source
# File lib/berkeley_library/tind/export/table.rb, line 167
def write_csv(out)
  csv = out.respond_to?(:write) ? CSV.new(out) : CSV.open(out, 'wb')
  csv << headers
  each_row { |row| csv << row.values }
end