dh_easy-config 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ require 'yaml'
2
+ require 'dh_easy/core'
3
+ require 'dh_easy/config/version'
4
+ require 'dh_easy/config/local'
5
+
6
+ module DhEasy
7
+ module Config
8
+ end
9
+ end
@@ -0,0 +1,128 @@
1
+ module DhEasy
2
+ module Config
3
+ # Manage configuration from a file.
4
+ class Local
5
+ # Configuration loaded from local configuarion file (see #load).
6
+ #
7
+ # @return [Hash,nil] `nil` when nothing has been loaded.
8
+ attr_reader :local
9
+
10
+ # Clear cache.
11
+ def self.clear_cache
12
+ @@local = {}
13
+ end
14
+
15
+ # Load into or from cache a configuration file contents.
16
+ #
17
+ # @param [String] file_path Configuration file path.
18
+ # @param [Hash] opts ({}) Configuration options.
19
+ # @option opts [Boolean] :force (false) Will reload configuration file
20
+ # when `true`.
21
+ #
22
+ # @return [Hash] Configuration file contents.
23
+ def self.load_file file_path, opts = {}
24
+ opts = {
25
+ force: false
26
+ }.merge opts
27
+
28
+ return {} if file_path.nil?
29
+
30
+ @@local ||= {}
31
+ key = file_path = File.expand_path file_path
32
+ return @@local[key] if !opts[:force] && @@local.has_key?(key)
33
+
34
+ @@local[key] = (YAML.load_file(file_path) rescue {}) || {}
35
+ @@local[key].freeze
36
+ end
37
+
38
+ # Default configuration file path list to be prioritized from first to last.
39
+ #
40
+ # @return [Array<String>] Configuration file path list. Default is
41
+ # `['./dh_easy.yaml', './dh_easy.yml']`
42
+ def self.default_file_path_list
43
+ @@default_file_path_list ||= [
44
+ './dh_easy.yaml',
45
+ './dh_easy.yml'
46
+ ]
47
+ end
48
+
49
+ # Convert to hash.
50
+ #
51
+ # @return [Hash]
52
+ def to_h
53
+ local
54
+ end
55
+
56
+ # Get configuration key contents.
57
+ #
58
+ # @param [String] key Configuration option key.
59
+ #
60
+ # @return [Object,nil]
61
+ def [](key)
62
+ local[key]
63
+ end
64
+
65
+ # Lookup #file_path_list for the first valid file.
66
+ #
67
+ # @return [String,nil] Valid file path or `nil`.
68
+ def lookup_file_path
69
+ file_path_list.each do |candidate_file_path|
70
+ next unless File.file?(File.expand_path(candidate_file_path))
71
+ return candidate_file_path
72
+ end
73
+ nil
74
+ end
75
+
76
+ # Local configuration file path. It will lookup for the first valid file
77
+ # at #file_path_list as default value.
78
+ #
79
+ # @return [String] Configuration local file path.
80
+ def file_path
81
+ @file_path ||= lookup_file_path
82
+ end
83
+
84
+ # Local configuration file path list. It will prioritize from first to last.
85
+ #
86
+ # @return [Array<String>] Configuration local file path.
87
+ def file_path_list
88
+ @file_path_list ||= self.class.default_file_path_list
89
+ end
90
+
91
+ # Loads a local configuration file.
92
+ #
93
+ # @param [Hash] opts ({}) Configuration options.
94
+ # @option opts [String] :file_path (nil) Configuration file path to load (see
95
+ # #file_path for configuration default file.)
96
+ # @option opts [Boolean] :force (false) Will reload configuration file
97
+ # when `true`.
98
+ def load opts = {}
99
+ opts = {
100
+ file_path: nil,
101
+ force: false
102
+ }.merge opts
103
+ @file_path = opts[:file_path] || file_path
104
+ @local = self.class.load_file(file_path, opts)
105
+ end
106
+
107
+ # Reloads local configuration file.
108
+ def reload!
109
+ load force: true
110
+ end
111
+
112
+ # Reset instance to lookup for valid files from #file_path_list and load
113
+ # the first valid configuration file found.
114
+ def reset!
115
+ @file_path = nil
116
+ load force: true
117
+ end
118
+
119
+ # Initialize.
120
+ #
121
+ # @param [Hash] opts ({}) Configuration options (see #load).
122
+ def initialize opts = {}
123
+ @file_path_list = (opts.delete(:file_path_list) + []) unless opts[:file_path_list].nil?
124
+ load opts
125
+ end
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,6 @@
1
+ module DhEasy
2
+ module Config
3
+ # Gem version
4
+ VERSION = "0.0.6"
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,188 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dh_easy-config
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.6
5
+ platform: ruby
6
+ authors:
7
+ - Eduardo Rosales
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-12-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dh_easy-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov-console
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: timecop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: byebug
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: DataHen Easy toolkit config module allow an easy configuration from files
126
+ for advance features.
127
+ email:
128
+ - eduardo@datahen.com
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - ".travis.yml"
135
+ - ".yardopts"
136
+ - CODE_OF_CONDUCT.md
137
+ - Gemfile
138
+ - LICENSE
139
+ - README.md
140
+ - Rakefile
141
+ - dh_easy-config.gemspec
142
+ - doc/DhEasy.html
143
+ - doc/DhEasy/Config.html
144
+ - doc/DhEasy/Config/Local.html
145
+ - doc/_index.html
146
+ - doc/class_list.html
147
+ - doc/css/common.css
148
+ - doc/css/full_list.css
149
+ - doc/css/style.css
150
+ - doc/file.README.html
151
+ - doc/file_list.html
152
+ - doc/frames.html
153
+ - doc/index.html
154
+ - doc/js/app.js
155
+ - doc/js/full_list.js
156
+ - doc/js/jquery.js
157
+ - doc/method_list.html
158
+ - doc/top-level-namespace.html
159
+ - lib/dh_easy/config.rb
160
+ - lib/dh_easy/config/local.rb
161
+ - lib/dh_easy/config/version.rb
162
+ homepage: https://datahen.com
163
+ licenses:
164
+ - MIT
165
+ metadata:
166
+ homepage_uri: https://datahen.com
167
+ source_code_uri: https://github.com/DataHenOfficial/dh_easy-config
168
+ post_install_message:
169
+ rdoc_options: []
170
+ require_paths:
171
+ - lib
172
+ required_ruby_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ version: 2.2.2
177
+ required_rubygems_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ requirements: []
183
+ rubyforge_project:
184
+ rubygems_version: 2.7.6
185
+ signing_key:
186
+ specification_version: 4
187
+ summary: DataHen Easy toolkit config module
188
+ test_files: []