Sha256: f0d28e7465d53249fce6bc5e455a874fe1d5bfc23484f84d632669b12e1819ff

Contents?: true

Size: 844 Bytes

Versions: 1

Compression:

Stored size: 844 Bytes

Contents

module JCukeForker
  class NormalisedEncodingFile
    COMMENT_OR_EMPTY_LINE_PATTERN = /^\s*#|^\s*$/ #:nodoc:
    ENCODING_PATTERN = /^\s*#\s*encoding\s*:\s*([^\s]+)/ #:nodoc:

    def self.read(path)
      new(path).read
    end

    def initialize(path)
      begin
        @file = File.new(path)
        set_encoding
      rescue Errno::EACCES => e
        raise FileNotFoundException.new(e, File.expand_path(path))
      rescue Errno::ENOENT => e
        raise FeatureFolderNotFoundException.new(e, path)
      end
    end

    def read
      @file.read.encode("UTF-8")
    end

    private

    def set_encoding
      @file.each do |line|
        if ENCODING_PATTERN =~ line
          @file.set_encoding $1
          break
        end
        break unless COMMENT_OR_EMPTY_LINE_PATTERN =~ line
      end
      @file.rewind
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jcukeforker-0.4.0 lib/jcukeforker/normalised_encoding_file.rb