Class: MARC::FieldMap
- Inherits:
-
Array
- Object
- Array
- MARC::FieldMap
- 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
-
#clean ⇒ Object
Returns the value of attribute clean.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Instance Method Summary collapse
-
#each_by_tag(tags) ⇒ Object
Returns an array of fields, in the order they appear, according to their tag.
-
#initialize ⇒ FieldMap
constructor
A new instance of FieldMap.
-
#reindex ⇒ Object
Rebuild the HashWithChecksumAttribute with the current values of the fields Array.
-
#tag_list ⇒ Object
Returns an array of all of the tags that appear in the record (not in the order they appear, however).
Constructor Details
#initialize ⇒ FieldMap
Returns a new instance of FieldMap.
9 10 11 12 |
# File 'lib/marc/record.rb', line 9 def initialize @tags = {} @clean = true end |
Instance Attribute Details
#clean ⇒ Object
Returns the value of attribute clean.
8 9 10 |
# File 'lib/marc/record.rb', line 8 def clean @clean end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
7 8 9 |
# File 'lib/marc/record.rb', line 7 def @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’)).
34 35 36 37 38 39 40 41 |
# File 'lib/marc/record.rb', line 34 def each_by_tag() reindex unless @clean indices = @tags.values_at(*(@tags.keys & [*])).flatten.sort return [] if indices.empty? self.values_at(*indices).each do |tag| yield tag end end |
#reindex ⇒ Object
Rebuild the HashWithChecksumAttribute with the current values of the fields Array
16 17 18 19 20 21 22 23 |
# File 'lib/marc/record.rb', line 16 def reindex @tags = {} self.each_with_index do |field, i| @tags[field.tag] ||= [] @tags[field.tag] << i end @clean = true end |
#tag_list ⇒ Object
Returns an array of all of the tags that appear in the record (not in the order they appear, however).
26 27 28 29 |
# File 'lib/marc/record.rb', line 26 def tag_list reindex unless @clean @tags.keys end |