Class: MARC::AlephSequential::ASLine
- Inherits:
-
Object
- Object
- MARC::AlephSequential::ASLine
- Includes:
- Log
- Defined in:
- lib/marc_alephsequential/asline.rb
Overview
A model of a line (field) in an alephsequential file.
Constant Summary collapse
- TURN_TO_SPACE =
Characters in leader/control fields that need to be turned (back) into spaces
/\^/
- SUBFIELD_SPLIT_PATTERN =
Pattern used to split data field values into subfield code/value pairs
/\$\$([a-zA-Z0-9])/
- VALID_ID =
How to know if we have a valid id? Must be 9 digits
/^\d{9}$/
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#ind1 ⇒ Object
Returns the value of attribute ind1.
-
#ind2 ⇒ Object
Returns the value of attribute ind2.
-
#line_number ⇒ Object
The line number in the file/stream, for error reporting.
-
#rawstr ⇒ Object
The passed in raw string, used for post-processing later on.
-
#tag ⇒ Object
The MARC field's tag.
-
#type ⇒ Object
The type of field (:leader, :control, :data, or :invalid_id).
-
#value ⇒ Object
Either the value of a control/fiexed field, or a string representation of a datafield's subfield.
Instance Method Summary collapse
-
#cleanup_fixed(val) ⇒ String
Clean up fixed fields/leader, turning Ex Libris characters back into normal characters.
-
#initialize(rawstr, line_number) ⇒ ASLine
constructor
Given a raw string and a line number, construct the appropriate ASLine.
-
#parse_string_into_subfields(val) ⇒ Array<Subfield>
Parse out a non-controlfield value string into a set of subfields If the first value in the array returned by the split isn't the empty string, then the string didn't start with '$$' and we should throw a warning (and put the value into a subfield 'a' if we're running in flexible mode).
-
#parseline(line) ⇒ Array
Get a line and parse it out into its componant parts.
-
#to_control_field ⇒ MARC::ControlField
Turn the current object into a control field, without doing any checks.
-
#to_data_field ⇒ MARC::DataField
Turn the current object into a datafield, without doing any checks.
-
#to_field ⇒ MARC::ControlField, MARC::DataField
Turn it into an actual MARC field (control or data) Throw an error if called on a leader (LDR) line.
-
#valid_id? ⇒ Boolean
Does this line have a valid (-looking) id?.
Methods included from Log
Constructor Details
#initialize(rawstr, line_number) ⇒ ASLine
Given a raw string and a line number, construct the appropriate ASLine.
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/marc_alephsequential/asline.rb', line 46 def initialize(rawstr, line_number) @rawstr = rawstr.chomp @line_number = line_number (self.id,self.tag,self.ind1,self.ind2,self.value) = *(parseline(@rawstr)) # clean up the leader or fixed fields if [:leader, :control].include? self.type self.value = cleanup_fixed(self.value) end end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
35 36 37 |
# File 'lib/marc_alephsequential/asline.rb', line 35 def id @id end |
#ind1 ⇒ Object
Returns the value of attribute ind1.
35 36 37 |
# File 'lib/marc_alephsequential/asline.rb', line 35 def ind1 @ind1 end |
#ind2 ⇒ Object
Returns the value of attribute ind2.
35 36 37 |
# File 'lib/marc_alephsequential/asline.rb', line 35 def ind2 @ind2 end |
#line_number ⇒ Object
The line number in the file/stream, for error reporting
27 28 29 |
# File 'lib/marc_alephsequential/asline.rb', line 27 def line_number @line_number end |
#rawstr ⇒ Object
The passed in raw string, used for post-processing later on
24 25 26 |
# File 'lib/marc_alephsequential/asline.rb', line 24 def rawstr @rawstr end |
#tag ⇒ Object
The MARC field's tag
38 39 40 |
# File 'lib/marc_alephsequential/asline.rb', line 38 def tag @tag end |
#type ⇒ Object
The type of field (:leader, :control, :data, or :invalid_id)
33 34 35 |
# File 'lib/marc_alephsequential/asline.rb', line 33 def type @type end |
#value ⇒ Object
Either the value of a control/fiexed field, or a string representation of a datafield's subfield
30 31 32 |
# File 'lib/marc_alephsequential/asline.rb', line 30 def value @value end |
Instance Method Details
#cleanup_fixed(val) ⇒ String
Clean up fixed fields/leader, turning Ex Libris characters back into normal characters
121 122 123 |
# File 'lib/marc_alephsequential/asline.rb', line 121 def cleanup_fixed(val) return val.gsub(TURN_TO_SPACE, ' ') end |
#parse_string_into_subfields(val) ⇒ Array<Subfield>
Parse out a non-controlfield value string into a set of subfields If the first value in the array returned by the split isn't the empty string, then the string didn't start with '$$' and we should throw a warning (and put the value into a subfield 'a' if we're running in flexible mode)
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/marc_alephsequential/asline.rb', line 107 def parse_string_into_subfields(val) sfpairs = val.split(SUBFIELD_SPLIT_PATTERN) initial_null_string = sfpairs.shift unless initial_null_string == '' # do something about the error end sfpairs.each_slice(2).map {|code, val| MARC::Subfield.new(code, val) } end |
#parseline(line) ⇒ Array
Get a line and parse it out into its componant parts
145 146 147 148 149 150 151 152 |
# File 'lib/marc_alephsequential/asline.rb', line 145 def parseline(line) id = line[0,9] tag = line[10,3] ind1 = line[13,1] ind2 = line[14,1] value = line[18..-1] return [id,tag,ind1,ind2,value] end |
#to_control_field ⇒ MARC::ControlField
Turn the current object into a control field, without doing any checks
81 82 83 |
# File 'lib/marc_alephsequential/asline.rb', line 81 def to_control_field MARC::ControlField.new(tag, cleanup_fixed(self.value)) end |
#to_data_field ⇒ MARC::DataField
Turn the current object into a datafield, without doing any checks
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/marc_alephsequential/asline.rb', line 87 def to_data_field if self.value[0..1] != '$$' log.error("#{self.line_number} #{self.id} Variable field #{self.tag} doesn't start with '$$'. Prepending '$$a'.") self.value = '$$a' + self.value end subfields = parse_string_into_subfields(value) f = MARC::DataField.new(tag, ind1, ind2) f.subfields = subfields return f end |
#to_field ⇒ MARC::ControlField, MARC::DataField
Turn it into an actual MARC field (control or data) Throw an error if called on a leader (LDR) line
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/marc_alephsequential/asline.rb', line 68 def to_field case type when :control self.to_control_field when :data self.to_data_field else raise MARC::AlephSequential::Error.new(id, line_number ), "Tried to call #to_field on line type '#{self.type}'", nil end end |
#valid_id? ⇒ Boolean
Does this line have a valid (-looking) id?
60 61 62 |
# File 'lib/marc_alephsequential/asline.rb', line 60 def valid_id? return VALID_ID.match(id) ? true : false end |