eac_config 0.2.0 → 0.5.1

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: fd6a6d3c5ab71a03c476e9d3237623e6d9911128c27301c73dcaf3fdc3e49e33
4
- data.tar.gz: fd1d1139b115073c1f7bcec332dcdbbcd92c9ed7825acff34eaa7c3b76747e04
3
+ metadata.gz: 6a404b0d89fc683f1632c29e760ac77ac204a89de0dc979f3dc4a14365443859
4
+ data.tar.gz: 2efe809b7f1c5ccd6891c668ed35535d9096c26d3fd72eebbff0d3bae9c8e4a0
5
5
  SHA512:
6
- metadata.gz: c29cc31ab4a5c6a834af31f12986c0e8e3b862d6603d117b7a3ca5792465636693542ae13417764c23fc2b6cfdd834bda7db0915d419157b329aec836fc7d2b2
7
- data.tar.gz: 2390811743a6a997ee6d0ec2bba40ca5f27222ac14d352a51eb508e0e345c3f2190a12105bb3e9cfd63dc45052cdbe41d0c3c4df2dcf319103ec00f16db162a1
6
+ metadata.gz: dbbbbd172244973867db06006fb0d5a2a6bf48675ef5806b742aeb7e9fdde0c83153f4de46d4ebe1bc9a38ebebf91fffd4d4d7e57d75a2ffee9958f1d6973e21
7
+ data.tar.gz: 00d7a496a83302ed6ec13f691a18ef82947c1bf8f6b9d7dfd93e90fda649d2661aa1b4ed15b94602ff0caaa5bf9bc794aa817dc491cdfaa85c97932244d2a036
@@ -5,10 +5,45 @@ require 'eac_ruby_utils/core_ext'
5
5
 
6
6
  module EacConfig
7
7
  class Entry
8
- common_constructor :source_node, :path, :found, :value
8
+ enable_simple_cache
9
+ common_constructor :root_node, :path do
10
+ self.path = ::EacConfig::EntryPath.assert(path)
11
+ end
9
12
 
10
13
  def found?
11
- found
14
+ node_entry.if_present(false, &:found?)
15
+ end
16
+
17
+ def found_node
18
+ node_entry.if_present(&:node)
19
+ end
20
+
21
+ def value
22
+ node_entry.if_present(&:value)
23
+ end
24
+
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
32
+
33
+ private
34
+
35
+ def node_entry_uncached
36
+ node_entry_from_root || node_entry_from_load_path
37
+ end
38
+
39
+ def node_entry_from_load_path_uncached
40
+ root_node.recursive_loaded_nodes.lazy.map { |loaded_node| loaded_node.self_entry(path) }
41
+ .find(&:found?)
42
+ end
43
+
44
+ def node_entry_from_root_uncached
45
+ e = root_node.self_entry(path)
46
+ e.found? ? e : nil
12
47
  end
13
48
  end
14
49
  end
@@ -33,5 +33,9 @@ module EacConfig
33
33
  def initialize(parts)
34
34
  @parts = parts.to_a.freeze
35
35
  end
36
+
37
+ def to_s
38
+ "#{self.class}[#{parts.join(PART_SEPARATOR)}]"
39
+ end
36
40
  end
37
41
  end
@@ -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
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_config/entry_path'
4
+ require 'eac_ruby_utils/core_ext'
5
+
6
+ module EacConfig
7
+ class LoadPath
8
+ ENTRY_PATH = ::EacConfig::EntryPath.assert(%w[load_path])
9
+
10
+ common_constructor :node
11
+
12
+ def entry
13
+ node.self_entry(ENTRY_PATH)
14
+ end
15
+
16
+ # @return [Array<String>]
17
+ def paths
18
+ r = entry.value
19
+ r.is_a?(::Array) ? r : []
20
+ end
21
+
22
+ def push(new_path)
23
+ entry.value = paths + [new_path]
24
+ end
25
+
26
+ delegate :to_s, to: :paths
27
+ end
28
+ end
@@ -1,21 +1,36 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'eac_config/entry'
3
4
  require 'eac_config/entry_path'
