Class: MARC::FieldMap

Inherits:
Array
  • Object
show all
Defined in:
lib/marc/record.rb

Overview

The FieldMap is an Array of DataFields and Controlfields. It also contains a HashWithChecksumAttribute with a Hash-based representation of the fields for faster lookups

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (FieldMap) initialize

A new instance of FieldMap



17
18
19
# File 'lib/marc/record.rb', line 17

def initialize
  @tags = HashWithChecksumAttribute.new
end

Instance Attribute Details

- (Object) tags (readonly)

Returns the value of attribute tags



16
17
18
# File 'lib/marc/record.rb', line 16

def tags
  @tags
end

Instance Method Details

- (Object) each_by_tag(tags)

Returns an array of fields, in the order they appear, according to their tag. The tags argument can be a string (e.g. ‘245’), an array ([‘100’,’700’,’800’]) or a range ((‘600’..’699’)).



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/marc/record.rb', line 47

def each_by_tag(tags)
  reindex unless in_sync?
  matched_tags = []
  [*tags].each do |tag|
    next unless tag && @tags[tag]
    @tags[tag].each do |idx|
      matched_tags[idx] = self[idx]                  
    end
  end
  matched_tags.compact!
  matched_tags.each do |tag|
    yield tag
  end
  matched_tags
end

- (Boolean) in_sync?

Checks to see if the HashWithChecksumAttribute is in sync with the Array of fields

Returns:

  • (Boolean)


23
24
25
# File 'lib/marc/record.rb', line 23

def in_sync?
  @tags.checksum == self.hash
end

- (Object) reindex

Rebuild the HashWithChecksumAttribute with the current values of the fields Array



29
30
31
32
33
34
35
36
# File 'lib/marc/record.rb', line 29

def reindex
  @tags.clear
  self.each do |field|
    @tags[field.tag] ||= []
    @tags[field.tag] << self.index(field)
  end
  @tags.checksum = self.hash
end

- (Object) tag_list

Returns an array of all of the tags that appear in the record (not in the order they appear, however).



39
40
41
42
# File 'lib/marc/record.rb', line 39

def tag_list
  reindex unless in_sync?
  @tags.keys
end