skn_utils 5.1.0 → 5.1.1

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
  SHA1:
3
- metadata.gz: 4532c0129c738d77525a2c540cf7d6bdba464b29
4
- data.tar.gz: f6fd9d50967810320042a7ceea023164082beeec
3
+ metadata.gz: c8ddbccefff19413e5adde085173b94bbe2a8f4f
4
+ data.tar.gz: 1e640fd3aa205210e71574c64d315bee47867a97
5
5
  SHA512:
6
- metadata.gz: 6e784b05da136b62ccb2920d8449bb1258c1415c7d550ca2e5094c26697f01407b891b769e2d0ce094b72b36ae8171b1d3195a589f7a11f942ec15db4073bf67
7
- data.tar.gz: a8ecfb811398cdde7a2aa029478f50358d99585ddcfbfe00ee9a9de5d9686607d4a0436a03c62d4e917c6c2694d4ffb97800e8ec8343094760633d7f9be2ab65
6
+ metadata.gz: 63a0c08de09ac7a6165e6c920d57d54c7a279b3b15ad646b101b848432d0cac7fe51a416ccd7653b99c0d2ad2fd0030efc9ced6656ee7adca8c8034bb2a70985
7
+ data.tar.gz: 1ce7693ec5a65324e6993745819826b57218a0065b23dad7646878ec53f6023e04d83485c1c62ad22583004c53f9e07f7051d13c94897faf0eaeaf365cb3443d
data/README.md CHANGED
@@ -46,18 +46,23 @@ There are many more use cases for Ruby's Hash that this gem just makes easier to
46
46
  * SknSuccess
47
47
  * SknFailure
48
48
  * SknSettings
49
- * SknUtils::Configurable
49
+ * SknUtils::Configuration
50
50
  * SknUtils::EnvStringHandler
51
- * SknContainer
52
51
  * SknRegistry
52
+ * SknContainer
53
53
  * SknHash
54
- * SknUtils::NestedResult
55
54
  * SknUtils::ResultBean
56
55
  * SknUtils::PageControls
56
+ * SknUtils::NestedResult
57
57
  * SknUtils::NullObject
58
58
  * SknUtils::CoreObjectExtensions
59
+ * SknUtils::Configurable
60
+
59
61
 
60
62
  ## History
63
+ 10/15/2018 V5.1.1
64
+ Enhanced SknSettings to match 95% of the (Rb)Config.gem public API.
65
+
61
66
  10/13/2018 V5.1.0
62
67
  Added SknRegistry to handle service and handler registrations.
63
68
  - Command.class => CommandHandler.class/instance container
@@ -142,14 +147,14 @@ There are many more use cases for Ruby's Hash that this gem just makes easier to
142
147
 
143
148
  SknSettings # Multi-level application Configuration class, Key/Value Container with Dot/Hash notiation support.
144
149
 
145
- SknContainer # Basic Key/Value container which #registers and #resolves procs, classes, and/or object
150
+ SknContainer/SknRegistry # Basic Key/Value container which #registers and #resolves procs, classes, and/or object
146
151
 
147
152
  SknSuccess # Three attribute value containers for return codes -- #success, #message, #value
148
153
  SknFailure # Three attribute value containers for return codes -- #success, #message, #value
149
154
 
150
155
 
151
156
  ## Configuration Options
152
- None required other than initialization hash
157
+ None required
153
158
 
154
159
 
155
160
  ## Public Methods: SknContainer ONLY
@@ -186,11 +191,33 @@ There are many more use cases for Ruby's Hash that this gem just makes easier to
186
191
  #config_path!(path) -- Where path format is './<dirs>/', default is: './config/'
187
192
  and contains a settings.yml file and a 'path/settings/' directory
188
193
 
189
- Paths ./config and ./config/settings must exist.
194
+ Paths ./config must exist.
195
+ Paths ./config/settings/, and ./config/environments/ are optional
190
196
  File ./config/settings.yml must exist and contain a valid YAML file structure.
