Class: MARCSpec::VariableFieldSpec

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

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



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

#codehistoryObject

Returns the value of attribute codehistory.



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

def codehistory
  @codehistory
end

#codesObject

Returns the value of attribute codes.



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

def codes
  @codes
end

#ind1Object

Returns the value of attribute ind1.



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

def ind1
  @ind1
end

#ind2Object

Returns the value of attribute ind2.



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

def ind2
  @ind2
end

#joinerObject

Returns the value of attribute joiner.



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

def joiner
  @joiner
end

#tagObject

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

Deprecated.

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

Parameters:

Returns:

  • (Boolean)

    whether or not it matches in all values



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

#asDSLStringString

Get a DSL snipped representing this object

Returns:

  • (String)

    the DSL string



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

#asPPStringObject

Deprecated.

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

Parameters:

  • r (MARC4J4R::Record)

    The record you want to extract values from

Returns:

  • (Array<String>)

    the extracted values, if any



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

Deprecated.

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