Class: MARC::DataField
- 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 be in the range 010-999. 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
-
#indicator1 ⇒ Object
The first indicator.
-
#indicator2 ⇒ Object
The second indicator.
-
#subfields ⇒ Object
A list of MARC::Subfield objects.
-
#tag ⇒ Object
The tag for the field.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Two fields are equal if their tag, indicators and subfields are all equal.
-
#=~(regex) ⇒ Object
To support regex matching with fields.
-
#[](code) ⇒ Object
You can lookup subfields with this shorthand.
-
#append(subfield) ⇒ Object
Add a subfield (MARC::Subfield) to the field field.append(MARC::Subfield.new(‘a’,‘Dave Thomas’)).
-
#each ⇒ Object
You can iterate through the subfields in a Field: field.each {|s| print s}.
-
#initialize(tag, i1 = ' ', i2 = ' ', *subfields) ⇒ DataField
constructor
Create a new field with tag, indicators and subfields.
-
#to_s ⇒ Object
Returns a string representation of the field such as: 245 00 $aConsilience :$bthe unity of knowledge $cby Edward O.
-
#value ⇒ Object
to get the field as a string, without the tag and indicators useful in situations where you want a legible version of the field.
Methods included from Enumerable
#deep_clone, #deep_dup, #inject, #select_map
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.'] )
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 84 |
# File 'lib/marc/datafield.rb', line 52 def initialize(tag, i1=' ', i2=' ', *subfields) @tag = tag # 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 = [] # must use MARC::ControlField for tags < 010 if @tag.to_i < 10 and not @tag =~ /[A-z]/ raise MARC::Exception.new(), "MARC::DataField objects can't have tags < 010" end # 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" 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
#indicator1 ⇒ Object
The first indicator
28 29 30 |
# File 'lib/marc/datafield.rb', line 28 def indicator1 @indicator1 end |
#indicator2 ⇒ Object
The second indicator
31 32 33 |
# File 'lib/marc/datafield.rb', line 31 def indicator2 @indicator2 end |
#subfields ⇒ Object
A list of MARC::Subfield objects
34 35 36 |
# File 'lib/marc/datafield.rb', line 34 def subfields @subfields end |
#tag ⇒ Object
The tag for the field
25 26 27 |
# File 'lib/marc/datafield.rb', line 25 def tag @tag end |
Instance Method Details
#==(other) ⇒ Object
Two fields are equal if their tag, indicators and subfields are all equal.
130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/marc/datafield.rb', line 130 def ==(other) 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 return true end |
#=~(regex) ⇒ Object
To support regex matching with fields
if field =~ /Huckleberry/ ...
148 149 150 |
# File 'lib/marc/datafield.rb', line 148 def =~(regex) return self.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']
120 121 122 123 124 |
# File 'lib/marc/datafield.rb', line 120 def [](code) subfield = self.find {|s| s.code == code} return subfield.value if subfield return end |
#append(subfield) ⇒ Object
101 102 103 |
# File 'lib/marc/datafield.rb', line 101 def append(subfield) @subfields.push(subfield) end |
#each ⇒ Object
You can iterate through the subfields in a Field:
field.each {|s| print s}
109 110 111 112 113 |
# File 'lib/marc/datafield.rb', line 109 def each for subfield in subfields yield subfield end end |
#to_s ⇒ Object
Returns a string representation of the field such as:
245 00 $aConsilience :$bthe unity of knowledge $cby Edward O. Wilson.
90 91 92 93 94 95 |
# File 'lib/marc/datafield.rb', line 90 def to_s str = "#{tag} " str += "#{indicator1}#{indicator2} " @subfields.each { |subfield| str += subfield.to_s } return str end |