Class: MARCSpec::ControlFieldSpec
- Inherits:
-
Object
- Object
- MARCSpec::ControlFieldSpec
- Includes:
- JLogger::Simple
- Defined in:
- lib/marcspec/controlfieldspec.rb,
lib/marcspec/dsl.rb
Overview
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.
cfs = MARCSpec::ControlTagSpec.new('001')
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.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#range ⇒ Object
Returns the value of attribute range.
-
#rangehistory ⇒ Object
Returns the value of attribute rangehistory.
-
#tag ⇒ Object
Returns the value of attribute tag.
Class Method Summary collapse
-
.fromPPString(str) ⇒ Object
deprecated
Deprecated.
Use the DSL
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#asDSLString ⇒ Object
Print out as a DSL segment.
-
#asPPString ⇒ Object
deprecated
Deprecated.
Use the DSL
- #char(c) ⇒ Object (also: #chars)
-
#initialize(tag, range = nil) ⇒ ControlFieldSpec
constructor
A new instance of ControlFieldSpec.
- #marc_values(r) ⇒ Object
-
#pretty_print(pp) ⇒ Object
deprecated
Deprecated.
Use the DSL
Constructor Details
#initialize(tag, range = nil) ⇒ ControlFieldSpec
Returns a new instance of ControlFieldSpec.
26 27 28 29 30 31 32 33 |
# 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 |
Instance Attribute Details
#range ⇒ Object
Returns the value of attribute range.
24 25 26 |
# File 'lib/marcspec/controlfieldspec.rb', line 24 def range @range end |
#rangehistory ⇒ Object
Returns the value of attribute rangehistory.
24 25 26 |
# File 'lib/marcspec/controlfieldspec.rb', line 24 def rangehistory @rangehistory end |
#tag ⇒ Object
Returns the value of attribute tag.
24 25 26 |
# File 'lib/marcspec/controlfieldspec.rb', line 24 def tag @tag end |
Class Method Details
.fromPPString(str) ⇒ Object
Use the DSL
Recreate from an asPPString call
116 117 118 119 |
# File 'lib/marcspec/controlfieldspec.rb', line 116 def self.fromPPString str a = eval(str) return self.new(*a) end |
Instance Method Details
#==(other) ⇒ Object
35 36 37 38 |
# File 'lib/marcspec/controlfieldspec.rb', line 35 def == other return ((self.tag == other.tag) and (self.range == other.range)) end |
#asDSLString ⇒ Object
Print out as a DSL segment
92 93 94 95 96 97 98 |
# File 'lib/marcspec/controlfieldspec.rb', line 92 def asDSLString if (@range) return "spec('#{@tag}') {chars #{@range}}" else return "spec('#{@tag}')" end end |
#asPPString ⇒ Object
Use the DSL
Print out as a ruby hash.
103 104 105 106 107 108 109 110 111 |
# 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 |
#char(c) ⇒ Object Also known as: chars
116 117 118 119 |
# File 'lib/marcspec/dsl.rb', line 116 def char c self.range = c return self end |
#marc_values(r) ⇒ Object
75 76 77 78 79 80 81 82 |
# 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 |
#pretty_print(pp) ⇒ Object
Use the DSL
Print it out has a ruby hash
87 88 89 |
# File 'lib/marcspec/controlfieldspec.rb', line 87 def pretty_print pp pp.pp eval(self.asPPString) end |