Sha256: f56194c0fb4f9eddfd15be1a0e589e3e7cde09da9fbc37c761b301e74f7142a2

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

module Metacrunch
  module Marcxml
    class Document
      class Datafield

        attr_reader :tag
        attr_reader :ind1, :ind2

        def initialize(tag, ind1:nil, ind2:nil)
          @tag  = tag
          @ind1 = ind1
          @ind2 = ind2
          @subfields_map = {}
        end

        #
        # Returns the sub fields matching the given code.
        #
        # @param [String, nil, Array<String>] code of the sub field
        # @return [Metacrunch::Marcxml::Document::SubfieldSet] sub field with the given code. The set
        #  is empty if the sub field doesn't exists.
        #
        def subfields(code = nil)
          matched_subfields = if code.nil?
            @subfields_map.values.flatten(1)
          else
            if (codes = code).is_a?(Array)
              codes.map{ |_code| @subfields_map[_code] }.compact.flatten(1)
            else
              @subfields_map[code]
            end
          end

          SubfieldSet.new(matched_subfields)
        end

        #
        # Adds a new sub field.
        #
        # @param [Metacrunch::Marcxml::Document::Subfield] subfield
        #
        def add_subfield(subfield)
          (@subfields_map[subfield.code] ||= []) << subfield
          subfield
        end

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
metacrunch-marcxml-3.0.0 lib/metacrunch/marcxml/document/datafield.rb
metacrunch-marcxml-2.0.0 lib/metacrunch/marcxml/document/datafield.rb