191
- ./config/settings.yml -- Required base
192
- ./config/settings/<config_name>.yml -- Optional base.deep_merge!()
193
- ./config/settings/<config_name>.local.yml -- Optional base.deep_merge!()
197
+
198
+ File load/deep_merge sequence:
199
+ -------------------------------------------------
200
+ <prepend-somefile-or-hash> -- see #prepend_source!(...)
201
+ config/settings.yml
202
+ config/settings/#{environment}.yml
203
+ config/environments/#{environment}.yml
204
+ config/settings.local.yml
205
+ config/settings/#{environment}.local.yml
206
+ config/environments/#{environment}.local.yml
207
+ <append-somefile-or-hash> -- see #add_source!(...)
208
+
209
+
210
+ Public API
211
+ -------------------------------------------------
212
+ #load_config_basename!(environment_name) -- self, loads std sequence
213
+ #config_path!(config_root) -- self, set config_root for above
214
+ #load_and_set_settings(ordered_filelist) -- self, sets config_root and loads std sequence
215
+ - Alias: #reload_from_files(ordered_filelist)
216
+ #reload!() -- self, clears and reloads last filelist
217
+ #setting_files(config_root, environment_name) -- returns ordered filelist
218
+ #add_source!(file_path_or_hash) -- self, adds yaml_file or hash to end of filelist (:reload! required)
219
+ #prepend_source!(file_path_or_hash) -- self, adds yaml_file or hash to start of filelist (:reload! required)
220
+ -------------------------------------------------
194
221
 
195
222
 
196
223
  ## Public Methods: SknUtils::NestedResult, SknHash & SknSettings
data/lib/skn_settings.rb CHANGED
@@ -5,7 +5,31 @@
5
5
  # Usage:
6
6
  # SknSettings.<config.key>[.<config.key>]...
7
7
  ##
8
-
8
+ # Filepath Targets:
9
+ # -------------------------------------------------
10
+ # <prepend-somefile>
11
+ # config/settings.yml
12
+ # config/settings/#{environment}.yml
13
+ # config/environments/#{environment}.yml
14
+ #
15
+ # config/settings.local.yml
16
+ # config/settings/#{environment}.local.yml
17
+ # config/environments/#{environment}.local.yml
18
+ # <append-somefile>
19
+ #
20
+ #
21
+ # Public API
22
+ # -------------------------------------------------
23
+ # load_config_basename!(environment_name)
24
+ # config_path!(config_root)
25
+ # load_and_set_settings(ordered_list_of_files)
26
+ # - Alias: reload_from_files(ordered_list_of_files)
27
+ # reload!()
28
+ # setting_files(config_root, environment_name)
29
+ # add_source!(file_path_or_hash)
30
+ # prepend_source!(file_path_or_hash)
31
+ # -------------------------------------------------
32
+ # ##
9
33
  # This creates a global constant (and singleton) with a defaulted configuration
10
34
  class << (SknSettings = SknUtils::Configuration.new())
11
35
  end
@@ -1,44 +1,151 @@
1
+ # ##
2
+ # lib/skn_utils/configuration.rb
1
3
  #
2
4
  # @see SknSettings
5
+ #
6
+ # Filepath Targets:
7
+ # -------------------------------------------------
8
+ # <prepend-somefile>
9
+ # config/settings.yml
10
+ # config/settings/#{environment}.yml
11
+ # config/environments/#{environment}.yml
12
+ #
13
+ # config/settings.local.yml
14
+ # config/settings/#{environment}.local.yml
15
+ # config/environments/#{environment}.local.yml
16
+ # <append-somefile>
17
+ #
18
+ #
19
+ # Public API
20
+ # -------------------------------------------------
21
+ # load_config_basename!(environment_name)
22
+ # config_path!(config_root)
23
+ # load_and_set_settings(ordered_list_of_files)
24
+ # - Alias: reload_from_files(ordered_list_of_files)
25
+ # reload!()
26
+ # setting_files(config_root, environment_name)
27
+ # add_source!(file_path_or_hash)
28
+ # prepend_source!(file_path_or_hash)
29
+ # -------------------------------------------------
30
+ #
31
+ # SknSettings setup:
32
+ # class << (SknSetting = SknUtils::Configuration.new( ENV['RACK_ENV'] )); end
33
+ #
34
+ #
3
35
 
4
36
  module SknUtils
5
37
 
6
38
  class Configuration < NestedResult
7
39
 
40
+ # "filename-only"
41
+ # {config_filename: "filename-only"}
8
42
  def initialize(params={})
