Module: MARCExtensions::RecordExtensions

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

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.



71
72
73
# File 'lib/marc_extensions/record.rb', line 71

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:



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

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.



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

def each_control_field(&block)
  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:



54
55
56
# File 'lib/marc_extensions/record.rb', line 54

def each_data_field(&block)
  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:



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

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

#freezeObject

Freezes the leader and fields.



76
77
78
79
80
81
# File 'lib/marc_extensions/record.rb', line 76

def freeze
  leader.freeze
  fields.each(&:freeze)
  fields.freeze
  self
end

#frozen?Boolean

Returns true if the fields and leader are frozen.

Returns:

  • (Boolean)

    true if the fields and leader are frozen



84
85
86
# File 'lib/marc_extensions/record.rb', line 84

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

#record_idObject

TODO: use info from parsed documentation? or move to TIND-specific extension



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

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



99
100
101
# File 'lib/marc_extensions/record.rb', line 99

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