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 Hash representation of the fields for faster lookups (under certain conditions)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFieldMap

Returns a new instance of FieldMap.



10
11
12
13
# File 'lib/marc/record.rb', line 10

def initialize
  @tags  = {}
  @clean = true
end

Instance Attribute Details

#cleanObject

Returns the value of attribute clean.



8
9
10
# File 'lib/marc/record.rb', line 8

def clean
  @clean
end

#tagsObject (readonly)

Returns the value of attribute tags.



7
8
9
# File 'lib/marc/record.rb', line 7

def tags
  @tags
end

Instance Method Details

#each_by_tag(tags) ⇒ Object

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’)).



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/marc/record.rb', line 36

def each_by_tag(tags)
  reindex unless @clean
  indices = []
  # Get all the indices associated with the tags
  Array(tags).each do |t|
    indices.concat @tags[t] if @tags[t]
  end

  # Remove any nils
  indices.compact!
  return [] if indices.empty?

 # Sort it, so we get the fields back in the order they appear in the record
  indices.sort!

  indices.each do |tag|
    yield self[tag]
  end
end

#freezeObject

Freeze for immutability, first reindexing if needed. A frozen FieldMap is safe for concurrent access, and also can more easily avoid accidental reindexing on even read-only use.



61
62
63
64
# File 'lib/marc/record.rb', line 61

def freeze
  self.reindex unless @clean
  super
end

#reindexObject

Rebuild the HashWithChecksumAttribute with the current values of the fields Array



17
18
19
20
21
22
23
24
# File 'lib/marc/record.rb', line 17

def reindex
  @tags = {}
  self.each_with_index do |field, i|
    @tags[field.tag] ||= []
    @tags[field.tag] << i
  end
  @clean = true
end

#tag_listObject

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



27
28
29
30
# File 'lib/marc/record.rb', line 27

def tag_list
  reindex unless @clean
  @tags.keys
end