Class: MARCSpec::VariableFieldSpec
- Inherits:
-
Object
- Object
- MARCSpec::VariableFieldSpec
- Includes:
- Logback::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® 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
- #sub(c) ⇒ Object (also: #subs)
Constructor Details
#initialize(tag, codes = nil, joiner = ' ') ⇒ VariableFieldSpec
Get a new object
28 29 30 31 32 33 |
# File 'lib/marcspec/variablefieldspec.rb', line 28 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.
19 20 21 |
# File 'lib/marcspec/variablefieldspec.rb', line 19 def codehistory @codehistory end |
#codes ⇒ Object
Returns the value of attribute codes.
19 20 21 |
# File 'lib/marcspec/variablefieldspec.rb', line 19 def codes @codes end |
#ind1 ⇒ Object
Returns the value of attribute ind1.
19 20 21 |
# File 'lib/marcspec/variablefieldspec.rb', line 19 def ind1 @ind1 end |
#ind2 ⇒ Object
Returns the value of attribute ind2.
19 20 21 |
# File 'lib/marcspec/variablefieldspec.rb', line 19 def ind2 @ind2 end |
#joiner ⇒ Object
Returns the value of attribute joiner.
19 20 21 |
# File 'lib/marcspec/variablefieldspec.rb', line 19 def joiner @joiner end |
#tag ⇒ Object
Returns the value of attribute tag.
19 20 21 |
# File 'lib/marcspec/variablefieldspec.rb', line 19 def tag @tag end |
Class Method Details
.fromPPString(str) ⇒ Object
Use the DSL
Create an object from an asPPString string
123 124 125 126 |
# File 'lib/marcspec/variablefieldspec.rb', line 123 def self.fromPPString str a = eval(str) return self.new(a[0], a[1], a[2]) end |
Instance Method Details
#==(other) ⇒ Boolean
Basic equality
39 40 41 42 43 |
# File 'lib/marcspec/variablefieldspec.rb', line 39 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
91 92 93 94 95 96 97 98 99 |
# File 'lib/marcspec/variablefieldspec.rb', line 91 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
111 112 113 114 115 116 117 118 119 |
# File 'lib/marcspec/variablefieldspec.rb', line 111 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
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/marcspec/variablefieldspec.rb', line 75 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
104 105 106 |
# File 'lib/marcspec/variablefieldspec.rb', line 104 def pretty_print pp pp.pp eval(self.asPPString) 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 |