eac_config 0.3.0 → 0.4.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: 1146b6d7dcccd0749b9ac875dceb8b521e386cc4468ffeab106f33e29e254a85
4
- data.tar.gz: b4da054e60c2c5316670d047869ce98c3ca4255fc1df62db9b5ef5f303bdd5e8
3
+ metadata.gz: fecd4e56674406cf96275edc74b51ee0c908076bf3f991f079ee439a7fd98606
4
+ data.tar.gz: 33ed9a3d05488a9d06919589432727d6a9e72430234c05bb89e1119ea6b4c05f
5
5
  SHA512:
6
- metadata.gz: e3c47344815bd911603f54a37581a6be9b7182f87d3b2546e708f903a5da3bb7799da013c189db7a3f3d0572e64b4124562a43f9ebe54c0e88805d12fa94503a
7
- data.tar.gz: 5f6ba2fb942129f1b8b5d47ab1e1693d025303f501ebb6e7f0a25109369beaa99dfcc420e08f5d8c50ffb495bf52c287f6491a5d4b11014c1c051f21c4cd272b
6
+ metadata.gz: cdd3b1bfe2d2e8dce429aa34dde375120f13796106c9145b8abcdfc6cbbb42187f1cf3504e1ec5027aa28afe437311c82fe1b1bf77b0aed195148ea9c57ba60a
7
+ data.tar.gz: 37807d3bfeae842fb0f43a58cb2f90f1851cee1bd1802437a23cad122c6ab2d5cad896a4125aa45b3da73098863611d9e9575fdd5241d7ce67143bcb9ccbc97a
@@ -22,7 +22,13 @@ module EacConfig
22
22
  node_entry.if_present(&:value)
23
23
  end
24
24
 
25
- delegate :value=, to: :root_node
25
+ def value=(a_value)
26
+ write_node.self_entry(path).value = a_value
27
+ end
28
+
29
+ def write_node
30
+ root_node.write_node || root_node
31
+ end
26
32
 
27
33
  private
28
34
 
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'addressable'
4
+ require 'eac_config/node'
5
+ require 'eac_ruby_utils/core_ext'
6
+
7
+ module EacConfig
8
+ # A node that read/write entries from environment variables.
9
+ class EnvvarsNode
10
+ require_sub __FILE__
11
+ include ::EacConfig::Node
12
+
13
+ URI = ::Addressable::URI.parse('self://envvars')
14
+
15
+ class << self
16
+ def from_uri(uri)
17
+ return new if uri == URI
18
+ end
19
+ end
20
+
21
+ def url
22
+ URI
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_config/entry_path'
4
+ require 'eac_config/node_entry'
5
+ require 'eac_ruby_utils/blank_not_blank'
6
+ require 'eac_ruby_utils/core_ext'
7
+ require 'eac_ruby_utils/yaml'
8
+
9
+ module EacConfig
10
+ class EnvvarsNode
11
+ class Entry < ::EacConfig::NodeEntry
12
+ class << self
13
+ def entry_path_to_envvar_name(path)
14
+ ::EacConfig::EntryPath.assert(path).parts.join('_').gsub(/[^a-z0-9_]/i, '')
15
+ .gsub(/\A_+/, '').gsub(/_+\z/, '').gsub(/_{2,}/, '_').upcase
16
+ end
17
+
18
+ def from_value(string)
19
+ return nil if string.nil?
20
+ return ::EacRubyUtils::Yaml.load(string) if string.start_with?('---')
21
+
22
+ string
23
+ end
24
+
25
+ def to_value(object)
26
+ return nil if object.nil?
27
+ return object if object.is_a?(String)
28
+
29
+ ::EacRubyUtils::Yaml.dump(object)
30
+ end
31
+ end
32
+
33
+ enable_simple_cache
34
+
35
+ def found?
36
+ ENV.key?(envvar_name)
37
+ end
38
+
39
+ def value
40
+ self.class.from_value(ENV[envvar_name])
41
+ end
42
+
43
+ def value=(a_value)
44
+ ENV[envvar_name] = self.class.to_value(a_value)
45
+ end
46
+
47
+ private
48
+
49
+ def envvar_name_uncached
50
+ self.class.entry_path_to_envvar_name(path)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -6,17 +6,6 @@ require 'eac_ruby_utils/core_ext'
6
6
  module EacConfig
7
7
  class LoadPath
