Class: MARC::SubfieldMap

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

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (SubfieldMap) initialize

A new instance of SubfieldMap



9
10
11
# File 'lib/marc/datafield.rb', line 9

def initialize
  @codes = HashWithChecksumAttribute.new    
end

Instance Attribute Details

- (Object) codes (readonly)

Returns the value of attribute codes



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

def codes
  @codes
end

Instance Method Details

- (Object) code_list



25
26
27
28
# File 'lib/marc/datafield.rb', line 25

def code_list
  reindex unless in_sync?
  @codes.keys
end

- (Object) each_by_code(codes)



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/marc/datafield.rb', line 30

def each_by_code(codes)
  reindex unless in_sync?
  matched_codes = []
  [*codes].each do |code|
    next unless code && @codes[code]
    @codes[code].each do |idx|
      matched_codes << self[idx]
      yield self[idx]
    end
  end
  matched_codes
end

- (Boolean) in_sync?

Returns:

  • (Boolean)


13
14
15
# File 'lib/marc/datafield.rb', line 13

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

- (Object) reindex



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

def reindex
  @codes.clear
  self.each do |subfield|
    @codes[subfield.code] ||= []
    @codes[subfield.code] << self.index(subfield)
  end
  @codes.checksum = self.hash
end