Class: MARC::DataField

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/marc/datafield.rb

Overview

MARC records contain data fields, each of which has a tag, indicators and subfields. Tags for data fields must are all three-character tags that are not control fields (generally, any numeric tag greater than 009).

Accessor attributes: tag, indicator1, indicator2

DataField mixes in Enumerable to enable access to it’s constituent Subfield objects. For instance, if you have a DataField representing a 856 tag, and want to find all ‘z’ subfields:

subfield_z = field.find_all {|subfield| subfield.code == 'z'}

Also, the accessor ‘subfields’ is an array of MARC::Subfield objects which can be accessed or modified by the client directly if neccesary.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, i1 = " ", i2 = " ", *subfields) ⇒ DataField

Create a new field with tag, indicators and subfields. Subfields are passed in as comma separated list of MARC::Subfield objects,

field = MARC::DataField.new('245','0','0',
  MARC::Subfield.new('a', 'Consilience :'),
  MARC::Subfield.new('b', 'the unity of knowledge '),
  MARC::Subfield.new('c', 'by Edward O. Wilson.'))

or using a shorthand:

field = MARC::DataField.new('245','0','0',
  ['a', 'Consilience :'],['b','the unity of knowledge '],
  ['c', 'by Edward O. Wilson.'] )


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/marc/datafield.rb', line 49

def initialize(tag, i1 = " ", i2 = " ", *subfields)
  # if the tag is less than 3 characters long and
  # the string is all numeric then we pad with zeros
  @tag = if (tag.length < 3) && (/^[0-9]+$/ =~ tag)
    "%03d" % tag
  else
    tag
  end
  # can't allow nil to be passed in or else it'll
  # screw us up later when we try to encode
  @indicator1 = i1.nil? ? " " : i1
  @indicator2 = i2.nil? ? " " : i2

  @subfields = []

  # allows MARC::Subfield objects to be passed directly
  # or a shorthand of ['a','Foo'], ['b','Bar']
  subfields.each do |subfield|
    case subfield
    when MARC::Subfield
      @subfields.push(subfield)
    when Array
      if subfield.length > 2
        raise MARC::Exception.new,
          "arrays must only have 2 elements: " + subfield.to_s
      end
      @subfields.push(
        MARC::Subfield.new(subfield[0], subfield[1])
      )
    else
      raise MARC::Exception.new,
        "invalid subfield type #{subfield.class}"
    end
  end
end

Instance Attribute Details

#indicator1Object

The first indicator



26
27
28
# File 'lib/marc/datafield.rb', line 26

def indicator1
  @indicator1
end

#indicator2Object

The second indicator



29
30
31
# File 'lib/marc/datafield.rb', line 29

def indicator2
  @indicator2
end

#subfieldsObject

A list of MARC::Subfield objects



32
33
34
# File 'lib/marc/datafield.rb', line 32

def subfields
  @subfields
end

#tagObject

The tag for the field



23
24
25
# File 'lib/marc/datafield.rb', line 23

def tag
  @tag
end

Instance Method Details

#==(other) ⇒ Object

Two fields are equal if their tag, indicators and subfields are all equal.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/marc/datafield.rb', line 168

def ==(other)
  if !other.is_a?(DataField)
    return false
  end
  if @tag != other.tag
    return false
  elsif @indicator1 != other.indicator1
    return false
  elsif @indicator2 != other.indicator2
    return false
  elsif @subfields != other.subfields
    return false
  end
  true
end

#=~(regex) ⇒ Object

To support regex matching with fields

if field =~ /Huckleberry/ ...


188
189
190
# File 'lib/marc/datafield.rb', line 188

def =~(regex)
  to_s =~ regex
end

#[](code) ⇒ Object

You can lookup subfields with this shorthand. Note it will return a string and not a MARC::Subfield object.

subfield = field['a']


153
154
155
156
157
# File 'lib/marc/datafield.rb', line 153

def [](code)
  subfield = find { |s| s.code == code }
  return subfield.value if subfield
  nil
end

#append(subfield) ⇒ Object

Add a subfield (MARC::Subfield) to the field

field.append(MARC::Subfield.new('a','Dave Thomas'))


132
133
134
# File 'lib/marc/datafield.rb', line 132

def append(subfield)
  @subfields.push(subfield)
end

#codes(dedup = true) ⇒ Object



159
160
161
162
163
# File 'lib/marc/datafield.rb', line 159

def codes(dedup = true)
  codes = []
  @subfields.each { |s| codes << s.code }
  dedup ? codes.uniq : codes
end

#eachObject

You can iterate through the subfields in a Field:

field.each {|s| print s}


139
140
141
142
143
# File 'lib/marc/datafield.rb', line 139

def each
  subfields.each do |subfield|
    yield subfield
  end
end

#errorsObject

Returns an array of validation errors



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/marc/datafield.rb', line 91

def errors
  messages = []

  # must use MARC::ControlField for tags < 010 or
  # those in MARC::ControlField#extra_control_fields

  if MARC::ControlField.control_tag?(@tag)
    messages << "MARC::DataField objects can't have ControlField tag '" + @tag + "'"
  end

  messages
end

#to_hashObject

Turn the variable field and subfields into a hash for MARC-in-JSON



121
122
123
124
125
126
127
# File 'lib/marc/datafield.rb', line 121

def to_hash
  field_hash = {@tag => {"ind1" => @indicator1, "ind2" => @indicator2, "subfields" => []}}
  each do |subfield|
    field_hash[@tag]["subfields"] << {subfield.code => subfield.value}
  end
  field_hash
end

#to_marchashObject

Turn into a marc-hash structure



115
116
117
# File 'lib/marc/datafield.rb', line 115

def to_marchash
  [@tag, @indicator1, @indicator2, @subfields.map { |sf| [sf.code, sf.value] }]
end

#to_sObject

Returns a string representation of the field such as:

245 00 $aConsilience :$bthe unity of knowledge $cby Edward O. Wilson.


107
108
109
110
111
112
# File 'lib/marc/datafield.rb', line 107

def to_s
  str = "#{tag} "
  str += "#{indicator1}#{indicator2} "
  @subfields.each { |subfield| str += subfield.to_s }
  str
end

#valid?Boolean

Returns true if there are no error messages associated with the field

Returns:

  • (Boolean)


86
87
88
# File 'lib/marc/datafield.rb', line 86

def valid?
  errors.none?
end

#valueObject

to get the field as a string, without the tag and indicators useful in situations where you want a legible version of the field

print record.value



197
198
199
# File 'lib/marc/datafield.rb', line 197

def value
  (@subfields.map { |s| s.value }.join "")
end