load_file 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +22 -1
- data/lib/load_file.rb +42 -34
- data/lib/load_file/loader.rb +6 -5
- data/lib/load_file/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 763d76493f7cb8621d494224c8d5e398dc9162e0b5d4f79ce848dc85a54ded9d
|
4
|
+
data.tar.gz: 978f14a25d1e48357cf46b6700bd7fbe10f41302ba9e51efdf31a16445d4ed59
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ab5f12d5a81dc50355ab08280efa961155c5b7cbf32da8233e6ca61ae2cb94fd10a4e52f78ca2abee0d2fb7b99fe3320381c2f4e0e0b561b599609e27721fe00
|
7
|
+
data.tar.gz: 84c6c5d28f16bfd5c298f58024b9c1368276070792cf601ae4432f19a01954778fc299652e11aaa4b5482a2399618b2265baa76e22570541fab5c9c6265292b6
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,14 @@
|
|
1
1
|
# LoadFile
|
2
2
|
|
3
|
+
[][gem]
|
4
|
+
[][travis]
|
5
|
+
[][docinfo]
|
6
|
+
|
7
|
+
[gem]: https://rubygems.org/gems/load_file
|
8
|
+
[travis]: https://travis-ci.org/JuanitoFatas/load_file
|
9
|
+
[docs]: https://inch-ci.org/github/juanitofatas/load_file
|
10
|
+
[docinfo]: https://www.rubydoc.info/github/JuanitoFatas/load_file/master
|
11
|
+
|
3
12
|
Load/Overload YAML/JSON file(s) into desired constant.
|
4
13
|
|
5
14
|
## Installation
|
@@ -28,13 +37,25 @@ HerokuApp
|
|
28
37
|
"environments"=>{"test"=>{"scripts"=>{"test"=>"bundle exec rake test"}}}}
|
29
38
|
```
|
30
39
|
|
40
|
+
You can also load into a namespaced constant:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
LoadFile.load(file: "examples/app.json", constant: :App, namespace: Heroku)
|
44
|
+
|
45
|
+
# Heroku::App will be the hash loaded from examples/app.json
|
46
|
+
```
|
47
|
+
|
48
|
+
The caveat here is the keyword argument `namespace` must be a ruby object.
|
49
|
+
|
50
|
+
Or should I introduce `constantize`? Please let me know.
|
51
|
+
|
31
52
|
### What if I want to override existing constant?
|
32
53
|
|
33
54
|
Use `overload` APIs.
|
34
55
|
|
35
56
|
## Contributing
|
36
57
|
|
37
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/JuanitoFatas/
|
58
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/JuanitoFatas/loadfile.
|
38
59
|
|
39
60
|
## License
|
40
61
|
|
data/lib/load_file.rb
CHANGED
@@ -6,58 +6,63 @@ require "load_file/loader"
|
|
6
6
|
module LoadFile
|
7
7
|
# Loads file into constant.
|
8
8
|
#
|
9
|
-
# @param [String, Pathname, File] path to load the file from
|
10
|
-
# @param [String, Symbol] constant name to load into
|
9
|
+
# @param file [String, Pathname, File] path to load the file from
|
10
|
+
# @param constant [String, Symbol] constant name to load into
|
11
|
+
# @param namespace [Object] namespace to find/load the constant, defaults to Object
|
11
12
|
# @return [Hash] loaded file content
|
12
13
|
# @return [NilClass] nil when file not exists
|
13
|
-
def self.load(file:, constant:)
|
14
|
+
def self.load(file:, constant:, namespace: Object)
|
14
15
|
ignore_file_not_exists do
|
15
|
-
|
16
|
-
|
16
|
+
loader = Loader.new(file, constant, namespace: namespace)
|
17
|
+
loader.set_constant
|
17
18
|
end
|
18
19
|
end
|
19
20
|
|
20
21
|
# Loads file into constant.
|
21
22
|
#
|
22
|
-
# @param [String, Pathname, File] path to load the file from
|
23
|
-
# @param [String, Symbol] constant name to load into
|
23
|
+
# @param file [String, Pathname, File] path to load the file from
|
24
|
+
# @param constant [String, Symbol] constant name to load into
|
25
|
+
# @param namespace [Object] namespace to find/load the constant, defaults to Object
|
24
26
|
# @return [Hash] loaded file content, raises an error when file not exists
|
25
27
|
# @return [NilClass] nil when file not exists
|
26
|
-
def self.load!(file:, constant:)
|
27
|
-
|
28
|
-
|
28
|
+
def self.load!(file:, constant:, namespace: Object)
|
29
|
+
loader = Loader.new(file, constant, namespace: namespace)
|
30
|
+
loader.set_constant
|
29
31
|
end
|
30
32
|
|
31
33
|
# Loads files into constant.
|
32
34
|
# Any file not exists will be ignored and not raise error.
|
33
35
|
#
|
34
|
-
# @param [Array<String>] list of files to load
|
35
|
-
# @param [String, Symbol] constant name to load into
|
36
|
+
# @param files [Array<String>] list of files to load
|
37
|
+
# @param constant [String, Symbol] constant name to load into
|
38
|
+
# @param namespace [Object] namespace to find/load the constant, defaults to Object
|
36
39
|
# @return [Hash] last loaded file content
|
37
40
|
# @return [NilClass] nil when last file not exists
|
38
|
-
def self.load_files(files:, constant:)
|
39
|
-
files.each { |file| load(file: file, constant: constant) }
|
41
|
+
def self.load_files(files:, constant:, namespace: Object)
|
42
|
+
files.each { |file| load(file: file, constant: constant, namespace: namespace) }
|
40
43
|
end
|
41
44
|
|
42
45
|
# Loads files into constant.
|
43
46
|
#
|
44
|
-
# @param [Array<String>] list of files to load
|
45
|
-
# @param [String, Symbol] constant name to load into
|
47
|
+
# @param files [Array<String>] list of files to load
|
48
|
+
# @param constant [String, Symbol] constant name to load into
|
49
|
+
# @param namespace [Object] namespace to find/load the constant, defaults to Object
|
46
50
|
# @return [Hash] last loaded file content, raises an error when any file not exists
|
47
|
-
def self.load_files!(files:, constant:)
|
48
|
-
files.each { |file| load!(file: file, constant: constant) }
|
51
|
+
def self.load_files!(files:, constant:, namespace: Object)
|
52
|
+
files.each { |file| load!(file: file, constant: constant, namespace: namespace) }
|
49
53
|
end
|
50
54
|
|
51
55
|
# Overload a `file` into `constant`.
|
52
56
|
# Same as `load`, but will override existing values in `constant`.
|
53
57
|
#
|
54
|
-
# @param [String, Pathname, File] path to overload the file from
|
55
|
-
# @param [String, Symbol] constant name to overload into
|
58
|
+
# @param file [String, Pathname, File] path to overload the file from
|
59
|
+
# @param constant [String, Symbol] constant name to overload into
|
60
|
+
# @param namespace [Object] namespace to find/load the constant, defaults to Object
|
56
61
|
# @return [Hash] overloaded file content
|
57
62
|
# @return [NilClass] nil when file not exists
|
58
|
-
def self.overload(file:, constant:)
|
63
|
+
def self.overload(file:, constant:, namespace: Object)
|
59
64
|
ignore_file_not_exists do
|
60
|
-
reader = Loader.new(file, constant)
|
65
|
+
reader = Loader.new(file, constant, namespace: namespace)
|
61
66
|
reader.set_constant!
|
62
67
|
end
|
63
68
|
end
|
@@ -65,12 +70,13 @@ module LoadFile
|
|
65
70
|
# Overload a `file` into `constant`.
|
66
71
|
# Same as `load!`, but will override existing values in `constant`.
|
67
72
|
#
|
68
|
-
# @param [String, Pathname, File] path to overload the file from
|
69
|
-
# @param [String, Symbol] constant name to overload into
|
73
|
+
# @param file [String, Pathname, File] path to overload the file from
|
74
|
+
# @param constant [String, Symbol] constant name to overload into
|
75
|
+
# @param namespace [Object] namespace to find/load the constant, defaults to Object
|
70
76
|
# @return [Hash] overloaded file content, raises an error when file not exists
|
71
77
|
# @return [NilClass] nil when file not exists
|
72
|
-
def self.overload!(file:, constant:)
|
73
|
-
reader = Loader.new(file, constant)
|
78
|
+
def self.overload!(file:, constant:, namespace: Object)
|
79
|
+
reader = Loader.new(file, constant, namespace: namespace)
|
74
80
|
reader.set_constant!
|
75
81
|
end
|
76
82
|
|
@@ -78,23 +84,25 @@ module LoadFile
|
|
78
84
|
# Any file not exists will be ignored and not raise error.
|
79
85
|
# Same as `load_files`, but will override existing values in `constant`.
|
80
86
|
#
|
81
|
-
# @param [Array<String>] list of files to overload
|
82
|
-
# @param [String, Symbol] constant name to overload into
|
87
|
+
# @param files [Array<String>] list of files to overload
|
88
|
+
# @param constant [String, Symbol] constant name to overload into
|
89
|
+
# @param namespace [Object] namespace to find/load the constant, defaults to Object
|
83
90
|
# @return [Hash] last overloaded file content
|
84
91
|
# @return [NilClass] nil when last file not exists
|
85
|
-
def self.overload_files(files:, constant:)
|
86
|
-
files.each { |file| overload(file: file, constant: constant) }
|
92
|
+
def self.overload_files(files:, constant:, namespace: Object)
|
93
|
+
files.each { |file| overload(file: file, constant: constant, namespace: namespace) }
|
87
94
|
end
|
88
95
|
|
89
96
|
# Overload files into constant.
|
90
97
|
# Any file not exists will raise error.
|
91
98
|
# Same as `load_files!`, but will override existing values in `constant`.
|
92
99
|
#
|
93
|
-
# @param [Array<String>] list of files to overload
|
94
|
-
# @param [String, Symbol] constant name to overload into
|
100
|
+
# @param files [Array<String>] list of files to overload
|
101
|
+
# @param constant [String, Symbol] constant name to overload into
|
102
|
+
# @param namespace [Object] namespace to find/load the constant, defaults to Object
|
95
103
|
# @return [Hash] last overloaded file content, raises an error when any file not exists
|
96
|
-
def self.overload_files!(files:, constant:)
|
97
|
-
files.each { |file| overload!(file: file, constant: constant) }
|
104
|
+
def self.overload_files!(files:, constant:, namespace: Object)
|
105
|
+
files.each { |file| overload!(file: file, constant: constant, namespace: namespace) }
|
98
106
|
end
|
99
107
|
|
100
108
|
# @private
|
data/lib/load_file/loader.rb
CHANGED
@@ -6,8 +6,9 @@ module LoadFile
|
|
6
6
|
#
|
7
7
|
# @return [Hash] parsed YAML or JSON content
|
8
8
|
class Loader < Hash
|
9
|
-
def initialize(file, constant_name)
|
9
|
+
def initialize(file, constant_name, namespace: Object)
|
10
10
|
@file = file
|
11
|
+
@namespace = namespace
|
11
12
|
@constant = find_or_define(constant_name)
|
12
13
|
|
13
14
|
update parsed_content
|
@@ -25,13 +26,13 @@ module LoadFile
|
|
25
26
|
|
26
27
|
private
|
27
28
|
|
28
|
-
attr_reader :file, :constant
|
29
|
+
attr_reader :file, :constant, :namespace
|
29
30
|
|
30
31
|
def find_or_define(constant_name)
|
31
|
-
if
|
32
|
-
|
32
|
+
if namespace.const_defined?(constant_name)
|
33
|
+
namespace.const_get(constant_name)
|
33
34
|
else
|
34
|
-
|
35
|
+
namespace.const_set(constant_name, {})
|
35
36
|
end
|
36
37
|
end
|
37
38
|
|
data/lib/load_file/version.rb
CHANGED