Class: BerkeleyLibrary::TIND::Export::Table

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/berkeley_library/tind/export/table.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exportable_only: false) ⇒ Table

Initializes a new Table

Parameters:

  • exportable_only (Boolean) (defaults to: false)

    whether to filter out non-exportable fields

See Also:

  • Tags


26
27
28
# File 'lib/berkeley_library/tind/export/table.rb', line 26

def initialize(exportable_only: false)
  @column_groups = ColumnGroupList.new(exportable_only: exportable_only)
end

Instance Attribute Details

#column_groupsObject (readonly)


Accessors



17
18
19
# File 'lib/berkeley_library/tind/export/table.rb', line 17

def column_groups
  @column_groups
end

Class Method Details

.from_records(records, freeze: false, exportable_only: false) ⇒ Table

Returns a new Table for the provided MARC records.

Parameters:

  • records (Enumerable<MARC::Record>)

    the records

  • freeze (Boolean) (defaults to: false)

    whether to freeze the table

  • exportable_only (Boolean) (defaults to: false)

    whether to include only exportable fields

Returns:



40
41
42
43
44
45
# 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

Instance Method Details

#<<(marc_record) ⇒ Object

Adds the specified record

Parameters:

  • marc_record (MARC::Record)

    the record to add

Raises:

  • (FrozenError)


122
123
124
125
126
127
128
129
130
131
# 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_countObject



75
76
77
# File 'lib/berkeley_library/tind/export/table.rb', line 75

def column_count
  columns.size
end

#columnsArray<Column>

The columns

Returns:

  • (Array<Column>)

    the columns.



70
71
72
73
# 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| ... } ⇒ Object

Yield Parameters:

  • row (Row)

    each row



92
93
94
95
96
# 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?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/berkeley_library/tind/export/table.rb', line 112

def empty?
  marc_records.empty?
end

#freezeObject



141
142
143
144
145
146
# 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?Boolean


Object overrides

Returns:

  • (Boolean)


136
137
138
139
# 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

#headersArray<String>

The column headers

Returns:

  • (Array<String>)

    the column headers



63
64
65
# File 'lib/berkeley_library/tind/export/table.rb', line 63

def headers
  columns.map(&:header)
end

#marc_recordsArray<MARC::Record>

The MARC records

Returns:

  • (Array<MARC::Record>)

    the records



108
109
110
# File 'lib/berkeley_library/tind/export/table.rb', line 108

def marc_records
  @marc_records ||= []
end

#row_countInteger

The number of rows (records)

Returns:

  • (Integer)

    the number of rows



101
102
103
# File 'lib/berkeley_library/tind/export/table.rb', line 101

def row_count
  marc_records.size
end

#rowsArray<Row>

The rows

Returns:

  • (Array<Row>)

    the rows



85
86
87
88
89
# 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) ⇒ Object

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



152
153
154
155
156
# 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) ⇒ Object


Cell accessors



51
52
53
54
55
# 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