Class: MARC4J4R::DataField
- Inherits:
-
Object
- Object
- MARC4J4R::DataField
- Includes:
- Enumerable
- Defined in:
- lib/marc4j4r/datafield.rb
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#[](code) ⇒ String
(also: #sub, #first)
Get the value of the first subfield of this field with the given code.
- #controlField? ⇒ Boolean
-
#each ⇒ Object
Iterate over the subfields.
-
#indicator1 ⇒ Object
(also: #ind1)
Get first indicator as a one-character string.
- #indicator1=(char) ⇒ Object (also: #ind1=)
-
#indicator2 ⇒ Object
(also: #ind2)
Get second indicator as a one-character string.
- #indicator2=(char) ⇒ Object (also: #ind2=)
-
#initialize(tag = nil, ind1 = ' ', ind2 = ' ') ⇒ DataField
constructor
A new instance of DataField.
-
#oldinit ⇒ Object
Override the initialize to allow creation with just a tag (marc4j only allows either no args or the tag and both indicators).
-
#sub_values(code = nil) ⇒ Array<String>
Get all values from the subfields for the given code or array of codes code is included in the given codes (or all subfields is code is empty).
-
#subs(code = false) ⇒ Array<MARC4J4R::SubField] The matching subfields, or an empty array
Get all subfields, optionally restricting to those with a given code.
-
#to_s(joiner = ' ') ⇒ Object
Pretty-print.
-
#value(joiner = ' ') ⇒ Object
Get the concatentated values of the subfields in order the appear in the field.
Constructor Details
#initialize(tag = nil, ind1 = ' ', ind2 = ' ') ⇒ DataField
Returns a new instance of DataField.
15 16 17 |
# File 'lib/marc4j4r/datafield.rb', line 15 def initialize(tag = nil, ind1 = ' ', ind2 = ' ') self.oldinit(tag, ind1[0].ord, ind2[0].ord) end |
Instance Method Details
#==(other) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/marc4j4r/datafield.rb', line 23 def == other basics = ((self.tag == other.tag) and (self.indicator1 == other.indicator1) and (self.indicator2 == other.indicator2)) unless basics # puts "Failed basics" return false end selfsubs = self.to_a othersubs = other.to_a # puts "#{self} vs #{other}" while (selfsubs.length > 0) ssf = selfsubs.shift osf = othersubs.shift unless ssf == osf # puts "#{ssf} <> #{osf}" return false end end if ((selfsubs.size > 0) or (othersubs.size > 0)) # puts "sizes unequal" return false end return true end |
#[](code) ⇒ String Also known as: sub, first
Get the value of the first subfield of this field with the given code
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/marc4j4r/datafield.rb', line 64 def [] code raise ArgumentError, "Code must be a one-character string, not #{code}" unless code.is_a? String and code.size == 1 # need to send a char value that the underlying java can deal with sub = self.getSubfield(code[0].ord) if (sub) return sub.getData else return nil end end |
#controlField? ⇒ Boolean
19 20 21 |
# File 'lib/marc4j4r/datafield.rb', line 19 def controlField? return false end |
#each ⇒ Object
Iterate over the subfields
138 139 140 141 142 |
# File 'lib/marc4j4r/datafield.rb', line 138 def each self.getSubfields.each do |s| yield s end end |
#indicator1 ⇒ Object Also known as: ind1
Get first indicator as a one-character string
115 116 117 |
# File 'lib/marc4j4r/datafield.rb', line 115 def indicator1 return self.getIndicator1.chr end |
#indicator1=(char) ⇒ Object Also known as: ind1=
124 125 126 |
# File 'lib/marc4j4r/datafield.rb', line 124 def indicator1= char self.setIndicator1 char[0].ord end |
#indicator2 ⇒ Object Also known as: ind2
Get second indicator as a one-character string
120 121 122 |
# File 'lib/marc4j4r/datafield.rb', line 120 def indicator2 return self.getIndicator2.chr end |
#indicator2=(char) ⇒ Object Also known as: ind2=
128 129 130 |
# File 'lib/marc4j4r/datafield.rb', line 128 def indicator2= char self.setIndicator2 char[0].ord end |
#oldinit ⇒ Object
Override the initialize to allow creation with just a tag (marc4j only allows either no args or the tag and both indicators)
14 |
# File 'lib/marc4j4r/datafield.rb', line 14 alias_method :oldinit, :initialize |
#sub_values(code = nil) ⇒ Array<String>
109 110 111 |
# File 'lib/marc4j4r/datafield.rb', line 109 def sub_values(code=nil) return self.subs(code).collect {|s| s.value} end |
#subs(code = false) ⇒ Array<MARC4J4R::SubField] The matching subfields, or an empty array
Get all subfields, optionally restricting to those with a given code
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/marc4j4r/datafield.rb', line 84 def subs code = false unless code return self.to_a end # Is it a singleton? unless code.is_a? Array code = [code] end return self.select {|s| code.include? s.code} end |
#to_s(joiner = ' ') ⇒ Object
Pretty-print
52 53 54 55 56 57 58 |
# File 'lib/marc4j4r/datafield.rb', line 52 def to_s (joiner = ' ') arr = [self.tag + ' ' + self.indicator1 + self.indicator2] self.each do |s| arr.push s.to_s end return arr.join(joiner) end |
#value(joiner = ' ') ⇒ Object
Get the concatentated values of the subfields in order the appear in the field
146 147 148 149 |
# File 'lib/marc4j4r/datafield.rb', line 146 def value joiner=' ' data = self.getSubfields.map {|s| s.data} return data.join(joiner) end |