class MARC::AlephSequential::ASLine

A model of a line (field) in an alephsequential file.

Constants

SUBFIELD_SPLIT_PATTERN

Pattern used to split data field values into subfield code/value pairs

TURN_TO_SPACE

Characters in leader/control fields that need to be turned (back) into spaces

VALID_ID

How to know if we have a valid id? Must be 9 digits

Attributes

id[RW]
ind1[RW]
ind2[RW]
line_number[RW]

The line number in the file/stream, for error reporting

rawstr[RW]

The passed in raw string, used for post-processing later on

tag[RW]

The MARC field's tag

type[RW]

The type of field (:leader, :control, :data, or :invalid_id)

value[RW]

Either the value of a control/fiexed field, or a string representation of a datafield's subfield

Public Class Methods

new(rawstr, line_number) click to toggle source

Given a raw string and a line number, construct the appropriate ASLine.

@param [String] rawstr The raw string from the file @param [Number] line_number The line number from the file/stream, for error reporting

# 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

Public Instance Methods

cleanup_fixed(val) click to toggle source

Clean up fixed fields/leader, turning Ex Libris characters back into normal characters @param [String] val The string to clean @return [String] The cleaned string

# File lib/marc_alephsequential/asline.rb, line 121
def cleanup_fixed(val)
  return val.gsub(TURN_TO_SPACE, ' ')
end
parse_string_into_subfields(val) click to toggle source

Parse out a non-controlfield value string into a set of subfields @param [String] val the value string, of the form “$$athis is the a$$band the b” @return [Array<Subfield>] An array of MARC 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)

# 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) click to toggle source

Get a line and parse it out into its componant parts @param [String] line the line to parse @return [Array] An array of the form [id, tag, ind1, ind2, value]

# 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
tag=(t) click to toggle source

Set the tag. As a side effect, set the type when we set the tag type will end up as :leader, :control, :data, or :invalid_id

# File lib/marc_alephsequential/asline.rb, line 127
def tag=(t)
  @tag = t
  if t == 'LDR'
    self.type = :leader
  elsif MARC::ControlField.control_tag?(t)
    self.type = :control
  elsif self.valid_id?
    self.type = :data
  else
    self.type = :invalid_id
  end
end
to_control_field() click to toggle source

Turn the current object into a control field, without doing any checks @return [MARC::ControlField]

# File lib/marc_alephsequential/asline.rb, line 81
def to_control_field
  MARC::ControlField.new(tag, cleanup_fixed(self.value))
end
to_data_field() click to toggle source

Turn the current object into a datafield, without doing any checks @return [MARC::DataField]

# 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() click to toggle source

Turn it into an actual MARC field (control or data) Throw an error if called on a leader (LDR) line @return [MARC::ControlField, MARC::DataField]

# 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?() click to toggle source

Does this line have a valid (-looking) id?

# File lib/marc_alephsequential/asline.rb, line 60
def valid_id?
  return VALID_ID.match(id) ? true : false
end