Class: MARC::ControlField

Inherits:
Object
  • Object
show all
Defined in:
lib/marc/controlfield.rb

Overview

MARC records contain control fields, each of which has a tag and value. Tags for control fields must be in the 001-009 range or be specially added to the @@control_tags Set

Constant Summary collapse

@@control_tags =

Initially, control tags are the numbers 1 through 9 or the string ‘000’

Set.new(%w{000 001 002 003 004 005 006 007 008 009})

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, value = '') ⇒ ControlField

The constructor which must be passed a tag value and an optional value for the field.



32
33
34
35
36
37
38
# File 'lib/marc/controlfield.rb', line 32

def initialize(tag,value='')
  @tag = tag
  @value = value
  if not MARC::ControlField.control_tag?(@tag)
    raise MARC::Exception.new(), "tag must be in 001-009 or in the MARC::ControlField.control_tags set"
  end
end

Instance Attribute Details

#tagObject

the tag value (007, 008, etc)



24
25
26
# File 'lib/marc/controlfield.rb', line 24

def tag
  @tag
end

#valueObject

the value of the control field



27
28
29
# File 'lib/marc/controlfield.rb', line 27

def value
  @value
end

Class Method Details

.control_tag?(tag) ⇒ Boolean

A tag is a control tag if tag.to_s is a member of the @@control_tags set.

Returns:

  • (Boolean)


19
20
21
# File 'lib/marc/controlfield.rb', line 19

def self.control_tag?(tag)
  return @@control_tags.include? tag.to_s
end

.control_tagsObject



14
15
16
# File 'lib/marc/controlfield.rb', line 14

def self.control_tags
  return @@control_tags
end

Instance Method Details

#==(other) ⇒ Object

Two control fields are equal if their tags and values are equal.



42
43
44
45
46
47
48
49
# File 'lib/marc/controlfield.rb', line 42

def ==(other)
  if @tag != other.tag
    return false 
  elsif @value != other.value
    return false
  end
  return true
end

#=~(regex) ⇒ Object



65
66
67
# File 'lib/marc/controlfield.rb', line 65

def =~(regex)
  return self.to_s =~ regex
end

#to_hashObject

Turn the control field into a hash for MARC-in-JSON



57
58
59
# File 'lib/marc/controlfield.rb', line 57

def to_hash
  return {@tag=>@value}
end

#to_marchashObject

turning it into a marc-hash element



52
53
54
# File 'lib/marc/controlfield.rb', line 52

def to_marchash
  return [@tag, @value]
end

#to_sObject



61
62
63
# File 'lib/marc/controlfield.rb', line 61

def to_s
  return "#{tag} #{value}" 
end