Class: RMARC::DataField

Inherits:
VariableField show all
Includes:
Enumerable
Defined in:
lib/rmarc/model/data_field.rb

Overview

This class represents a data field in a MARC record.

The module Enumerable is included to enable searching within subfields using each.

To iterate over all subfields:

field.each { |s| print s }

To retrieve subfield with code ‘a’:

field.find {|s| s.code == 'a'}

To retrieve subfields with code ‘a’ through ‘c’:

field.find_all {|s| ('a'..'c' === s.code)}

Instance Attribute Summary collapse

Attributes inherited from VariableField

#tag

Instance Method Summary collapse

Constructor Details

#initialize(tag, ind1, ind2) ⇒ DataField

Returns a new instance of DataField.



47
48
49
50
51
52
# File 'lib/rmarc/model/data_field.rb', line 47

def initialize(tag, ind1, ind2)
  super(tag)
  @ind1 = ind1
  @ind2 = ind2
  @subfields = Array.new
end

Instance Attribute Details

#ind1Object

Returns the value of attribute ind1.



45
46
47
# File 'lib/rmarc/model/data_field.rb', line 45

def ind1
  @ind1
end

#ind2Object

Returns the value of attribute ind2.



45
46
47
# File 'lib/rmarc/model/data_field.rb', line 45

def ind2
  @ind2
end

#subfieldsObject

Returns the value of attribute subfields.



45
46
47
# File 'lib/rmarc/model/data_field.rb', line 45

def subfields
  @subfields
end

Instance Method Details

#add(subf) ⇒ Object

Adds a subfield.



55
56
57
# File 'lib/rmarc/model/data_field.rb', line 55

def add(subf)
  @subfields.push(subf)
end

#eachObject

Implements Enumarable.each to provide an iterator over all subfields.



66
67
68
69
70
# File 'lib/rmarc/model/data_field.rb', line 66

def each
  for subfield in @subfields
    yield subfield
  end
end

#remove(subf) ⇒ Object

Removes a subfield



60
61
62
# File 'lib/rmarc/model/data_field.rb', line 60

def remove(subf)
  @subfields.delete(subf)
end

#to_sObject

Returns a string representation of the data field.

Example:

100 1 $aChabon, Michael.


77
78
79
80
81
82
# File 'lib/rmarc/model/data_field.rb', line 77

def to_s
  a = "" 
  a << super << @ind1 << @ind2
  subfields.each { |s| a << s.to_s }
  return a
end