Class: MARCSpec::VariableFieldSpec

Inherits:
Object
  • Object
show all
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’)

Examples:

Get the $a from the 245s

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, codes = nil, joiner = ' ') ⇒ VariableFieldSpec

Get a new object

Parameters:

  • tag (String)

    The MARC field tag

  • codes (String, Array) (defaults to: nil)

    The list of subfield codes (as ‘abc’ or [‘a’, ‘b’, ‘c’]) whose values we want

  • joiner (String) (defaults to: ' ')

    What string to use to join the subfield values



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

#codehistoryObject

Returns the value of attribute codehistory.



19
20
21
# File 'lib/marcspec/variablefieldspec.rb', line 19

def codehistory
  @codehistory
end

#codesObject

Returns the value of attribute codes.



19
20
21
# File 'lib/marcspec/variablefieldspec.rb', line 19

def codes
  @codes
end

#ind1Object

Returns the value of attribute ind1.



19
20
21
# File 'lib/marcspec/variablefieldspec.rb', line 19

def ind1
  @ind1
end

#ind2Object

Returns the value of attribute ind2.



19
20
21
# File 'lib/marcspec/variablefieldspec.rb', line 19

def ind2
  @ind2
end

#joinerObject

Returns the value of attribute joiner.



19
20
21
# File 'lib/marcspec/variablefieldspec.rb', line 19

def joiner
  @joiner
end

#tagObject

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

Deprecated.

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

Parameters:

Returns:

  • (Boolean)

    whether or not it matches in all values



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

#asDSLStringString

Get a DSL snipped representing this object

Returns:

  • (String)

    the DSL string



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

#asPPStringObject

Deprecated.

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

Parameters:

  • r (MARC4J4R::Record)

    The record you want to extract values from

Returns:

  • (Array<String>)

    the extracted values, if any



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

Deprecated.

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