Class: BerkeleyLibrary::TIND::Export::Table
- Inherits:
-
Object
- Object
- BerkeleyLibrary::TIND::Export::Table
- Includes:
- Logging
- Defined in:
- lib/berkeley_library/tind/export/table.rb
Instance Attribute Summary collapse
-
#column_groups ⇒ Object
readonly
------------------------------------------------------------ Accessors.
Class Method Summary collapse
-
.from_records(records, freeze: false, exportable_only: false) ⇒ Table
Returns a new Table for the provided MARC records.
Instance Method Summary collapse
-
#<<(marc_record) ⇒ Object
Adds the specified record.
- #column_count ⇒ Object
-
#columns ⇒ Array<Column>
The columns.
- #each_row {|row| ... } ⇒ Object
- #empty? ⇒ Boolean
- #freeze ⇒ Object
-
#frozen? ⇒ Boolean
------------------------------------------------------------ Object overrides.
-
#headers ⇒ Array<String>
The column headers.
-
#initialize(exportable_only: false) ⇒ Table
constructor
Initializes a new Table.
-
#marc_records ⇒ Array<MARC::Record>
The MARC records.
-
#row_count ⇒ Integer
The number of rows (records).
-
#rows ⇒ Array<Row>
The rows.
-
#to_csv(out = nil) ⇒ Object
TODO: move to BerkeleyLibrary::TIND::Export::CSVExporter.
-
#value_at(row, col) ⇒ Object
------------------------------------------------------------ Cell accessors.
Constructor Details
#initialize(exportable_only: false) ⇒ Table
Initializes a new Table
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_groups ⇒ Object (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.
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
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_count ⇒ Object
75 76 77 |
# File 'lib/berkeley_library/tind/export/table.rb', line 75 def column_count columns.size end |
#columns ⇒ 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
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
112 113 114 |
# File 'lib/berkeley_library/tind/export/table.rb', line 112 def empty? marc_records.empty? end |
#freeze ⇒ Object
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
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 |
#headers ⇒ 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_records ⇒ Array<MARC::Record>
The MARC records
108 109 110 |
# File 'lib/berkeley_library/tind/export/table.rb', line 108 def marc_records @marc_records ||= [] end |
#row_count ⇒ Integer
The number of rows (records)
101 102 103 |
# File 'lib/berkeley_library/tind/export/table.rb', line 101 def row_count marc_records.size end |
#rows ⇒ 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 |