Class: Traject::MarcExtractor::Spec

Inherits:
Object
  • Object
show all
Defined in:
lib/traject/marc_extractor.rb

Overview

Represents a single specification for extracting data from a marc field, like "600abc" or "600|1*|x".

Includes the tag for reference, although this is redundant and not actually used in logic, since the tag is also implicit in the overall spec_hash with tag => [spec1, spec2]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Spec

Returns a new instance of Spec.



365
366
367
368
369
# File 'lib/traject/marc_extractor.rb', line 365

def initialize(hash = {})
  hash.each_pair do |key, value|
    self.send("#{key}=", value)
  end
end

Instance Attribute Details

#bytesObject

Returns the value of attribute bytes.



363
364
365
# File 'lib/traject/marc_extractor.rb', line 363

def bytes
  @bytes
end

#indicator1Object

Returns the value of attribute indicator1.



363
364
365
# File 'lib/traject/marc_extractor.rb', line 363

def indicator1
  @indicator1
end

#indicator2Object

Returns the value of attribute indicator2.



363
364
365
# File 'lib/traject/marc_extractor.rb', line 363

def indicator2
  @indicator2
end

#subfieldsObject

Returns the value of attribute subfields.



363
364
365
# File 'lib/traject/marc_extractor.rb', line 363

def subfields
  @subfields
end

#tagObject

Returns the value of attribute tag.



363
364
365
# File 'lib/traject/marc_extractor.rb', line 363

def tag
  @tag
end

Instance Method Details

#==(spec) ⇒ Object



398
399
400
401
402
403
404
405
406
# File 'lib/traject/marc_extractor.rb', line 398

def ==(spec)
  return false unless spec.kind_of?(Spec)

  return (self.tag == spec.tag) &&
    (self.subfields == spec.subfields) &&
    (self.indicator1 == spec.indicator1) &&
    (self.indicator1 == spec.indicator2) &&
    (self.bytes == spec.bytes)
end

#includes_subfield_code?(code) ⇒ Boolean

Pass in a string subfield code like 'a'; does this spec include it?

Returns:

  • (Boolean)


393
394
395
396
# File 'lib/traject/marc_extractor.rb', line 393

def includes_subfield_code?(code)
  # subfields nil means include them all
  self.subfields.nil? || self.subfields.include?(code)
end

#joinable?Boolean

Should subfields extracted by joined, if we have a seperator?

  • '630' no subfields specified => join all subfields
  • '630abc' multiple subfields specified = join all subfields
  • '633a' one subfield => do not join, return one value for each $a in the field
  • '633aa' one subfield, doubled => do join after all, will return a single string joining all the values of all the $a's.

Last case is handled implicitly at the moment when subfields == ['a', 'a']

Returns:

  • (Boolean)


379
380
381
# File 'lib/traject/marc_extractor.rb', line 379

def joinable?
  (self.subfields.nil? || self.subfields.size != 1)
end

#matches_indicators?(field) ⇒ Boolean

Pass in a MARC field, do it's indicators match indicators in this spec? nil indicators in spec mean we don't care, everything matches.

Returns:

  • (Boolean)


386
387
388
389
# File 'lib/traject/marc_extractor.rb', line 386

def matches_indicators?(field)
  return (self.indicator1.nil? || self.indicator1 == field.indicator1) &&
    (self.indicator2.nil? || self.indicator2 == field.indicator2)
end