8
8
  ENTRY_PATH = ::EacConfig::EntryPath.assert(%w[load_path])
9
- PATH_SEPARATOR = ':'
10
-
11
- class << self
12
- def paths_to_string(paths)
13
- paths.map(&:to_s).join(PATH_SEPARATOR)
14
- end
15
-
16
- def string_to_paths(string)
17
- string.to_s.split(PATH_SEPARATOR)
18
- end
19
- end
20
9
 
21
10
  common_constructor :node
22
11
 
@@ -26,11 +15,14 @@ module EacConfig
26
15
 
27
16
  # @return [Array<String>]
28
17
  def paths
29
- self.class.string_to_paths(entry.value)
18
+ r = entry.value
19
+ r.is_a?(::Array) ? r : []
30
20
  end
31
21
 
32
22
  def push(new_path)
33
- entry.value = self.class.paths_to_string(paths + [new_path])
23
+ entry.value = paths + [new_path]
34
24
  end
25
+
26
+ delegate :to_s, to: :paths
35
27
  end
36
28
  end
@@ -5,10 +5,13 @@ require 'eac_config/entry_path'
5
5
  require 'eac_config/load_path'
6
6
  require 'eac_config/load_nodes_search'
7
7
  require 'eac_config/node_entry'
8
+ require 'eac_config/node_uri'
8
9
  require 'eac_ruby_utils/core_ext'
9
10
 
10
11
  module EacConfig
11
12
  module Node
13
+ attr_accessor :write_node
14
+
12
15
  common_concern do
13
16
  enable_abstract_methods
14
17
  include ::Comparable
@@ -31,7 +34,11 @@ module EacConfig
31
34
  # Return a entry which search values only in the self node.
32
35
  # @return [EacConfig::NodeEntry]
33
36
  def self_entry(path)
34
- ::EacConfig::NodeEntry.new(self, path)
37
+ self_entry_class.new(self, path)
38
+ end
39
+
40
+ def self_entry_class
41
+ self.class.const_get('Entry')
35
42
  end
36
43
 
37
44
  # @return [Array<EacConfig::Node>]
@@ -47,7 +54,7 @@ module EacConfig
47
54
  private
48
55
 
49
56
  def load_node(node_path)
50
- self.class.new(node_path.to_pathname.expand_path(path.parent))
57
+ ::EacConfig::NodeUri.new(node_path, url).instanciate
51
58
  end
52
59
  end
53
60
  end
@@ -7,33 +7,12 @@ require 'eac_ruby_utils/core_ext'
7
7
  module EacConfig
8
8
  # A entry which search values only in the source node.
9
9
  class NodeEntry
10
+ enable_abstract_methods
10
11
  enable_simple_cache
11
12
  common_constructor :node, :path do
12
13
  self.path = ::EacConfig::EntryPath.assert(path)
13
14
  end
14
15
 
15
- def found?
16
- paths_hash.key?(to_paths_hash_key)
17
- end
18
-
19
- def value
20
- paths_hash[to_paths_hash_key]
21
- end
22
-
23
- def value=(a_value)
24
- paths_hash[to_paths_hash_key] = a_value
25
- node.persist_data(paths_hash.root.to_h)
26
- end
27
-
28
- private
29
-
30
- # @return [EacConfig::PathsHash]
31
- def paths_hash_uncached
32
- ::EacConfig::PathsHash.new(node.data)
33
- end
34
-
35
- def to_paths_hash_key
36
- path.parts.join('.')
37
- end
16
+ abstract_methods :found?, :value, :value=
38
17
  end
39
18
  end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacConfig
4
+ class NodeUri
5
+ enable_simple_cache
6
+ common_constructor :source, :loader_uri, default: [nil]
7
+
8
+ def available_node_classes
9
+ require 'eac_config/envvars_node'
10
+ require 'eac_config/yaml_file_node'
11
+ [::EacConfig::EnvvarsNode, ::EacConfig::YamlFileNode]
12
+ end
13
+
14
+ def instanciate
15
+ available_node_classes.lazy.map { |k| k.from_uri(self) }.find(&:present?) ||
16
+ raise("No class mapped for \"#{to_addressable}\"")
17
+ end
18
+
19
+ delegate :to_s, to: :to_addressable
20
+
21
+ private
22
+
23
+ def to_addressable_uncached
24
+ r = ::Addressable::URI.parse(source)
25
+ path = r.path.to_pathname
26
+ r.path = path.expand_path(loader_uri_path_directory).to_path if path.relative?
27
+ r.scheme = 'file' if r.scheme.blank?
28
+ r
29
+ end
30
+
31
+ def loader_uri_path_directory
32
+ r = loader_uri.path
33
+ raise ".loader_uri \"#{loader_uri}\" has no path (Source: \"#{source}\")" if r.blank?
34
+
35
+ r.to_pathname.parent
36
+ end
37
+ end
38
+ end
@@ -8,6 +8,7 @@ require 'eac_config/paths_hash'
8
8
  require 'eac_ruby_utils/simple_cache'
