app_config_for 0.0.1 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 16aa403545b3c7bb28165f904eb60d51138293dfbc8269c76bc2f13ec3cf44a0
4
- data.tar.gz: 707c8d7280805a21df63505882cabd48ac9e7a9536b717fb9054bd50c2b83ec9
3
+ metadata.gz: fffc63810437848a24a9e6f1d8cf02081060a0fe2d348bd8e7bb14c5917ca2ec
4
+ data.tar.gz: 76c6787ebe99d673214fdd9e9bba7f4e0ff003c18308214444bdd48e38736023
5
5
  SHA512:
6
- metadata.gz: a7996b0d285f25d1a874ed19593ed4773d20d6eeb55237bfdccf3f61c9045a49c73a703ffeeac014e3035c71f131f1f0b7f98f0a205fe7fb71250778e1985d58
7
- data.tar.gz: 372bb10b3e6f63754a65608b7d67553c6fd49ec821b8efe101f067e9060f816ab1ab023fafef38b5b37af1a0dc1495e0cc073120805fef15657452946bfa788d
6
+ metadata.gz: 142a9d57a16dcb0951ae39da4103138dd9b8d6b9d53b52bf4776d4ae4701290f0d00b6b36e773ac1af95d69bc150dade305bf43353a49a1072e715253c6fd034
7
+ data.tar.gz: '0499b786cade8df74b4adc315f231fcbb3ef1c7f0b91cd74bdb430d8d779451edd4dfba7fd509cbe2e8af2daec00f6ad7ad1b1a78b287211016eda55bfb623d3'
@@ -22,4 +22,17 @@ module AppConfigFor
22
22
  super "Could not load configuration file: #{@file}\n#{@original_exception.message}"
23
23
  end
24
24
  end
25
+
26
+ class InvalidEnvInheritanceStyle < Error
27
+
28
+ attr_reader :attempted, :valid
29
+
30
+ def initialize(attempted)
31
+ @attempted = attempted
32
+ @valid = EnvPrefixInheritanceStyles.dup
33
+ super "Invalid inheritance style #{@attempted.inspect}. Please use one of the following: #{@valid.map(&:inspect).join(', ')}"
34
+ end
35
+
36
+ end
37
+
25
38
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AppConfigFor
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.3"
5
5
  end
@@ -12,17 +12,20 @@ require 'active_support/core_ext/object/try'
12
12
 
13
13
  module AppConfigFor
14
14
 
15
+ EnvPrefixInheritanceStyles = %i(none namespace class namespace_class class_namespace)
16
+
15
17
  def initialize(*args)
16
18
  add_env_prefix
17
19
  super
18
20
  end
19
21
 
20
- def add_env_prefix(prefix = nil, at_beginning: true)
21
- env_prefixes(all: false, dup: false).send(at_beginning ? :unshift : :push, AppConfigFor.prefix_from(prefix || self)).uniq!
22
+ def add_env_prefix(prefix = nil, at_beginning = true)
23
+ env_prefixes(false, false).send(at_beginning ? :unshift : :push, AppConfigFor.prefix_from(prefix || self)).uniq!
22
24
  end
23
25
 
24
26
  def config_directories
25
- directories = ['Rails'.safe_constantize&.application&.paths, try(:paths)].compact.map { |root| Pathname.new(root["config"].existent.first) }
27
+ directories = ['Rails'.safe_constantize&.application&.paths, try(:paths)].compact.map { |root| root["config"].existent.first }.compact
28
+ directories.map! { |directory| Pathname.new(directory) }
26
29
  directories.push(Pathname.getwd + 'config')
27
30
  directories.uniq
28
31
  end
@@ -44,8 +47,8 @@ module AppConfigFor
44
47
  !config_file(name).blank?
45
48
  end
46
49
 