9
- default_mode = ENV.fetch('RACK_ENV', 'development')
10
- @config_filename = params.is_a?(String) ? params : params.fetch(:config_filename, default_mode)
11
- @base_path = ENV.fetch('TEST_GEM', 'rails').eql?('gem') ? './spec/factories/' : './config/'
12
- load_config_basename!(@config_filename)
43
+ super()
44
+ @_last_filelist = []
45
+ @_base_path = './config/'
46
+ @_default_mode = ENV['RACK_ENV'] || 'development'
47
+ cfg_file = params.is_a?(String) ? params : (params[:config_filename] || @_default_mode)
48
+ test_mode = ENV.fetch('TEST_GEM', 'rails').eql?('gem')
49
+ config_path!( test_mode ? './spec/factories/' : './config/' )
50
+ load_config_basename!(cfg_file)
51
+ end
52
+
53
+ def config_path!(config_root)
54
+ @_base_path = config_root[-1].eql?('/') ? config_root : "#{config_root}/"
55
+ self
13
56
  end
14
57
 
15
- def load_config_basename!(conf)
16
- reset_from_empty!(load_config(conf), false) # enable dot notation via defined methods(true) vs method_missing(false)
58
+ def load_config_basename!(environment_name=@_default_mode)
59
+ reset_from_empty!(load_config(environment_name), false) # enable dot notation via defined methods(true) vs method_missing(false)
17
60
  self
18
61
  end
19
62
 
20
- def config_path!(fpath)
21
- @base_path = fpath if File.exist?("#{fpath}settings.yml")
63
+ def load_and_set_settings(ordered_list_of_files)
64
+ reset_from_empty!( load_ordered_list( ordered_list_of_files ), false )
65
+ self
66
+ end
67
+ alias_method :reload_from_files, :load_and_set_settings
68
+
69
+ def reload!
70
+ reset_from_empty!( load_ordered_list(@_last_filelist), false )
71
+ self
72
+ end
73
+
74
+ # Config.setting_files("/path/to/config_root", "your_project_environment")
75
+ def setting_files(config_root, environment_name) # returns a file array
76
+ config_path!(config_root)
77
+ @_default_mode = environment_name
78
+ configuration_files(environment_name)
79
+ end
80
+
81
+ # :reload! required
82
+ def add_source!(file_path_or_hash) # load last
83
+ return {} unless valid_file_path?(file_path_or_hash)
84
+ @_last_filelist.push(file_path_or_hash).flatten
85
+ self
86
+ end
87
+
88
+ # :reload! required
89
+ def prepend_source!(file_path_or_hash) # load first
90
+ return {} unless valid_file_path?(file_path_or_hash)
91
+ @_last_filelist.unshift(file_path_or_hash).flatten
92
+ self
22
93
  end
23
94
 
24
95
  private
25
96
 
26
- def load_config(conf)
27
- yname = "#{@base_path}settings.yml"
28
- return {} unless File.exist?(yname)
29
- f_base = load_yml_with_erb(yname)
97
+ def valid_file_path?(file_path_or_hash)
98
+ if file_path_or_hash.kind_of?(Hash)
99
+ true
100
+
101
+ elsif file_path_or_hash.kind_of?(String)
102
+ File.exist?( file_path_or_hash )
103
+
104
+ else
105
+ false
106
+
107
+ end
108
+ end
30
109
 
31
- yname = "#{@base_path}settings/#{conf}.yml"
32
- f_env = load_yml_with_erb(yname) if File.exist?(yname)
110
+ def configuration_files(environment_name=@_default_mode)
111
+ [
112
+ "#{@_base_path}settings.yml",
113
+ "#{@_base_path}settings/#{environment_name}.yml",
114
+ "#{@_base_path}environments/#{environment_name}.yml",
115
+ "#{@_base_path}settings.local.yml",
116
+ "#{@_base_path}settings/#{environment_name}.local.yml",
117
+ "#{@_base_path}environments/#{environment_name}.local.yml"
118
+ ]
119
+ end
120
+
121
+ def load_ordered_list(filelist)
122
+ settings_file = nil
123
+ yname = filelist.first
124
+
125
+ # maintain last list for :reload
126
+ @_last_filelist = filelist unless @_last_filelist === filelist
127
+
128
+ settings_file = yname if !!yname && yname.include?('settings.yml')
129
+
130
+ f_base = {}
131
+ filelist.each do |filepath|
132
+ next unless valid_file_path?( filepath )
133
+ settings_file = filepath if filepath.include?('settings.yml')
134
+ f_env = filepath.is_a?(Hash) ? filepath : load_yml_with_erb(filepath)
33
135
  f_base = f_base.deep_merge!(f_env) unless (f_env.nil? || f_env.empty?)
