Sha256: cf8d25a8c6486beb982edd5b0cb0d1636bd895f7138ad9b6f7b40ebb212450c2

Contents?: true

Size: 1.69 KB

Versions: 3

Compression:

Stored size: 1.69 KB

Contents

require 'yaml'
require 'fileutils'
require 'tempfile'
require 'pathname'
require 'cjoiner/errors'

module Cjoiner
  module Helpers
    module Files
      # check if a file exists
      def file_exists(file, halt = true)
        check = ::File.exists? file
        raise(Cjoiner::Errors::FileNotFound, file) if !check and halt
        check
      end

      # load a yaml file
      def load_yaml(file)
        if file_exists(file)
          ::YAML::load_file(file)
        end
      end

      # move a file
      def move_file(from, to)
        return false if from == to
        FileUtils.mv from, to
      end

      # read file
      def read_file(file)
        File.read(file) if file_exists file
      end

      # write data to file
      def write_file(file, data)
        file.open("w") { |io| io.puts data }
      end

      # create a temporal file
      def temp_file(file, data)
        # due to some java+windows+cygwin limitation
        # we need to create our custom tempfile
        if on_windows
          name = "_cjoiner_tmp_#{file}"
          temp = File.new(name, "w")
          temp.write(data)
        else
          temp = Tempfile.new(file) << data
        end
        temp.close
        temp
      end

      # delete a file, can be File or String
      def delete_file(file)
        item = file.kind_of?(File) ? file.path : file
        File.delete item
      end

      # RUBY_PLATFORM tells cygwin or mswin
      def on_windows
        (RUBY_PLATFORM =~ /cygwin|mswin/) != nil
      end

      # return Pathname.new.expand_path
      def expand_path(file)
        Pathname.new(file).expand_path
      end

      # use Pathname
      def file(file)
        Pathname.new file
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cjoiner-1.6.1 lib/cjoiner/helpers.rb
cjoiner-1.6.0 lib/cjoiner/helpers.rb
cjoiner-1.5.2 lib/cjoiner/helpers.rb