load_file 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 370bdebd061e99a284e045ac276ebbf6525957656b133873acb19aa2206fcf6f
4
- data.tar.gz: 1fdc0d2148d432bc65b61bcaa5e002e7e74eecc3bb0eb90721c6cceb0f7cfc70
3
+ metadata.gz: 763d76493f7cb8621d494224c8d5e398dc9162e0b5d4f79ce848dc85a54ded9d
4
+ data.tar.gz: 978f14a25d1e48357cf46b6700bd7fbe10f41302ba9e51efdf31a16445d4ed59
5
5
  SHA512:
6
- metadata.gz: 87067068f85f261224bb1b19f0b164aa1f1c3f8a15f32c42253df95d43e71e3017fc714e5146731624050598c86b08b8c2ef7386dba2798c5e9cd90b65ac4c6e
7
- data.tar.gz: b8208625d5c588b40e6a24c36627a755726c0d45ee9e4e7bc353a7da70e5234519f930729ce9a579d0d67bcad0c4af123b6a46b0db532c46910d8c02d75eee51
6
+ metadata.gz: ab5f12d5a81dc50355ab08280efa961155c5b7cbf32da8233e6ca61ae2cb94fd10a4e52f78ca2abee0d2fb7b99fe3320381c2f4e0e0b561b599609e27721fe00
7
+ data.tar.gz: 84c6c5d28f16bfd5c298f58024b9c1368276070792cf601ae4432f19a01954778fc299652e11aaa4b5482a2399618b2265baa76e22570541fab5c9c6265292b6
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## Unreleased
4
+
5
+ ## 1.1.0
6
+
7
+ - Adds support to (over)load file(s) into namespaced constant
8
+ - documentation fixes
9
+
3
10
  ## 1.0.0
4
11
 
5
12
  1.0.0 includes:
data/README.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # LoadFile
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/os_name.svg)][gem]
4
+ [![Build Status](https://travis-ci.org/JuanitoFatas/load_file.svg?branch=master)][travis]
5
+ [![inch-ci.org doc badge](https://inch-ci.org/github/juanitofatas/load_file.svg?branch=master)][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/devyml.
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/JuanitoFatas/loadfile.
38
59
 
39
60
  ## License
40
61
 
@@ -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
- reader = Loader.new(file, constant)
16
- reader.set_constant
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
- reader = Loader.new(file, constant)
28
- reader.set_constant
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
@@ -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 Object.const_defined?(constant_name)
32
- Object.const_get(constant_name)
32
+ if namespace.const_defined?(constant_name)
33
+ namespace.const_get(constant_name)
33
34
  else
34
- Object.const_set(constant_name, {})
35
+ namespace.const_set(constant_name, {})
35
36
  end
36
37
  end
37
38
 
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module LoadFile
3
- VERSION = "1.0.0"
3
+ VERSION = "1.1.0"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: load_file
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juanito Fatas