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.



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.



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.



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.



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



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



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



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



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