class BerkeleyLibrary::TIND::Export::Table
Attributes
Accessors
Public Class Methods
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
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
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
# File lib/berkeley_library/tind/export/table.rb, line 75 def column_count columns.size end
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
@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
# File lib/berkeley_library/tind/export/table.rb, line 112 def empty? marc_records.empty? end
# 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
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
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
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
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
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
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
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
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
# 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