9
9
 
10
10
  module EacConfig
11
+ # @deprecated Use {EacConfig::YamlFileNode} instead.
11
12
  class OldConfigs
12
13
  include ::EacRubyUtils::SimpleCache
13
14
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacConfig
4
- VERSION = '0.3.0'
4
+ VERSION = '0.4.0'
5
5
  end
@@ -7,20 +7,27 @@ require 'eac_ruby_utils/yaml'
7
7
 
8
8
  module EacConfig
9
9
  class YamlFileNode
10
+ require_sub __FILE__
10
11
  include ::EacConfig::Node
11
12
 
13
+ class << self
14
+ def from_uri(uri)
15
+ return new(uri.to_addressable.path) if uri.to_addressable.scheme == 'file'
16
+ end
17
+ end
18
+
12
19
  common_constructor :path do
13
20
  self.path = path.to_pathname
14
21
  end
15
22
 
16
23
  def data
17
- @data ||= ::EacRubyUtils::Yaml.load_file(assert_path)
24
+ @data ||= ::EacRubyUtils::Yaml.load_file(assert_path) || {}
18
25
  end
19
26
 
20
27
  def persist_data(new_data)
21
28
  path.parent.mkpath
22
29
  ::EacRubyUtils::Yaml.dump_file(path, new_data)
23
- reset_cache(:data)
30
+ @data = nil
24
31
  end
25
32
 
26
33
  def url
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_config/entry_path'
4
+ require 'eac_config/node_entry'
5
+ require 'eac_ruby_utils/core_ext'
6
+
7
+ module EacConfig
8
+ class YamlFileNode
9
+ class Entry < ::EacConfig::NodeEntry
10
+ enable_simple_cache
11
+
12
+ def found?
13
+ paths_hash.key?(to_paths_hash_key)
14
+ end
15
+
16
+ def value
17
+ paths_hash[to_paths_hash_key]
18
+ end
19
+
20
+ def value=(a_value)
21
+ paths_hash[to_paths_hash_key] = a_value
22
+ node.persist_data(paths_hash.root.to_h)
23
+ end
24
+
25
+ private
26
+
27
+ # @return [EacConfig::PathsHash]
28
+ def paths_hash_uncached
29
+ ::EacConfig::PathsHash.new(node.data)
30
+ end
31
+
32
+ def to_paths_hash_key
33
+ path.parts.join('.')
34
+ end
35
+ end
36
+ end
37
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eac_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Put here the authors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-23 00:00:00.000000000 Z
11
+ date: 2021-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -61,10 +61,13 @@ files:
61
61
  - lib/eac_config.rb
62
62
  - lib/eac_config/entry.rb
63
63
  - lib/eac_config/entry_path.rb
64
+ - lib/eac_config/envvars_node.rb
65
+ - lib/eac_config/envvars_node/entry.rb
64
66
  - lib/eac_config/load_nodes_search.rb
65
67
  - lib/eac_config/load_path.rb
66
68
  - lib/eac_config/node.rb
67
69
  - lib/eac_config/node_entry.rb
70
+ - lib/eac_config/node_uri.rb
68
71
  - lib/eac_config/old_configs.rb
69
72
  - lib/eac_config/old_configs/base.rb
70
73
  - lib/eac_config/old_configs/file.rb
@@ -74,6 +77,7 @@ files:
74
77
  - lib/eac_config/paths_hash/path_search.rb
75
78
  - lib/eac_config/version.rb
76
79
  - lib/eac_config/yaml_file_node.rb
80
+ - lib/eac_config/yaml_file_node/entry.rb
77
81
  homepage:
78
82
  licenses: []
79
83
  metadata: {}