Module: MARCExtensions::RecordExtensions

Included in:
MARC::Record
Defined in:
lib/marc_extensions/record.rb

Overview

Extensions to MARC::Record.

Instance Method Summary collapse

Instance Method Details

#data_fieldsArray<DataField>

Gets only the data fields (tag 010-999) from the record. (Note that this method does not protect against pathological records with control fields in the data field range.)

Returns:

  • (Array<DataField>)

    the data fields.



74
75
76
# File 'lib/marc_extensions/record.rb', line 74

def data_fields
  data_fields_by_tag.values.flatten
end

#data_fields_by_tagHash<String, Array<MARC::DataField>>

Gets the data fields from the record and groups them by tag.

Returns:



64
65
66
67
# File 'lib/marc_extensions/record.rb', line 64

def data_fields_by_tag
  # noinspection RubyYardReturnMatch,RubyMismatchedReturnType
  each_data_field.with_object({}) { |df, t2df| (t2df[df.tag] ||= []) << df }
end

#each_control_fieldEnumerator::Lazy<MARC::ControlField> #each_control_field {|field| ... } ⇒ Object

Gets only the control fields (tag 000-009) from the record. (Note that this method does not protect against pathological records with data fields in the control field range.)

Overloads:

  • #each_control_fieldEnumerator::Lazy<MARC::ControlField>

    An enumerator of the control fields.

    Returns:

    • (Enumerator::Lazy<MARC::ControlField>)

      the fields

  • #each_control_field {|field| ... } ⇒ Object

    Yields each control field.

    Yield Parameters:

    • field (MARC::ControlField)

      Each control field.



41
42
43
44
# File 'lib/marc_extensions/record.rb', line 41

def each_control_field(&block)
  # noinspection RubyMismatchedReturnType
  each_sorted_by_tag.take_while { |df| df.tag.to_i <= 10 }.each(&block)
end

#each_data_fieldEnumerator::Lazy<MARC::DataField> #each_data_field {|field| ... } ⇒ Object

Gets only the data fields (tag 010-999) from the record. (Note that this method does not protect against pathological records with control fields in the data field range.)

Overloads:

  • #each_data_fieldEnumerator::Lazy<MARC::DataField>

    An enumerator of the data fields.

    Returns:

  • #each_data_field {|field| ... } ⇒ Object

    Yields each data field.

    Yield Parameters:



56
57
58
59
# File 'lib/marc_extensions/record.rb', line 56

def each_data_field(&block)
  # noinspection RubyMismatchedReturnType
  each_sorted_by_tag.select { |df| df.tag.to_i > 10 }.each(&block)
end

#each_sorted_by_tag(tags) {|field| ... } ⇒ Object #each_sorted_by_tag(tags) ⇒ Enumerator::Lazy<MARC::ControlField, MARC::DataField> #each_sorted_by_tag {|field| ... } ⇒ Object #each_sorted_by_tagEnumerator::Lazy<MARC::ControlField, MARC::DataField>

Gets the specified fields in order by tag.

Overloads:

  • #each_sorted_by_tag(tags) {|field| ... } ⇒ Object

    Yields each specified field.

    Parameters:

    • tags (String, Enumerable<String>)

      A tag, range of tags, array of tags, or similar

    Yield Parameters:

  • #each_sorted_by_tag(tags) ⇒ Enumerator::Lazy<MARC::ControlField, MARC::DataField>

    An enumerator of the specified variable fields, sorted by tag.

    Parameters:

    • tags (String, Enumerable<String>)

      A tag, range of tags, array of tags, or similar

    Returns:

  • #each_sorted_by_tag {|field| ... } ⇒ Object

    Yields all fields, sorted by tag.

    Yield Parameters:

  • #each_sorted_by_tagEnumerator::Lazy<MARC::ControlField, MARC::DataField>

    An enumerator of all fields, sorted by tag.

    Returns:

See Also:



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

def each_sorted_by_tag(tags = nil, &block)
  @fields.each_sorted_by_tag(tags, &block)
end

#freezeMARC::Record

Recursively freezes this record, along with its leader and fields.

Returns:



80
81
82
83
84
85
86
# File 'lib/marc_extensions/record.rb', line 80

def freeze
  return if frozen?

  leader.freeze
  fields.freeze
  super
end

#frozen?Boolean

Whether this record, its fields, and leader are all frozen.

Returns:

  • (Boolean)

    true if the fields and leader are frozen



90
91
92
# File 'lib/marc_extensions/record.rb', line 90

def frozen?
  (fields.frozen? && leader.frozen?) && super
end

#record_idString?

Returns the canonical ID from the 001 control field.

Returns:

  • (String, nil)

    the 001 control field value, or nil if not present



96
97
98
99
# File 'lib/marc_extensions/record.rb', line 96

def record_id
  cf_001 = self['001']
  return cf_001.value if cf_001
end

#spec(query_str) ⇒ Array

Apply the provided MARCSpec query to this record.

Parameters:

  • query_str (String)

    A MARCSpec query string

Returns:

  • (Array)

    an array of the results of the query



106
107
108
# File 'lib/marc_extensions/record.rb', line 106

def spec(query_str)
  MARC::Spec.find(query_str, self)
end