47
- def config_for(name, environment: nil)
48
- config, shared = config_options(name).fetch_values((environment || env).to_sym, :shared) {nil}
50
+ def config_for(name, env: nil)
51
+ config, shared = config_options(name).fetch_values((env || self.env).to_sym, :shared) {nil}
49
52
  config ||= shared
50
53
 
51
54
  if config.is_a?(Hash)
@@ -65,44 +68,52 @@ module AppConfigFor
65
68
  raise LoadError.new(file, exception)
66
69
  end
67
70
 
68
- def configured(environment: nil)
69
- config_for(self, environment: environment)
71
+ def configured(env: nil)
72
+ config_for(self, env: env)
70
73
  end
71
74
 
72
- def env(reload: false)
75
+ def env(reload = false)
73
76
  @env = ActiveSupport::EnvironmentInquirer.new(AppConfigFor.env_name(env_prefixes)) if reload || @env.nil?
74
77
  @env
75
78
  end
76
79
 
77
- def env_prefixes(all: true, dup: true)
80
+ def env_prefix_inheritance
81
+ @env_prefix_inheritance ||= :namespace
82
+ end
83
+
84
+ def env_prefix_inheritance=(style)
85
+ @env_prefix_inheritance = AppConfigFor.verified_style!(style)
86
+ end
87
+
88
+ def env_prefixes(all = true, dup = true)
78
89
  @env_prefixes ||= []
79
90
  if all
80
- @env_prefixes + AppConfigFor.progenitor_of(self).env_prefixes(all: true)
91
+ @env_prefixes + AppConfigFor.progenitor_prefixes_of(self)
81
92
  else
82
93
  dup ? @env_prefixes.dup : @env_prefixes
83
94
  end
84
95
  end
85
96
 
86
- def remove_env_prefix(prefix, all: false)
97
+ def remove_env_prefix(prefix, all = false)
87
98
  if all
88
99
  remove_env_prefix(prefix)
89
- AppConfigFor.progenitor_of(self).remove_env_prefix(prefix, all: true)
100
+ AppConfigFor.progenitor_of(self)&.remove_env_prefix(prefix, all)
90
101
  else
91
- env_prefixes(all: false, dup: false).delete(AppConfigFor.prefix_from(prefix))
102
+ env_prefixes(false, false).delete(AppConfigFor.prefix_from(prefix))
92
103
  end
93
104
  end
94
105
 
95
106
  class << self
96
107
 
97
- def add_env_prefix(prefix)
98
- env_prefixes(dup: false).push(prefix_from(prefix))
108
+ def add_env_prefix(prefix, at_beginning = true)
109
+ env_prefixes(false, false).send(at_beginning ? :unshift : :push, prefix_from(prefix)).uniq!
99
110
  end
100
111
 
101
112
  def env_name(prefixes = env_prefixes)
102
113
  prefixes.inject(nil) { |current_env, name| current_env || ENV["#{name.to_s.upcase}_ENV"].presence } || 'development'
103
114
  end
104
115
 
105
- def env_prefixes(all: true, dup: true)
116
+ def env_prefixes(_all = true, dup = true)
106
117
  # all is ignored as we are at the end of the chain
107
118
  @env_prefixes ||= [:rails, :rack]
108
119
  dup ? @env_prefixes.dup : @env_prefixes
@@ -119,6 +130,27 @@ module AppConfigFor
119
130
  end.deconstantize.safe_constantize
120
131
  end
121
132
 
133
+ # Not used internally, this is a convenience method to study what progenitors are used during namespace dives
134
+ def namespaces_of(object)
135
+ (object = [namespace_of(object)]).each { |x| x && object << namespace_of(x) }[0..-2]
136
+ end
137
+
138
+ def parent_of(object)
139
+ case object
140
+ when String
141
+ object.safe_constantize
142
+ when Class
143
+ object.superclass
144
+ else
145
+ object.class
146
+ end
147
+ end
148
+
149
+ # Not used internally, this is a convenience method to study what progenitors are used during class dives
150
+ def parents_of(object)
151
+ (object = [parent_of(object)]).each { |x| x && object << parent_of(x) }[0..-2]
152
+ end
153
+
122
154
  def prefix_from(object)
