Sha256: 94ccb1d4a8ad87424a65891f42d781058a66d0960bf05ef6969b8dcc7bf4feb5

Contents?: true

Size: 1.91 KB

Versions: 2

Compression:

Stored size: 1.91 KB

Contents

require 'berkeley_library/marc/field_info/var_fields/ind_def'
require 'berkeley_library/marc/field_info/var_fields/subfield_def'
require 'berkeley_library/marc/field_info/var_fields/instrument_or_voices_code'
require 'berkeley_library/marc/field_info/var_fields/obsolescible'

module BerkeleyLibrary
  module Marc
    module FieldInfo
      module VarFields
        class VarFieldDef
          include Obsolescible

          attr_reader :tag, :desc, :indicators, :subfield_codes, :inst_or_voices_codes

          def initialize(tag:, desc:, indicators: [], subfield_codes: [], inst_or_voices_codes: [])
            @tag = tag
            @desc = desc
            @indicators = indicators # TODO: split out ind1/ind2 (but what about obsolete?)
            @subfield_codes = subfield_codes
            @inst_or_voices_codes = inst_or_voices_codes
          end

          def to_s
            blocks = ["#{tag} - #{desc}"]

            concat_indented(blocks, indicators, 'Indicators')
            concat_indented(blocks, subfield_codes, 'Subfield Codes')
            concat_indented(blocks, inst_or_voices_codes, 'Instrument or Voices Codes')

            blocks.join("\n")
          end

          def reject_obsoletes
            VarFieldDef.new(
              tag: tag,
              desc: desc,
              indicators: _reject_obsolete(indicators),
              subfield_codes: _reject_obsolete(subfield_codes),
              inst_or_voices_codes: _reject_obsolete(inst_or_voices_codes)
            )
          end

          private

          INDENT = '   '.freeze
          private_constant :INDENT

          def concat_indented(blocks, values, header)
            return if values.empty?

            blocks << INDENT + header
            values.each do |v|
              lines = v.to_s.lines(chomp: true)
              lines.each { |line| blocks << INDENT + INDENT + line }
            end
          end

        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
berkeley_library-marc-0.2.1 lib/berkeley_library/marc/field_info/var_fields/var_field_def.rb
berkeley_library-marc-0.2.0 lib/berkeley_library/marc/field_info/var_fields/var_field_def.rb