class MARCSpec::ControlFieldSpec
A ControlFieldSpec
takes a control tag (generally 001..009) and an optional zero-based range When called with marc_values
(record), it returns either the complete value of all occurances of the field in question (in the order they appear in the record), or the zero-based substrings based on the passed range.
@example Get the whole 001 cfs = MARCSpec::ControlTagSpec.new('001')
@example Get the first three characters of the 008 cfs = MARCSpec::ControlTagSpec.new('001', 0..2)
Note that the use of the zero-based range in this manner conforms to the way MARC substrings are specified.
Attributes
Public Class Methods
Recreate from an asPPString call @deprecated Use the DSL
# File lib/marcspec/controlfieldspec.rb, line 116 def self.fromPPString str a = eval(str) return self.new(*a) end
# File lib/marcspec/controlfieldspec.rb, line 26 def initialize (tag, range=nil) unless MARC4J4R::ControlField.control_tag? tag raise ArgumentError, "Tag must be a control tag" end @tag = tag self.range = range @rangehistory = [] end
Public Instance Methods
# File lib/marcspec/controlfieldspec.rb, line 35 def == other return ((self.tag == other.tag) and (self.range == other.range)) end
Print out as a DSL segment
# File lib/marcspec/controlfieldspec.rb, line 92 def asDSLString if (@range) return "spec('#{@tag}') {chars #{@range}}" else return "spec('#{@tag}')" end end
Print out as a ruby hash. @deprecated Use the DSL
# File lib/marcspec/controlfieldspec.rb, line 103 def asPPString s = StringIO.new if @range PP.pp([@tag, @range], s) else PP.pp([@tag], s) end return s.string end
# File lib/marcspec/dsl.rb, line 116 def char c self.range = c return self end
# File lib/marcspec/controlfieldspec.rb, line 75 def marc_values r vals = r.find_by_tag(@tag).map {|f| f.value} if @range return vals.map {|v| v[@range]} else return vals end end
Print it out has a ruby hash @deprecated Use the DSL
# File lib/marcspec/controlfieldspec.rb, line 87 def pretty_print pp pp.pp eval(self.asPPString) end
Set the range of characters to use (nil for all)
Always force a real range, since in Ruby 1.9 a string subscript with a single fixnum will return the character code of that character (e.g., “Bill” => 66, wherease “Bill” gives the expected 'B'
@param [nil, Fixnum, Range] range A zero-based substring range or character position @return [MARCSpec::ControlFieldSpec] self
# File lib/marcspec/controlfieldspec.rb, line 49 def range= range @rangehistory << @range if @range if range.nil? @range = nil return self end if range.is_a? Fixnum if range < 0 raise ArgumentError, "Range must be nil, an integer offset (1-based), or a Range, not #{range}" end @range = range..range elsif range.is_a? Range if range.begin < 1 or range.begin > range.end raise ArgumentError "Range must be one-based, with the start before the end, not #{range}" else @range = range end else raise ArgumentError, "Range must be nil, an integer offset (1-based), or a Range, not #{range.inspect}" end return self end