136
+ end
34
137
 
35
- yname = "#{@base_path}settings/#{conf}.local.yml"
36
- f_local = load_yml_with_erb(yname) if File.exist?(yname)
37
- f_base = f_base.deep_merge!(f_local) unless (f_local.nil? || f_local.empty?)
138
+ unless settings_file.nil?
139
+ config_path!( File.dirname(settings_file) ) # maintain root dir
140
+ end
38
141
 
39
142
  f_base
40
143
  end
41
144
 
145
+ def load_config(conf)
146
+ load_ordered_list( configuration_files(conf) )
147
+ end
148
+
42
149
  def load_yml_with_erb(yml_file)
43
150
  erb = ERB.new(File.read(yml_file)).result
44
151
  erb.empty? ? {} : Psych.load(erb)
@@ -190,7 +190,7 @@ module SknUtils
190
190
  bundle.keys.each_with_object({}) do |attr,collector|
191
191
  value = bundle[attr]
192
192
  case value
193
- when NestedResult
193
+ when NestedResult, self.class
194
194
  value = value.to_hash
195
195
  when Array
196
196
  value = value.map {|ele| array_to_hash(ele) }
@@ -3,7 +3,7 @@ module SknUtils
3
3
  class Version
4
4
  MAJOR = 5
5
5
  MINOR = 1
6
- PATCH = 0
6
+ PATCH = 1
7
7
 
8
8
  def self.to_s
9
9
  [MAJOR, MINOR, PATCH].join('.')
@@ -0,0 +1,4 @@
1
+ ---
2
+ Packaging:
3
+ isDevelopment: true
4
+ configName: development
@@ -0,0 +1,4 @@
1
+ ---
2
+ Packaging:
3
+ isProduction: false
4
+ configName: production
@@ -0,0 +1,4 @@
1
+ ---
2
+ Packaging:
3
+ isTest: true
4
+ configName: test
@@ -0,0 +1,4 @@
1
+ ---
2
+ Packaging:
3
+ isDevelopment: true
4
+ configName: development
@@ -0,0 +1,4 @@
1
+ ---
2
+ Packaging:
3
+ isProduction: false
4
+ configName: production
@@ -1,5 +1,4 @@
1
1
  ---
2
2
  Packaging:
3
3
  isTest: true
4
- isDevelopment: false
5
4
  configName: test
@@ -2,10 +2,10 @@
2
2
  Packaging:
3
3
  pomVersion: <%= SknUtils::VERSION %>
4
4
  isTest: false
5
- isDevelopment: true
5
+ isDevelopment: false
6
6
  isStaging: false
7
7
  isProduction: false
8
- configName: development
8
+ configName: default
9
9
  short_name: skn
10
10
 
11
11
  # Create a EnvStringHandler to support: SknSettings.env.development?
@@ -1,22 +1,71 @@
1
1
  #
2
2
 
3
- describe SknSettings, "Application Configuration" do
3
+ describe SknUtils::Configuration, "SknSettings Application Configuration Module" do
4
+
5
+ let(:test_list) do
6
+ [
7
+ "./spec/factories/settings.yml",
8
+ "./spec/factories/settings/test.yml",
9
+ "./spec/factories/settings/test.local.yml"
10
+ ]
11
+ end
12
+
13
+ before :each do
14
+ @service = described_class.new.load_and_set_settings(test_list)
15
+ end
4
16
 
5
17
  it 'contains the test settings for the application' do
6
- expect(SknSettings.Packaging.pomVersion).to eq SknUtils::VERSION
7
- expect(SknSettings.Packaging.configName).to eq 'test.local'
8
- expect(SknSettings.Packaging.isTest).to be true
18
+ expect(@service.Packaging.pomVersion).to eq SknUtils::VERSION
19
+ expect(@service.Packaging.configName).to eq 'test.local'
20
+ expect(@service.Packaging.isTest).to be true
9
21
  end
10
22
 
11
23
  it 'contains the current RACK or RAILS environment values' do
12
- expect(SknSettings.env).to eq 'test'
13
- expect(SknSettings.env.test?).to be true
14
- expect(SknSettings.env.development?).to be false
24
+ expect(@service.env).to eq 'test'
25
+ expect(@service.env.test?).to be true
26
+ expect(@service.env.development?).to be false
15
27
  end