5
+ require 'eac_config/load_path'
4
6
  require 'eac_config/load_nodes_search'
7
+ require 'eac_config/node_entry'
8
+ require 'eac_config/node_uri'
5
9
  require 'eac_ruby_utils/core_ext'
10
+ require 'eac_ruby_utils/context'
6
11
 
7
12
  module EacConfig
8
13
  module Node
14
+ class << self
15
+ def context
16
+ @context ||= ::EacRubyUtils::Context.new
17
+ end
18
+ end
19
+
20
+ attr_accessor :write_node
21
+
9
22
  common_concern do
10
23
  enable_abstract_methods
11
24
  include ::Comparable
12
25
  end
13
26
 
14
- LOAD_PATH_ENTRY_PATH = ::EacConfig::EntryPath.assert(%w[load_path])
15
- LOAD_PATH_PATH_SEPARATOR = ':'
16
-
17
27
  def entry(path)
18
- ::EacConfig::EntrySearch.new(self, ::EacConfig::EntryPath.assert(path)).result
28
+ ::EacConfig::Entry.new(self, path)
29
+ end
30
+
31
+ # @return [[EacConfig::IncludePath]]
32
+ def load_path
33
+ @load_path ||= ::EacConfig::LoadPath.new(self)
19
34
  end
20
35
 
21
36
  # @return [Addressable::URI]
@@ -23,12 +38,19 @@ module EacConfig
23
38
  raise_abstract_method(__method__)
24
39
  end
25
40
 
41
+ # Return a entry which search values only in the self node.
42
+ # @return [EacConfig::NodeEntry]
43
+ def self_entry(path)
44
+ self_entry_class.new(self, path)
45
+ end
46
+
47
+ def self_entry_class
48
+ self.class.const_get('Entry')
49
+ end
50
+
26
51
  # @return [Array<EacConfig::Node>]
27
52
  def self_loaded_nodes
28
- ::EacConfig::EntrySearch.new(self, ::EacConfig::EntryPath.assert(LOAD_PATH_ENTRY_PATH))
29
- .result_from_self.if_present([]) do |e|
30
- e.value.split(LOAD_PATH_PATH_SEPARATOR).map { |node_path| load_node(node_path) }
31
- end
53
+ load_path.paths.map { |node_path| load_node(node_path) }
32
54
  end
33
55
 
34
56
  # @return [Array<EacConfig::Node>]
@@ -39,7 +61,7 @@ module EacConfig
39
61
  private
40
62
 
41
63
  def load_node(node_path)
42
- self.class.new(node_path.to_pathname.expand_path(path.parent))
64
+ ::EacConfig::NodeUri.new(node_path, url).instanciate
43
65
  end
44
66
  end
45
67
  end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_config/entry_path'
