Class: MARC::ControlField
- Inherits:
-
Object
- Object
- MARC::ControlField
- 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
Set.new(%w[000 001 002 003 004 005 006 007 008 009])
Instance Attribute Summary collapse
-
#tag ⇒ Object
the tag value (007, 008, etc).
-
#value ⇒ Object
the value of the control field.
Class Method Summary collapse
-
.control_tag?(tag) ⇒ Boolean
A tag is a control tag if tag.to_s is a member of the @@control_tags set.
- .control_tags ⇒ Object
Instance Method Summary collapse
-
#==(other) ⇒ Object
Two control fields are equal if their tags and values are equal.
- #=~(regex) ⇒ Object
-
#errors ⇒ Object
Returns an array of validation errors.
-
#initialize(tag, value = "") ⇒ ControlField
constructor
The constructor which must be passed a tag value and an optional value for the field.
-
#to_hash ⇒ Object
Turn the control field into a hash for MARC-in-JSON.
-
#to_marchash ⇒ Object
turning it into a marc-hash element.
- #to_s ⇒ Object
-
#valid? ⇒ Boolean
Returns true if there are no error messages associated with the field.
Constructor Details
#initialize(tag, value = "") ⇒ ControlField
The constructor which must be passed a tag value and an optional value for the field.
30 31 32 33 |
# File 'lib/marc/controlfield.rb', line 30 def initialize(tag, value = "") @tag = tag @value = value end |
Instance Attribute Details
#tag ⇒ Object
the tag value (007, 008, etc)
22 23 24 |
# File 'lib/marc/controlfield.rb', line 22 def tag @tag end |
#value ⇒ Object
the value of the control field
25 26 27 |
# File 'lib/marc/controlfield.rb', line 25 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.
17 18 19 |
# File 'lib/marc/controlfield.rb', line 17 def self.control_tag?(tag) @@control_tags.include? tag.to_s end |
.control_tags ⇒ Object
12 13 14 |
# File 'lib/marc/controlfield.rb', line 12 def self. @@control_tags end |
Instance Method Details
#==(other) ⇒ Object
Two control fields are equal if their tags and values are equal.
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/marc/controlfield.rb', line 53 def ==(other) if !other.is_a?(ControlField) return false end if @tag != other.tag return false elsif @value != other.value return false end true end |
#=~(regex) ⇒ Object
79 80 81 |
# File 'lib/marc/controlfield.rb', line 79 def =~(regex) to_s =~ regex end |
#errors ⇒ Object
Returns an array of validation errors
41 42 43 44 45 46 47 48 49 |
# File 'lib/marc/controlfield.rb', line 41 def errors = [] unless MARC::ControlField.control_tag?(@tag) << "tag #{@tag.inspect} must be in 001-009 or in the MARC::ControlField.control_tags set" end end |
#to_hash ⇒ Object
Turn the control field into a hash for MARC-in-JSON
71 72 73 |
# File 'lib/marc/controlfield.rb', line 71 def to_hash {@tag => @value} end |
#to_marchash ⇒ Object
turning it into a marc-hash element
66 67 68 |
# File 'lib/marc/controlfield.rb', line 66 def to_marchash [@tag, @value] end |
#to_s ⇒ Object
75 76 77 |
# File 'lib/marc/controlfield.rb', line 75 def to_s "#{tag} #{value}" end |
#valid? ⇒ Boolean
Returns true if there are no error messages associated with the field
36 37 38 |
# File 'lib/marc/controlfield.rb', line 36 def valid? errors.none? end |