module LoadFile

Module to load file(s) into constant

Constants

VERSION

Public Class Methods

ignore_file_not_exists() { || ... } click to toggle source

@private

# File lib/load_file.rb, line 109
def self.ignore_file_not_exists
  yield
rescue Errno::ENOENT
end
load(file:, constant:, namespace: Object) click to toggle source

Loads file into constant.

@param file [String, Pathname, File] path to load the file from @param constant [String, Symbol] constant name to load into @param namespace [Object] namespace to find/load the constant, defaults to Object @return [Hash] loaded file content @return [NilClass] nil when file not exists

# File lib/load_file.rb, line 14
def self.load(file:, constant:, namespace: Object)
  ignore_file_not_exists do
    loader = Loader.new(file, constant, namespace: namespace)
    loader.set_constant
  end
end
load!(file:, constant:, namespace: Object) click to toggle source

Loads file into constant.

@param file [String, Pathname, File] path to load the file from @param constant [String, Symbol] constant name to load into @param namespace [Object] namespace to find/load the constant, defaults to Object @return [Hash] loaded file content, raises an error when file not exists @return [NilClass] nil when file not exists

# File lib/load_file.rb, line 28
def self.load!(file:, constant:, namespace: Object)
  loader = Loader.new(file, constant, namespace: namespace)
  loader.set_constant
end
load_files(files:, constant:, namespace: Object) click to toggle source

Loads files into constant. Any file not exists will be ignored and not raise error.

@param files [Array<String>] list of files to load @param constant [String, Symbol] constant name to load into @param namespace [Object] namespace to find/load the constant, defaults to Object @return [Hash] last loaded file content @return [NilClass] nil when last file not exists

# File lib/load_file.rb, line 41
def self.load_files(files:, constant:, namespace: Object)
  files.each { |file| load(file: file, constant: constant, namespace: namespace) }
end
load_files!(files:, constant:, namespace: Object) click to toggle source

Loads files into constant.

@param files [Array<String>] list of files to load @param constant [String, Symbol] constant name to load into @param namespace [Object] namespace to find/load the constant, defaults to Object @return [Hash] last loaded file content, raises an error when any file not exists

# File lib/load_file.rb, line 51
def self.load_files!(files:, constant:, namespace: Object)
  files.each { |file| load!(file: file, constant: constant, namespace: namespace) }
end
overload(file:, constant:, namespace: Object) click to toggle source

Overload a `file` into `constant`. Same as `load`, but will override existing values in `constant`.

@param file [String, Pathname, File] path to overload the file from @param constant [String, Symbol] constant name to overload into @param namespace [Object] namespace to find/load the constant, defaults to Object @return [Hash] overloaded file content @return [NilClass] nil when file not exists

# File lib/load_file.rb, line 63
def self.overload(file:, constant:, namespace: Object)
  ignore_file_not_exists do
    reader = Loader.new(file, constant, namespace: namespace)
    reader.set_constant!
  end
end
overload!(file:, constant:, namespace: Object) click to toggle source

Overload a `file` into `constant`. Same as `load!`, but will override existing values in `constant`.

@param file [String, Pathname, File] path to overload the file from @param constant [String, Symbol] constant name to overload into @param namespace [Object] namespace to find/load the constant, defaults to Object @return [Hash] overloaded file content, raises an error when file not exists @return [NilClass] nil when file not exists

# File lib/load_file.rb, line 78
def self.overload!(file:, constant:, namespace: Object)
  reader = Loader.new(file, constant, namespace: namespace)
  reader.set_constant!
end
overload_files(files:, constant:, namespace: Object) click to toggle source

Overload files into constant. Any file not exists will be ignored and not raise error. Same as `load_files`, but will override existing values in `constant`.

@param files [Array<String>] list of files to overload @param constant [String, Symbol] constant name to overload into @param namespace [Object] namespace to find/load the constant, defaults to Object @return [Hash] last overloaded file content @return [NilClass] nil when last file not exists

# File lib/load_file.rb, line 92
def self.overload_files(files:, constant:, namespace: Object)
  files.each { |file| overload(file: file, constant: constant, namespace: namespace) }
end
overload_files!(files:, constant:, namespace: Object) click to toggle source

Overload files into constant. Any file not exists will raise error. Same as `load_files!`, but will override existing values in `constant`.

@param files [Array<String>] list of files to overload @param constant [String, Symbol] constant name to overload into @param namespace [Object] namespace to find/load the constant, defaults to Object @return [Hash] last overloaded file content, raises an error when any file not exists

# File lib/load_file.rb, line 104
def self.overload_files!(files:, constant:, namespace: Object)
  files.each { |file| overload!(file: file, constant: constant, namespace: namespace) }
end