16
28
 
17
29
  it 'contains the application root path as a string' do
18
- expect(SknSettings.root).to eq Dir.pwd
19
- expect(SknSettings.root).to be_a(String)
30
+ expect(@service.root).to eq Dir.pwd
31
+ expect(@service.root).to be_a(String)
32
+ end
33
+
34
+ it "#load_config_basename!(conf=@default_mode) load correct environments. " do
35
+ %w(development production).each do |env_mode|
36
+ expect(@service.config_path!('./spec/factories/').load_config_basename!(env_mode).Packaging.configName).to eq env_mode
37
+ end
38
+ end
39
+
40
+ it "#reload! reloads the current list of files. " do
41
+ expect( @service.reload!().env.test? ).to be true
42
+ expect( @service.reload!().env.test? ).to be true
43
+ end
44
+
45
+ it "#config_path!(fpath) set the configfuration root path properly. " do
46
+ @service.config_path!('./spec/factories/')
47
+ expect( @service.instance_variable_get(:@_base_path) ).to eq('./spec/factories/')
48
+
49
+ @service.config_path!('./spec/factories')
50
+ expect( @service.instance_variable_get(:@_base_path) ).to eq('./spec/factories/')
51
+ end
52
+
53
+ it "#load_and_set_settings(ordered_list_of_files) " do
54
+ expect( @service.load_and_set_settings(test_list).env.test? ).to be true
55
+ end
56
+
57
+ # Config.setting_files("/path/to/config_root", "your_project_environment")
58
+ it "#setting_files(config_root, env_name) returns a file array. " do
59
+ expect(@service.setting_files('./spec/factories/', 'test')).to be_a(Array)
20
60
  end
21
61
 
62
+ it "#add_source!(file_path_or_hash) adds provided file to end of list. " do
63
+ expect(@service.add_source!({Packaging: {configName: 'The Doctor'}}).reload!.Packaging.configName ).to eq('The Doctor')
64
+ end
65
+
66
+ it "#prepend_source!(prepend_fpath) adds provided value to front of list. " do
67
+ expect(@service.prepend_source!({Packaging: {configName: 'The Doctor'}}).reload!.Packaging.configName ).to eq('test.local')
68
+ end
69
+
70
+
22
71
  end
@@ -122,8 +122,8 @@ describe SknRegistry, "IoC Lite Container class." do
122
122
  expect(services.resolve(:some_depends, false)).to be_a Proc
123
123
  expect(
124
124
  services.resolve(:some_depends, false)
125
- .call({greet: 'My warmest', str: 'Hello'})
126
- ).to eq("My warmest HELLO")
125
+ .call({greet: 'My static', str: 'Hello'})
126
+ ).to eq("My static HELLO")
127
127
  end
128
128
  end
129
129
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: skn_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.1.0
4
+ version: 5.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Scott Jr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-13 00:00:00.000000000 Z
11
+ date: 2018-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: deep_merge
@@ -183,7 +183,12 @@ files:
183
183
  - lib/skn_utils/result_bean.rb
184
184
  - lib/skn_utils/version.rb
185
185
  - skn_utils.gemspec
186
+ - spec/factories/environments/development.yml
187
+ - spec/factories/environments/production.yml
188
+ - spec/factories/environments/test.yml
186
189
  - spec/factories/settings.yml
190
+ - spec/factories/settings/development.yml
191
+ - spec/factories/settings/production.yml
187
192
  - spec/factories/settings/test.local.yml
188
193
  - spec/factories/settings/test.yml
189
194
  - spec/lib/skn_settings_spec.rb
@@ -224,7 +229,12 @@ specification_version: 4
224
229
  summary: SknUtils contains a small collection of Ruby utilities, the first being a
225
230
  NestedResult a key/value container.
226
231
  test_files:
232
+ - spec/factories/environments/development.yml
233
+ - spec/factories/environments/production.yml
234
+ - spec/factories/environments/test.yml
227
235
  - spec/factories/settings.yml
236
+ - spec/factories/settings/development.yml
237
+ - spec/factories/settings/production.yml
228
238
  - spec/factories/settings/test.local.yml
229
239
  - spec/factories/settings/test.yml
230
240
  - spec/lib/skn_settings_spec.rb