Class: MARC::AlephSequential::ASLine

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Log

log, #log, log=

Constructor Details

#initialize(rawstr, line_number) ⇒ ASLine

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

Parameters:

  • rawstr (String)

    The raw string from the file

  • line_number (Number)

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



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

#idObject

Returns the value of attribute id.



35
36
37
# File 'lib/marc_alephsequential/asline.rb', line 35

def id
  @id
end

#ind1Object

Returns the value of attribute ind1.



35
36
37
# File 'lib/marc_alephsequential/asline.rb', line 35

def ind1
  @ind1
end

#ind2Object

Returns the value of attribute ind2.



35
36
37
# File 'lib/marc_alephsequential/asline.rb', line 35

def ind2
  @ind2
end

#line_numberObject

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

#rawstrObject

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

#tagObject

The MARC field's tag



38
39
40
# File 'lib/marc_alephsequential/asline.rb', line 38

def tag
  @tag
end

#typeObject

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

#valueObject

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

Parameters:

  • val (String)

    The string to clean

Returns:

  • (String)

    The cleaned string



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)

Parameters:

  • val (String)

    the value string, of the form "$$athis is the a$$band the b"

Returns:

  • (Array<Subfield>)

    An array of MARC subfields



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

Parameters:

  • line (String)

    the line to parse

Returns:

  • (Array)

    An array of the form [id, tag, ind1, ind2, value]



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_fieldMARC::ControlField

Turn the current object into a control field, without doing any checks

Returns:

  • (MARC::ControlField)


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_fieldMARC::DataField

Turn the current object into a datafield, without doing any checks

Returns:

  • (MARC::DataField)


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_fieldMARC::ControlField, MARC::DataField

Turn it into an actual MARC field (control or data) Throw an error if called on a leader (LDR) line

Returns:

  • (MARC::ControlField, MARC::DataField)


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?

Returns:

  • (Boolean)


60
61
62
# File 'lib/marc_alephsequential/asline.rb', line 60

def valid_id?
  return VALID_ID.match(id) ? true : false
end