Sha256: a81a500524e9833e3ed2f9526fc930f7313bb1eea50b2468c4ddd40cde10a4b0

Contents?: true

Size: 1.02 KB

Versions: 4

Compression:

Stored size: 1.02 KB

Contents

module DeadlySerious
  module Engine
    class FileChannel
      include Enumerable

      REGEXP = /\A>(.*?)\z/

      attr_reader :io_name

      def self.of_type(name)
        self if name.match(REGEXP)
      end

      def initialize(name, config)
        @io_name = self.class.io_name_for(name, config)
      end

      def each
        return enum_for(:each) unless block_given?
        open(io_name, 'r') { |file| file.each_line { |line| yield line } }
      end

      def <<(data)
        @writer ||= open(@io_name, 'w')
        @writer.print(data)
        self
      end

      def flush
        @writer.flush if @writer
      end

      def close
        @writer.close if @writer
      end

      def self.io_name_for(name, config)
        matcher = name.match(REGEXP)
        file_name = matcher[1]
        config.file_path_for(file_name)
      end

      def self.create(name, config)
        io_name = io_name_for(name, config)
        `touch '#{io_name}'` unless File.exist?(io_name)
        io_name
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
deadly_serious-2.0.0.pre.rc4 lib/deadly_serious/engine/channel/file_channel.rb
deadly_serious-2.0.0.pre.rc3 lib/deadly_serious/engine/channel/file_channel.rb
deadly_serious-2.0.0.pre.rc2 lib/deadly_serious/engine/channel/file_channel.rb
deadly_serious-2.0.0.pre.rc1 lib/deadly_serious/engine/channel/file_channel.rb