Sha256: f587b976802c998743aeffca6c0670a3cc1bc45a0e45315f269ffc3af058b57f

Contents?: true

Size: 1.71 KB

Versions: 5

Compression:

Stored size: 1.71 KB

Contents

# Copyright (c) 2012, SoundCloud Ltd., Tobias Bielohlawek

# MassiveSitemap Writer
# The purpose of a writer is to store the written data, and to keep the state of existing data.
# It offers an API to which a builder can talk to, and a Interface which other writers have to implement
#
module MassiveSitemap
  module Writer
    class Base
      OPTS = {}

      def initialize(options = {})
        @options = self.class::OPTS.merge(options)
        @stream  = nil
      end

      #
      # API to which a builder talks to
      #
      # update wirter options, e.g. filename or overwrite behavior
      def set(options)
        @options.merge!(options)
        self
      end

      # init writer: try to open stream (e.g. file)
      def init!(options = {})
        set(options)
        if init?
          @stream = open_stream
        end
      end

      # close writer (e.g store file)
      def close!
        if inited?
          close_stream(@stream)
          @stream = nil
        end
      end

      # keep status of stream
      def inited?
        @stream
      end

      # write to stream
      def print(string)
        @stream.print(string) if inited?
      end

      def each(&block)
        stream_ids.each(&block)
      end

      def current
        stream_id
      end


      #
      # Interface which other writers have to implement
      #
      protected
      def open_stream
        @string ||= StringIO.new
      end

      def close_stream(stream)
      end

      # whether if stream can be inited, likely to throw an error
      # (e.g. on file existence)
      def init?
        true
      end

      def stream_ids
        @stream_ids ||= []
      end

      def stream_id
        nil
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
massive_sitemap-2.1.1 lib/massive_sitemap/writer/base.rb
massive_sitemap-2.1.0 lib/massive_sitemap/writer/base.rb
massive_sitemap-2.0.0 lib/massive_sitemap/writer/base.rb
massive_sitemap-2.0.0.rc8 lib/massive_sitemap/writer/base.rb
massive_sitemap-2.0.0.rc7 lib/massive_sitemap/writer/base.rb