Sha256: 0d797e145c8b43f59b87de60785a20b8da51f06fff773d1f10702389acfa7a05

Contents?: true

Size: 1.13 KB

Versions: 8

Compression:

Stored size: 1.13 KB

Contents

require_relative "./document"

module Metacrunch
  module Mab2
    class Builder

      attr_reader :document

      def self.build(&block)
        builder = new
        document = builder.instance_eval(&block) if block_given?
        document || Document.new
      end

      def initialize
        @document = Document.new
      end

      def controlfield(tag, values)
        controlfield = Document::Controlfield.new(tag, values)
        @document.add_controlfield(controlfield)
        @document
      end

      def datafield(tag, ind1:nil, ind2:nil, &block)
        datafield = Document::Datafield.new(tag, ind1: ind1, ind2: ind2)
        @document.add_datafield(datafield)

        if block_given?
          subfield_builder = SubfieldBuilder.new(datafield)
          subfield_builder.instance_eval(&block)
        end

        @document
      end


      class SubfieldBuilder
        def initialize(datafield)
          @datafield = datafield
        end

        def subfield(code, value)
          subfield = Document::Subfield.new(code, value)
          @datafield.add_subfield(subfield)
          subfield
        end
      end

    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
metacrunch-mab2-1.3.1 lib/metacrunch/mab2/builder.rb
metacrunch-mab2-1.3.0 lib/metacrunch/mab2/builder.rb
metacrunch-mab2-1.2.1 lib/metacrunch/mab2/builder.rb
metacrunch-mab2-1.2.1-universal-java-1.8 lib/metacrunch/mab2/builder.rb
metacrunch-mab2-1.2.0 lib/metacrunch/mab2/builder.rb
metacrunch-mab2-1.1.2 lib/metacrunch/mab2/builder.rb
metacrunch-mab2-1.1.1 lib/metacrunch/mab2/builder.rb
metacrunch-mab2-1.1.0 lib/metacrunch/mab2/builder.rb