4
+ require 'eac_config/paths_hash'
5
+ require 'eac_ruby_utils/core_ext'
6
+
7
+ module EacConfig
8
+ # A entry which search values only in the source node.
9
+ class NodeEntry
10
+ enable_abstract_methods
11
+ enable_simple_cache
12
+ common_constructor :node, :path do
13
+ self.path = ::EacConfig::EntryPath.assert(path)
14
+ end
15
+
16
+ abstract_methods :found?, :value, :value=
17
+ end
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
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/string'
4
+ require 'yaml'
5
+ require 'eac_config/old_configs/file'
6
+ require 'eac_ruby_utils/patches/hash/sym_keys_hash'
7
+ require 'eac_config/paths_hash'
8
+ require 'eac_ruby_utils/simple_cache'
9
+
10
+ module EacConfig
11
+ # @deprecated Use {EacConfig::YamlFileNode} instead.
12
+ class OldConfigs
13
+ include ::EacRubyUtils::SimpleCache
14
+
15
+ attr_reader :configs_key, :options
16
+
17
+ # Valid options: [:storage_path]
18
+ def initialize(configs_key, options = {})
19
+ @configs_key = configs_key
20
+ @options = options.to_sym_keys_hash.freeze
21
+ load
22
+ end
23
+
24
+ delegate :clear, to: :file
25
+
26
+ delegate :save, to: :file
27
+
28
+ delegate :load, to: :file
29
+
30
+ def []=(entry_key, entry_value)
31
+ write_entry(entry_key, entry_value)
32
+ end
33
+
34
+ def write_entry(entry_key, entry_value)
35
+ file.write(entry_key, entry_value)
36
+ end
37
+
38
+ def [](entry_key)
39
+ read_entry(entry_key)
40
+ end
41
+
42
+ def read_entry(entry_key)
43
+ file.read(entry_key)
44
+ end
45
+
46
+ delegate :autosave?, to: :file
47
+
48
+ private
49
+
50
+ attr_accessor :data
51
+
52
+ def file_uncached
53
+ ::EacConfig::OldConfigs::File.new(
54
+ storage_path, options
55
+ )
56
+ end
57
+
58
+ def storage_path_uncached
59
+ path = options_storage_path || default_storage_path
60
+ return path if ::File.exist?(path) && ::File.size(path).positive?
61
+
62
+ ::FileUtils.mkdir_p(::File.dirname(path))
63
+ ::File.write(path, {}.to_yaml)
64
+ path
65
+ end
66
+
67
+ def options_storage_path
68
+ options[:storage_path]
69
+ end
70
+
71
+ def default_storage_path
72
+ ::File.join(ENV['HOME'], '.config', configs_key, 'settings.yml')
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/blank_not_blank'
4
+ require 'eac_ruby_utils/core_ext'
5
+ require 'eac_config/paths_hash'
6
+
7
+ module EacConfig
8
+ class OldConfigs
9
+ class Base
10
+ enable_simple_cache
11
+
12
+ common_constructor :data, default: [{}] do
13
+ self.data = ::EacConfig::PathsHash.new(data)
14
+ end
15
+
16
+ def []=(entry_key, entry_value)
17
+ write(entry_key, entry_value)
18
+ end
19
+
20
+ def [](entry_key)
21
+ read(entry_key)
22
+ end
23
+
24
+ def clear
25
+ replace({})
26
+ end
27
+
28
+ def read(entry_key)
29
+ return nil unless data.key?(entry_key)
30
+
31
+ data.fetch(entry_key).if_present(::EacRubyUtils::BlankNotBlank.instance)
32
+ end
33
+
34
+ def replace(new_data)
35
+ self.data = ::EacConfig::PathsHash.new(new_data)
36
+ end
37
+
38
+ def write(entry_key, entry_value)
39
+ data[entry_key] = entry_value
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_config/old_configs/base'
4
+ require 'yaml'
5
+
6
+ module EacConfig
7
+ class OldConfigs
8
+ class File < ::EacConfig::OldConfigs::Base
9
+ attr_reader :path, :options
10
+
11
+ # Valid options: [:autosave]
12
+ def initialize(path, options = {})
13
+ @path = path
14
+ @options = options.to_sym_keys_hash.freeze
15
+ super(raw_data_from_file)
16
+ end
17
+
18
+ def save
19
+ ::FileUtils.mkdir_p(::File.dirname(path))
20
+ ::File.write(path, data.to_h.to_yaml)
21
+ end
22
+
23
+ def load
24
+ replace(raw_data_from_file)
25
+ end
26
+
27
+ def write(entry_key, entry_value)
28
+ super
29
+ save if autosave?
30
+ end
31
+
32
+ def autosave?
33
+ options[:autosave] ? true : false
34
+ end
35
+
36
+ private
37
+
38
+ def raw_data_from_file
39
+ if ::File.exist?(path) && ::File.size(path).positive?
40
+ ::YAML.load_file(path)
41
+ else
42
+ {}
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacConfig
4
- VERSION = '0.2.0'
4
+ VERSION = '0.5.1'
5
5
  end
@@ -1,26 +1,48 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'addressable'
4
- require 'eac_config/entry'
5
- require 'eac_config/entry_search'
6
4
  require 'eac_config/node'
7
5
  require 'eac_ruby_utils/core_ext'
8
6
  require 'eac_ruby_utils/yaml'
9
7
 
10
8
  module EacConfig
11
9
  class YamlFileNode
