Class: MARC::AlephSequential::ASLineGroup

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

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

log, #log, log=

Constructor Details

#initializeASLineGroup

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

#aslinesArray<MARC::Field>

Returns Internal list of MARC field object.

Returns:

  • (Array<MARC::Field>)

    Internal list of MARC field object


18
19
20
# File 'lib/marc_alephsequential/asline_group.rb', line 18

def aslines
  @aslines
end

#leaderObject (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.

Returns:

  • (Undefined)

    side effect only

Raises:

  • MARC::AlephSequential::Error when there's an invalid ID and there's no previous


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_recordMARC::Record Also known as: to_record

Turn this object into a MARC::Record

Returns:

  • (MARC::Record)

Raises:

  • MARC::AlephSequential::Error if this object is empty

  • MARC::AlephSequential::Error if there's no leader


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?

Returns:

  • (Boolean)

37
38
39
# File 'lib/marc_alephsequential/asline_group.rb', line 37

def empty?
  aslines.empty?
end

#sizeObject

Number of aslines already added

Returns:

  • Integer


32
33
34
# File 'lib/marc_alephsequential/asline_group.rb', line 32

def size
  aslines.size
end