Module: LoadFile

Defined in:
lib/load_file.rb,
lib/load_file/loader.rb,
lib/load_file/parser.rb,
lib/load_file/version.rb

Overview

Module to load file(s) into constant

Defined Under Namespace

Classes: Loader, Parser

Constant Summary collapse

VERSION =
"1.1.0"

Class Method Summary collapse

Class Method Details

.ignore_file_not_existsObject



109
110
111
112
# File 'lib/load_file.rb', line 109

def self.ignore_file_not_exists
  yield
rescue Errno::ENOENT
end

.load(file:, constant:, namespace: Object) ⇒ Hash, NilClass

Loads file into constant.

Parameters:

  • file (String, Pathname, File)

    path to load the file from

  • constant (String, Symbol)

    constant name to load into

  • namespace (Object) (defaults to: Object)

    namespace to find/load the constant, defaults to Object

Returns:

  • (Hash)

    loaded file content

  • (NilClass)

    nil when file not exists



14
15
16
17
18
19
# 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) ⇒ Hash, NilClass

Loads file into constant.

Parameters:

  • file (String, Pathname, File)

    path to load the file from

  • constant (String, Symbol)

    constant name to load into

  • namespace (Object) (defaults to: Object)

    namespace to find/load the constant, defaults to Object

Returns:

  • (Hash)

    loaded file content, raises an error when file not exists

  • (NilClass)

    nil when file not exists



28
29
30
31
# 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) ⇒ Hash, NilClass

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

Parameters:

  • files (Array<String>)

    list of files to load

  • constant (String, Symbol)

    constant name to load into

  • namespace (Object) (defaults to: Object)

    namespace to find/load the constant, defaults to Object

Returns:

  • (Hash)

    last loaded file content

  • (NilClass)

    nil when last file not exists



41
42
43
# 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) ⇒ Hash

Loads files into constant.

Parameters:

  • files (Array<String>)

    list of files to load

  • constant (String, Symbol)

    constant name to load into

  • namespace (Object) (defaults to: Object)

    namespace to find/load the constant, defaults to Object

Returns:

  • (Hash)

    last loaded file content, raises an error when any file not exists



51
52
53
# 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) ⇒ Hash, NilClass

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

Parameters:

  • file (String, Pathname, File)

    path to overload the file from

  • constant (String, Symbol)

    constant name to overload into

  • namespace (Object) (defaults to: Object)

    namespace to find/load the constant, defaults to Object

Returns:

  • (Hash)

    overloaded file content

  • (NilClass)

    nil when file not exists



63
64
65
66
67
68
# 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) ⇒ Hash, NilClass

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

Parameters:

  • file (String, Pathname, File)

    path to overload the file from

  • constant (String, Symbol)

    constant name to overload into

  • namespace (Object) (defaults to: Object)

    namespace to find/load the constant, defaults to Object

Returns:

  • (Hash)

    overloaded file content, raises an error when file not exists

  • (NilClass)

    nil when file not exists



78
79
80
81
# 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) ⇒ Hash, NilClass

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`.

Parameters:

  • files (Array<String>)

    list of files to overload

  • constant (String, Symbol)

    constant name to overload into

  • namespace (Object) (defaults to: Object)

    namespace to find/load the constant, defaults to Object

Returns:

  • (Hash)

    last overloaded file content

  • (NilClass)

    nil when last file not exists



92
93
94
# 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) ⇒ Hash

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

Parameters:

  • files (Array<String>)

    list of files to overload

  • constant (String, Symbol)

    constant name to overload into

  • namespace (Object) (defaults to: Object)

    namespace to find/load the constant, defaults to Object

Returns:

  • (Hash)

    last overloaded file content, raises an error when any file not exists



104
105
106
# 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