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.
26 27 28 29 |
# File 'lib/marc_alephsequential/asline_group.rb', line 26 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.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/marc_alephsequential/asline_group.rb', line 50 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
73 74 75 |
# File 'lib/marc_alephsequential/asline_group.rb', line 73 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
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/marc_alephsequential/asline_group.rb', line 81 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?
38 39 40 |
# File 'lib/marc_alephsequential/asline_group.rb', line 38 def empty? aslines.empty? end |
#size ⇒ Object
Number of aslines already added
33 34 35 |
# File 'lib/marc_alephsequential/asline_group.rb', line 33 def size aslines.size end |