123
155
  if object.is_a?(Symbol)
124
156
  object
@@ -134,19 +166,41 @@ module AppConfigFor
134
166
  object.class.name
135
167
  end.underscore.gsub('/','_').to_sym
136
168
  end
169
+ end
137
170
 
171
+ def progenitor_of(object, style = nil)
172
+ style = verified_style!(style, object)
173
+ command = {namespace: :namespace_of, class: :parent_of}[style]
174
+ object && command && send(command, object).yield_self { |n| n && (n.respond_to?(:env_prefixes) ? n : progenitor_of(n)) }
138
175
  end
139
176
 
140
- # First namespace of the object that supports env_prefixes or AppConfig
141
- def progenitor_of(object)
142
- (namespace_of(object) || self).yield_self do |namespace|
143
- namespace.respond_to?(:env_prefixes) ? namespace : progenitor_of(namespace)
144
- end
177
+ def progenitor_prefixes_of(object, style = nil, all = true)
178
+ Array(progenitor_of(object, style)&.env_prefixes(all))
145
179
  end
146
180
 
147
- def remove_env_prefix(prefix, all: false)
148
- # all is ignored as we are at the end of the chain
149
- env_prefixes(dup: false).delete(prefix_from(prefix))
181
+ def progenitors_of(object, style = nil, terminate = true)
182
+ style = verified_style!(style, object)
183
+ terminate = terminate && style != :none
184
+ if object && style != :none
185
+ styles = style.to_s.split('_')
186
+ if styles.size > 1
187
+ styles.flat_map{ |style| progenitors_of(object, style, false) }
188
+ else
189
+ Array(progenitor_of(object, style)).yield_self { |x| x + progenitors_of(x.last, nil, false) }
190
+ end
191
+ else
192
+ []
193
+ end.yield_self { |result| terminate ? result.reverse.uniq.reverse + [self] : result }
194
+ end
195
+
196
+ def remove_env_prefix(prefix, all = false)
197
+ env_prefixes(all, false).delete(prefix_from(prefix))
198
+ end
199
+
200
+ def verified_style!(style, object = nil)
201
+ style ||= object.respond_to?(:env_prefix_inheritance) ? object.send(:env_prefix_inheritance) : :namespace
202
+ style = style.try(:to_sym) || style.to_s.to_sym
203
+ EnvPrefixInheritanceStyles.include?(style) ? style : raise(InvalidEnvInheritanceStyle.new(style))
150
204
  end
151
205
 
152
206
  def yml_name_from(object)
@@ -172,11 +226,11 @@ module AppConfigFor
172
226
  base.add_env_prefix
173
227
  end
174
228
 
175
- # Todo: Determine progenitor_of with respect to combining inheritance with namespace scoping
176
- # def included(base)
177
- # base.add_env_prefix
178
- # end
229
+ def included(base)
230
+ base.add_env_prefix
231
+ end
179
232
 
180
233
  end
181
234
 
182
235
  end
236
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: app_config_for
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frank Hall
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-07 00:00:00.000000000 Z
11
+ date: 2022-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -79,8 +79,8 @@ licenses:
79
79
  - MIT
80
80
  metadata:
81
81
  homepage_uri: https://github.com/ChapterHouse/app_config_for
82
- source_code_uri: https://github.com/ChapterHouse/app_config_for/tree/v0.0.1
83
- changelog_uri: https://github.com/ChapterHouse/app_config_for/blob/v0.0.1/CHANGELOG.md
82
+ source_code_uri: https://github.com/ChapterHouse/app_config_for/tree/v0.0.3
83
+ changelog_uri: https://github.com/ChapterHouse/app_config_for/blob/v0.0.3/CHANGELOG.md
84
84
  post_install_message:
85
85
  rdoc_options: []
86
86
  require_paths: