Class: MARCSpec::VariableFieldSpec
- Inherits:
-
Object
- Object
- MARCSpec::VariableFieldSpec
- Includes:
- JLogger::Simple
- Defined in:
- lib/marcspec/variablefieldspec.rb,
lib/marcspec/dsl.rb
Overview
A VariableFieldSpec has a tag (three chars) and a set of codes. Its #marc_values(r) method will return all the values for the subfields for the given codes joined by the optional joiner (space by default)
The subfield values are presented in the order they appear in the document, not the order the subfield codes are specified
vfs = MARCSpec::VariableFieldSpec.new('245', 'a') vfs = MARCSpec::VariableFieldSpec.new('245', 'ab') vfs = MARCSpec::VariableFieldSpec.new('245', ['a', 'b']) vfs = MARCSpec::VariableFieldSpec.new('245', 'a'..'b')
Instance Attribute Summary collapse
-
#codehistory ⇒ Object
Returns the value of attribute codehistory.
-
#codes ⇒ Object
Returns the value of attribute codes.
-
#ind1 ⇒ Object
Returns the value of attribute ind1.
-
#ind2 ⇒ Object
Returns the value of attribute ind2.
-
#joiner ⇒ Object
Returns the value of attribute joiner.
-
#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) ⇒ Boolean
Basic equality.
-
#asDSLString ⇒ String
Get a DSL snipped representing this object.
-
#asPPString ⇒ Object
deprecated
Deprecated.
Use the DSL
-
#initialize(tag, codes = nil, joiner = ' ') ⇒ VariableFieldSpec
constructor
Get a new object.
-
#marc_values(r) ⇒ Array<String>
Get the values associated with the tag (and optional subfield codes) for the given record.
-
#pretty_print(pp) ⇒ Object
deprecated
Deprecated.
Use the DSL
- #separator(s) ⇒ Object
- #sub(c) ⇒ Object (also: #subs)
Constructor Details
#initialize(tag, codes = nil, joiner = ' ') ⇒ VariableFieldSpec
Get a new object
29 30 31 32 33 34 |
# File 'lib/marcspec/variablefieldspec.rb', line 29 def initialize tag, codes=nil, joiner=' ' @tag = tag @joiner = joiner || ' ' self.codes = codes @codehistory = [] end |
Instance Attribute Details
#codehistory ⇒ Object
Returns the value of attribute codehistory.
20 21 22 |
# File 'lib/marcspec/variablefieldspec.rb', line 20 def codehistory @codehistory end |
#codes ⇒ Object
Returns the value of attribute codes.
20 21 22 |
# File 'lib/marcspec/variablefieldspec.rb', line 20 def codes @codes end |
#ind1 ⇒ Object
Returns the value of attribute ind1.
20 21 22 |
# File 'lib/marcspec/variablefieldspec.rb', line 20 def ind1 @ind1 end |
#ind2 ⇒ Object
Returns the value of attribute ind2.
20 21 22 |
# File 'lib/marcspec/variablefieldspec.rb', line 20 def ind2 @ind2 end |
#joiner ⇒ Object
Returns the value of attribute joiner.
20 21 22 |
# File 'lib/marcspec/variablefieldspec.rb', line 20 def joiner @joiner end |
#tag ⇒ Object
Returns the value of attribute tag.
20 21 22 |
# File 'lib/marcspec/variablefieldspec.rb', line 20 def tag @tag end |
Class Method Details
.fromPPString(str) ⇒ Object
Use the DSL
Create an object from an asPPString string
124 125 126 127 |
# File 'lib/marcspec/variablefieldspec.rb', line 124 def self.fromPPString str a = eval(str) return self.new(a[0], a[1], a[2]) end |
Instance Method Details
#==(other) ⇒ Boolean
Basic equality
40 41 42 43 44 |
# File 'lib/marcspec/variablefieldspec.rb', line 40 def == other return ((self.tag == other.tag) and (self.codes = other.codes) and (self.joiner = other.joiner)) end |
#asDSLString ⇒ String
Get a DSL snipped representing this object
92 93 94 95 96 97 98 99 100 |
# File 'lib/marcspec/variablefieldspec.rb', line 92 def asDSLString subs = @codes.join('') if subs.size > 0 # return "spec('#{@tag}') {subs '#{subs}'}" return "spec('#{tag}#{subs}')" else return "spec('#{@tag}')" end end |
#asPPString ⇒ Object
Use the DSL
Create a eval'able string of a hash version of this object
112 113 114 115 116 117 118 119 120 |
# File 'lib/marcspec/variablefieldspec.rb', line 112 def asPPString s = StringIO.new if @joiner and @joiner != ' ' PP.pp([@tag, @codes.join(''), @joiner], s) else PP.pp([@tag, @codes.join('')], s) end return s.string end |
#marc_values(r) ⇒ Array<String>
Get the values associated with the tag (and optional subfield codes) for the given record
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/marcspec/variablefieldspec.rb', line 76 def marc_values r fields = r.find_by_tag(@tag) vals = [] fields.each do |f| subvals = f.sub_values(@codes) subvals = subvals.join(@joiner) if subvals.size > 0 and (@codes.nil? or @codes.size > 1) vals << subvals end vals.flatten! return vals end |
#pretty_print(pp) ⇒ Object
Use the DSL
Print out hash version of this object
105 106 107 |
# File 'lib/marcspec/variablefieldspec.rb', line 105 def pretty_print pp pp.pp eval(self.asPPString) end |
#separator(s) ⇒ Object
130 131 132 133 |
# File 'lib/marcspec/dsl.rb', line 130 def separator s self.joiner = s return self end |
#sub(c) ⇒ Object Also known as: subs
125 126 127 128 |
# File 'lib/marcspec/dsl.rb', line 125 def sub c self.codes = c return self end |