lib/massive_sitemap/writer/base.rb in massive_sitemap-2.0.0.rc4 vs lib/massive_sitemap/writer/base.rb in massive_sitemap-2.0.0.rc5

- old
+ new

@@ -1,71 +1,84 @@ - +# 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 = {} - attr_reader :options - def initialize(options = {}) @options = self.class::OPTS.merge(options) @stream = nil end - # API - def init!(options = {}) - close! + # + # 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) - streams.each(&block) + stream_ids.each(&block) end def current - stream + stream_id end - # def flush! - # @streams = [] - # end - # Interface + # + # 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 streams - @streams ||= [] + def stream_ids + @stream_ids ||= [] end - def stream + def stream_id nil end end end