Method: MARC::DataField#initialize

Defined in:
lib/marc/datafield.rb

#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.'] )


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
84
85
86
87
88
89
90
91
# File 'lib/marc/datafield.rb', line 50

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
  if tag.length < 3 and /^[0-9]*$/ =~ tag
    @tag = "%03d" % tag
  else
    @tag = 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 = []

  # must use MARC::ControlField for tags < 010 or
  # those in MARC::ControlField#extra_control_fields
  
  if MARC::ControlField.control_tag?(@tag)
    raise MARC::Exception.new(),
      "MARC::DataField objects can't have ControlField tag '" + @tag + "')"
  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: " + 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