module SaveFile

#

This is the class-variant from which you can subclass, if you need a specialized class that already has this functionality.

#

require ‘save_file/base/base.rb’

#
#

This file holds the constants that we will use in the save_file gem.

#
#

This file holds the main module of SaveFile. It requires the file constants.rb.

#

require ‘save_file/module/save_file.rb’

#
#

Constants

DEFAULT_PERMISSION_TO_USE
FILE_MODE_TO_BE_USED
#

Specify the file mode to be used here.

#
SAVE_FILE_WHERE_TO
#

SAVE_FILE_WHERE_TO

#
VERSION
#

VERSION

#

Public Class Methods

append_what_into( what = "Testing.\n", into_where = 'default.txt', permission_to_use = ::SaveFile::DEFAULT_PERMISSION_TO_USE ) click to toggle source
#

SaveFile.append_what_into

#
# File lib/save_file/module/save_file.rb, line 119
def self.append_what_into(
    what              = "Testing.\n",
    into_where        = 'default.txt',
    permission_to_use = ::SaveFile::DEFAULT_PERMISSION_TO_USE
  )
  ::SaveFile.save_what_into(
    what,
    into_where, 
    permission_to_use,
    'a+'
  )
end
new() click to toggle source
#

SaveFile.new

#
# File lib/save_file/base/base.rb, line 42
def self.new
  ::SaveFile::SaveFile.new
end

Public Instance Methods

save( what = "Testing.\n", into_where = SAVE_FILE_WHERE_TO, permission_to_use = DEFAULT_PERMISSION_TO_USE, file_mode_to_be_used = FILE_MODE_TO_BE_USED )
Alias for: save_file
save_file( what = "Testing.\n", into_where = SAVE_FILE_WHERE_TO, permission_to_use = DEFAULT_PERMISSION_TO_USE, file_mode_to_be_used = FILE_MODE_TO_BE_USED ) click to toggle source
#

save_file

Use this method to save a file in your ruby projects.

The arguments to this method are the following:

(1) what
(2) into_where
(3) the permission to use for the newly created file
(4) file mode to use

The second argument, ‘into_where`, is the target file that will be created or stored-into. For instance, ’test.txt’

As of Dec 2013, the third argument is the permission bit.

The fourth argument is the file mode to be used, which we default to the ‘w+’ mode. If you require append-functionality, it is recommended to use the append_into() or append_file() method, which can be found in this file as well, below the save_file() method.

#
# File lib/save_file/module/save_file.rb, line 39
def save_file(
    what                 = "Testing.\n",              # What we will save.
    into_where           = SAVE_FILE_WHERE_TO,        # Save into this file here.
    permission_to_use    = DEFAULT_PERMISSION_TO_USE, # With these permissions.
    file_mode_to_be_used = FILE_MODE_TO_BE_USED       # The file mode to be used. Can also be a Symbol such as :overwrite
  )

  into_where = into_where.to_s
  if    file_mode_to_be_used == :overwrite
    file_mode_to_be_used = 'w+'
  elsif file_mode_to_be_used == :append
    file_mode_to_be_used = 'a+'
  end
  # ======================================================================= #
  # Next handle Array input. Unsure whether we should join via \n or
  # not. Dec 2013 I decided that we join via '', and not via "\n".
  # ======================================================================= #
  if what.is_a? Array
    what = what.join('') # ("\n")
  end
  if File.directory? into_where
    into_where = sfile(into_where) if respond_to? :sfile
    e 'Sorry, can not store into '+into_where
    e 'as it is a directory.'
  end
  case permission_to_use
  when :default
    permission_to_use = DEFAULT_PERMISSION_TO_USE
  end
  if permission_to_use.is_a? String # File.open() does not like Strings, hence convert them.
     case permission_to_use
     when 'w' # We assume the user made a mistake here.
       file_mode_to_be_used = 'w'
       permission_to_use = DEFAULT_PERMISSION_TO_USE
     else # Else we treat it like an Octal.
       permission_to_use = permission_to_use.to_i(8)
     end
  elsif permission_to_use.nil?
     permission_to_use = DEFAULT_PERMISSION_TO_USE
  end
  if permission_to_use == :append
    permission_to_use = DEFAULT_PERMISSION_TO_USE
  elsif permission_to_use.is_a? Symbol # If that is the case then we assume an error happened.
    permission_to_use = DEFAULT_PERMISSION_TO_USE
  end
  file_mode_to_be_used = file_mode_to_be_used.to_s
  begin
    Signal.trap('SIGINT') { exit }
    # ===================================================================== #
    # If we are appending then we are effectively doing this:
    #   File.open('myfile.out','a') {|f| f.puts "Hello, world." }
    # ===================================================================== #
    File.open(into_where, file_mode_to_be_used, permission_to_use) {|file|
      file.write(what) # Write the new file here.
    } # The aliases to it here.
  rescue Errno::EINVAL
    if into_where.include? '-' # Then try again.
      into_where = into_where.split('-').first
    end
    File.open(into_where, file_mode_to_be_used, permission_to_use) {|file|
      file.write(what) # Write the new file here.
    }
  rescue Errno::EACCES
    into_where = sfile(into_where) if respond_to? :sfile
    error_string = 'You do not have sufficient permission '+
    'to write into '+into_where+'.'
    e error_string
  end
end
save_to( what = "Testing.\n", into_where = SAVE_FILE_WHERE_TO, permission_to_use = DEFAULT_PERMISSION_TO_USE, file_mode_to_be_used = FILE_MODE_TO_BE_USED )
Alias for: save_file
save_what_into( what = "Testing.\n", into_where = SAVE_FILE_WHERE_TO, permission_to_use = DEFAULT_PERMISSION_TO_USE, file_mode_to_be_used = FILE_MODE_TO_BE_USED )
Alias for: save_file
save_what_to( what = "Testing.\n", into_where = SAVE_FILE_WHERE_TO, permission_to_use = DEFAULT_PERMISSION_TO_USE, file_mode_to_be_used = FILE_MODE_TO_BE_USED )
Alias for: save_file
save_what_where( what = "Testing.\n", into_where = SAVE_FILE_WHERE_TO, permission_to_use = DEFAULT_PERMISSION_TO_USE, file_mode_to_be_used = FILE_MODE_TO_BE_USED )
Alias for: save_file
write_what_into( what = "Testing.\n", into_where = SAVE_FILE_WHERE_TO, permission_to_use = DEFAULT_PERMISSION_TO_USE, file_mode_to_be_used = FILE_MODE_TO_BE_USED )
Alias for: save_file
write_what_to( what = "Testing.\n", into_where = SAVE_FILE_WHERE_TO, permission_to_use = DEFAULT_PERMISSION_TO_USE, file_mode_to_be_used = FILE_MODE_TO_BE_USED )
Alias for: save_file