10
+ require_sub __FILE__
12
11
  include ::EacConfig::Node
13
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
+
14
19
  common_constructor :path do
15
20
  self.path = path.to_pathname
16
21
  end
17
22
 
18
23
  def data
19
- @data ||= ::EacRubyUtils::Yaml.load_file(path)
24
+ @data ||= ::EacRubyUtils::Yaml.load_file(assert_path) || {}
25
+ end
26
+
27
+ def persist_data(new_data)
28
+ path.parent.mkpath
29
+ ::EacRubyUtils::Yaml.dump_file(path, new_data)
30
+ @data = nil
20
31
  end
21
32
 
22
33
  def url
23
34
  ::Addressable::URI.parse("file://#{path.expand_path}")
24
35
  end
36
+
37
+ private
38
+
39
+ def assert_path
40
+ unless path.file?
41
+ raise("\"#{path}\" is a not a file") if path.exist?
42
+
43
+ persist_data({})
44
+ end
45
+ path
46
+ end
25
47
  end
26
48
  end
@@ -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.2.0
4
+ version: 0.5.1
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-16 00:00:00.000000000 Z
11
+ date: 2021-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -30,28 +30,28 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.62'
33
+ version: '0.70'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0.62'
40
+ version: '0.70'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: eac_ruby_gem_support
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0.2'
47
+ version: '0.3'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0.2'
54
+ version: '0.3'
55
55
  description:
56
56
  email:
57
57
  executables: []
@@ -61,15 +61,23 @@ 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/entry_search.rb
64
+ - lib/eac_config/envvars_node.rb
65
+ - lib/eac_config/envvars_node/entry.rb
65
66
  - lib/eac_config/load_nodes_search.rb
67
+ - lib/eac_config/load_path.rb
66
68
  - lib/eac_config/node.rb
69
+ - lib/eac_config/node_entry.rb
70
+ - lib/eac_config/node_uri.rb
71
+ - lib/eac_config/old_configs.rb
72
+ - lib/eac_config/old_configs/base.rb
73
+ - lib/eac_config/old_configs/file.rb
67
74
  - lib/eac_config/paths_hash.rb
68
75
  - lib/eac_config/paths_hash/entry_key_error.rb
69
76
  - lib/eac_config/paths_hash/node.rb
70
77
  - lib/eac_config/paths_hash/path_search.rb
71
78
  - lib/eac_config/version.rb
72
79
  - lib/eac_config/yaml_file_node.rb
80
+ - lib/eac_config/yaml_file_node/entry.rb
73
81
  homepage:
74
82
  licenses: []
75
83
  metadata: {}
@@ -88,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
96
  - !ruby/object:Gem::Version
89
97
  version: '0'
90
98
  requirements: []
91
- rubygems_version: 3.0.9
99
+ rubygems_version: 3.1.6
92
100
  signing_key:
93
101
  specification_version: 4
94
102
  summary: Put here de description.
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'eac_config/entry'
4
- require 'eac_config/paths_hash'
5
-
6
- module EacConfig
7
- class EntrySearch
8
- enable_simple_cache
9
- common_constructor :node, :path
10
-
11
- private
12
-
13
- def result_from_load_path_uncached
14
- node.recursive_loaded_nodes.lazy.map { |loaded_node| loaded_node.entry(path) }.find(&:found?)
15
- end
16
-
17
- def result_from_self_uncached
18
- return nil unless paths_hash.key?(to_paths_hash_key)
19
-
20
- ::EacConfig::Entry.new(node, path, true, paths_hash.fetch(to_paths_hash_key))
21
- end
22
-
23
- def result_not_found_uncached
24
- ::EacConfig::Entry.new(nil, path, false, nil)
25
- end
26
-
27
- # @return [EacConfig::Entry]
28
- def result_uncached
29
- result_from_self || result_from_load_path || result_not_found
30
- end
31
-
32
- # @return [EacConfig::PathsHash]
33
- def paths_hash_uncached
34
- ::EacConfig::PathsHash.new(node.data)
35
- end
36
-
37
- def to_paths_hash_key
38
- path.parts.join('.')
39
- end
40
- end
41
- end