Sha256: a43e73840094cbfff734d75e8dd6857d94c8010b67794c893548cef2069024ce

Contents?: true

Size: 887 Bytes

Versions: 22

Compression:

Stored size: 887 Bytes

Contents

require 'fileutils'

# Util methods for processing file.
module FileHelper
  # Create file with at given path with given content. Ensure parent directories
  # exist.
  #
  # path    - An String path of file.
  # content - An String or an Array of Strings content of file.
  def create_file(path, content = '')
    file_path = File.expand_path(path)
    dir_path = File.dirname(file_path)
    FileUtils.makedirs(dir_path) unless File.exist?(dir_path)
    write_file(path, content)
  end

  # Write file content at given path.
  #
  # path    - An String path of file.
  # content - An String or an Array of Strings content of file.
  def write_file(path, content)
    File.open(path, 'w') do |file|
      case content
      when ''
        # Create empty file.
      when String
        file.puts content
      when Array
        file.puts content.join("\n")
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
fasterer-0.11.0 spec/support/file_helper.rb
fasterer-0.10.1 spec/support/file_helper.rb
fasterer-0.10.0 spec/support/file_helper.rb
fasterer-0.9.0 spec/support/file_helper.rb
fasterer-0.8.3 spec/support/file_helper.rb
fasterer-0.8.2 spec/support/file_helper.rb
fasterer-0.8.1 spec/support/file_helper.rb
fasterer-0.8.0 spec/support/file_helper.rb
fasterer-0.7.1 spec/support/file_helper.rb
fasterer-0.7.0 spec/support/file_helper.rb
fasterer-0.6.0 spec/support/file_helper.rb
fasterer-0.5.1 spec/support/file_helper.rb
fasterer-0.5.0 spec/support/file_helper.rb
fasterer-0.4.2 spec/support/file_helper.rb
fasterer-0.4.1 spec/support/file_helper.rb
fasterer-0.4.0 spec/support/file_helper.rb
fasterer-0.3.2 spec/support/file_helper.rb
fasterer-0.3.1 spec/support/file_helper.rb
fasterer-0.3.0 spec/support/file_helper.rb
fasterer-0.2.1 spec/support/file_helper.rb