Class: MARC::AlephSequential::ASLineGroup
- Inherits:
-
Object
- Object
- MARC::AlephSequential::ASLineGroup
- Includes:
- Log
- Defined in:
- lib/marc_alephsequential/asline_group.rb
Overview
A group of ASLine objects with logic to correctly turn them into a MARC::Record object
Instance Attribute Summary collapse
-
#aslines ⇒ Array<MARC::Field>
Internal list of MARC field object.
-
#leader ⇒ Object
readonly
Returns the value of attribute leader.
Instance Method Summary collapse
-
#add(asline) ⇒ Undefined
Add an ASLine object, turning it into the appropriate type of field as we go An ASLine object with type :invalid_id will be treated as a string and appended to the previous field (to deal with not-uncommon spurious newlines in data fields) field to concatentate it to.
-
#add_string(asline_string, line_number) ⇒ Object
Add an asline as a raw string.
-
#as_record ⇒ MARC::Record
(also: #to_record)
Turn this object into a MARC::Record.
-
#empty? ⇒ Boolean
Is this group empty?.
-
#initialize ⇒ ASLineGroup
constructor
A new instance of ASLineGroup.
-
#size ⇒ Object
Number of aslines already added.
Methods included from Log
Constructor Details
#initialize ⇒ ASLineGroup
Returns a new instance of ASLineGroup.
25 26 27 28 |
# File 'lib/marc_alephsequential/asline_group.rb', line 25 def initialize @aslines = [] @leader = nil end |
Instance Attribute Details
#aslines ⇒ Array<MARC::Field>
Returns Internal list of MARC field object.
18 19 20 |
# File 'lib/marc_alephsequential/asline_group.rb', line 18 def aslines @aslines end |
#leader ⇒ Object (readonly)
Returns the value of attribute leader.
22 23 24 |
# File 'lib/marc_alephsequential/asline_group.rb', line 22 def leader @leader end |
Instance Method Details
#add(asline) ⇒ Undefined
Add an ASLine object, turning it into the appropriate type of field as we go An ASLine object with type :invalid_id will be treated as a string and appended to the previous field (to deal with not-uncommon spurious newlines in data fields) field to concatentate it to.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/marc_alephsequential/asline_group.rb', line 49 def add(asline) case asline.type when :leader if leader log.warn("#{asline.line_number} #{asline.id} Set leader more than once; last one wins") end @leader = asline.value when :invalid_id lastfield = @aslines.pop unless lastfield raise MARC::AlephSequential::Error.new('unknown', asline.line_number), "#{asline.line_number} has invalid id and no preivous line to concat it to (file starts bad?)" nil end log.info "#{asline.line_number} #{lastfield.id} / #{lastfield.tag} Concatenating line #{asline.line_number} to previous line" @aslines.push ASLine.new(lastfield.rawstr + asline.rawstr, lastfield.line_number) else @aslines.push asline end end |
#add_string(asline_string, line_number) ⇒ Object
Add an asline as a raw string
72 73 74 |
# File 'lib/marc_alephsequential/asline_group.rb', line 72 def add_string(asline_string, line_number) self.add(ASLine.new(asline_string, line_number)) end |
#as_record ⇒ MARC::Record Also known as: to_record
Turn this object into a MARC::Record
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/marc_alephsequential/asline_group.rb', line 80 def as_record if empty? raise MARC::AlephSequential::Error.new('unknown', 'unknown'), "Can't turn an empty group into a record", nil end unless leader raise MARC::AlephSequential::Error.new(@aslines[0].id, @aslines[0].line_number), "Record #{@aslines[0].id} (near line #{ @aslines[0].line_number}) has no leader; can't turn into a record", nil end r = MARC::Record.new r.leader = leader aslines.map { |f| r << f.to_field } return r end |
#empty? ⇒ Boolean
Is this group empty?
37 38 39 |
# File 'lib/marc_alephsequential/asline_group.rb', line 37 def empty? aslines.empty? end |
#size ⇒ Object
Number of aslines already added
32 33 34 |
# File 'lib/marc_alephsequential/asline_group.rb', line 32 def size aslines.size end |