Sha256: 859c92c4818ad340ed7a761d33c76f6307ec5f7d2810adff6bd7aec848a5026e

Contents?: true

Size: 1.32 KB

Versions: 73

Compression:

Stored size: 1.32 KB

Contents

module Flydata
  module Util
    module FileUtil
      def write_line(file_path, new_value)
        File.delete(file_path) if FileTest.exist?(file_path)
        File.open(file_path, 'w') do |out|
          out.write new_value
        end
      end

      def read_line(file_path, default_value = nil)
        ret = nil
        if FileTest.exist?(file_path)
          ret = File.readlines(file_path).first.to_s.strip
        end
        ret
      end

      # snip from https://gist.github.com/shaiguitar/6d926587e98fc8a5e301
      def tail(path, num_of_lines)
        file = File.open(path, "r")
        buffer_s = 512
        line_count = 0
        file.seek(0, IO::SEEK_END)

        offset = file.pos # we start at the end

        while line_count <= num_of_lines && offset > 0
          to_read = if (offset - buffer_s) < 0
                      offset
                    else
                      buffer_s
                    end

          file.seek(offset-to_read)
          data = file.read(to_read)

          data.reverse.each_char do |c|
            if line_count > num_of_lines
              offset += 1
              break
            end
            offset -= 1
            if c == "\n"
              line_count += 1
            end
          end
        end

        file.seek(offset)
        data = file.read
      end
    end
  end
end

Version data entries

73 entries across 73 versions & 1 rubygems

Version Path
flydata-0.8.10.2 lib/flydata/util/file_util.rb
flydata-0.8.10.1 lib/flydata/util/file_util.rb
flydata-0.8.9.11 lib/flydata/util/file_util.rb
flydata-0.8.10 lib/flydata/util/file_util.rb
flydata-0.8.9 lib/flydata/util/file_util.rb
flydata-0.8.8 lib/flydata/util/file_util.rb
flydata-0.8.7 lib/flydata/util/file_util.rb
flydata-0.8.6 lib/flydata/util/file_util.rb
flydata-0.8.5 lib/flydata/util/file_util.rb
flydata-0.8.4 lib/flydata/util/file_util.rb
flydata-0.8.3 lib/flydata/util/file_util.rb
flydata-0.8.2 lib/flydata/util/file_util.rb
flydata-0.8.1 lib/flydata/util/file_util.rb
flydata-0.8.0 lib/flydata/util/file_util.rb
flydata-0.7.19 lib/flydata/util/file_util.rb
flydata-0.7.18 lib/flydata/util/file_util.rb
flydata-0.7.17 lib/flydata/util/file_util.rb
flydata-0.7.16 lib/flydata/util/file_util.rb
flydata-0.7.15 lib/flydata/util/file_util.rb
flydata-0.7.14 lib/flydata/util/file_util.rb