eac_config 0.2.0 → 0.3.0

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: 1146b6d7dcccd0749b9ac875dceb8b521e386cc4468ffeab106f33e29e254a85
4
+ data.tar.gz: b4da054e60c2c5316670d047869ce98c3ca4255fc1df62db9b5ef5f303bdd5e8
5
5
  SHA512:
6
- metadata.gz: c29cc31ab4a5c6a834af31f12986c0e8e3b862d6603d117b7a3ca5792465636693542ae13417764c23fc2b6cfdd834bda7db0915d419157b329aec836fc7d2b2
7
- data.tar.gz: 2390811743a6a997ee6d0ec2bba40ca5f27222ac14d352a51eb508e0e345c3f2190a12105bb3e9cfd63dc45052cdbe41d0c3c4df2dcf319103ec00f16db162a1
6
+ metadata.gz: e3c47344815bd911603f54a37581a6be9b7182f87d3b2546e708f903a5da3bb7799da013c189db7a3f3d0572e64b4124562a43f9ebe54c0e88805d12fa94503a
7
+ data.tar.gz: 5f6ba2fb942129f1b8b5d47ab1e1693d025303f501ebb6e7f0a25109369beaa99dfcc420e08f5d8c50ffb495bf52c287f6491a5d4b11014c1c051f21c4cd272b
@@ -5,10 +5,39 @@ 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
+ delegate :value=, to: :root_node
26
+
27
+ private
28
+
29
+ def node_entry_uncached
30
+ node_entry_from_root || node_entry_from_load_path
31
+ end
32
+
33
+ def node_entry_from_load_path_uncached
34
+ root_node.recursive_loaded_nodes.lazy.map { |loaded_node| loaded_node.self_entry(path) }
35
+ .find(&:found?)
36
+ end
37
+
38
+ def node_entry_from_root_uncached
39
+ e = root_node.self_entry(path)
40
+ e.found? ? e : nil
12
41
  end
13
42
  end
14
43
  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,36 @@
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
+ 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
+
21
+ common_constructor :node
22
+
23
+ def entry
24
+ node.self_entry(ENTRY_PATH)
25
+ end
26
+
27
+ # @return [Array<String>]
28
+ def paths
29
+ self.class.string_to_paths(entry.value)
30
+ end
31
+
32
+ def push(new_path)
33
+ entry.value = self.class.paths_to_string(paths + [new_path])
34
+ end
35
+ end
36
+ end
@@ -1,7 +1,10 @@
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'
5
8
  require 'eac_ruby_utils/core_ext'
6
9
 
7
10
  module EacConfig
@@ -11,11 +14,13 @@ module EacConfig
11
14
  include ::Comparable
12
15
  end
13
16
 
14
- LOAD_PATH_ENTRY_PATH = ::EacConfig::EntryPath.assert(%w[load_path])
15
- LOAD_PATH_PATH_SEPARATOR = ':'
16
-
17
17
  def entry(path)
18
- ::EacConfig::EntrySearch.new(self, ::EacConfig::EntryPath.assert(path)).result
18
+ ::EacConfig::Entry.new(self, path)
19
+ end
20
+
21
+ # @return [[EacConfig::IncludePath]]
22
+ def load_path
23
+ @load_path ||= ::EacConfig::LoadPath.new(self)
19
24
  end
20
25
 
21
26
  # @return [Addressable::URI]
@@ -23,12 +28,15 @@ module EacConfig
23
28
  raise_abstract_method(__method__)
24
29
  end
25
30
 
31
+ # Return a entry which search values only in the self node.
32
+ # @return [EacConfig::NodeEntry]
33
+ def self_entry(path)
34
+ ::EacConfig::NodeEntry.new(self, path)
35
+ end
36
+
26
37
  # @return [Array<EacConfig::Node>]
27
38
  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
39
+ load_path.paths.map { |node_path| load_node(node_path) }
32
40
  end
33
41
 
34
42
  # @return [Array<EacConfig::Node>]
@@ -0,0 +1,39 @@
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_simple_cache
11
+ common_constructor :node, :path do
12
+ self.path = ::EacConfig::EntryPath.assert(path)
13
+ end
14
+
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
38
+ end
39
+ end
@@ -0,0 +1,74 @@
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
+ class OldConfigs
12
+ include ::EacRubyUtils::SimpleCache
13
+
14
+ attr_reader :configs_key, :options
15
+
16
+ # Valid options: [:storage_path]
17
+ def initialize(configs_key, options = {})
18
+ @configs_key = configs_key
19
+ @options = options.to_sym_keys_hash.freeze
20
+ load
21
+ end
22
+
23
+ delegate :clear, to: :file
24
+
25
+ delegate :save, to: :file
26
+
27
+ delegate :load, to: :file
28
+
29
+ def []=(entry_key, entry_value)
30
+ write_entry(entry_key, entry_value)
31
+ end
32
+
33
+ def write_entry(entry_key, entry_value)
34
+ file.write(entry_key, entry_value)
35
+ end
36
+
37
+ def [](entry_key)
38
+ read_entry(entry_key)
39
+ end
40
+
41
+ def read_entry(entry_key)
42
+ file.read(entry_key)
43
+ end
44
+
45
+ delegate :autosave?, to: :file
46
+
47
+ private
48
+
49
+ attr_accessor :data
50
+
51
+ def file_uncached
52
+ ::EacConfig::OldConfigs::File.new(
53
+ storage_path, options
54
+ )
55
+ end
56
+
57
+ def storage_path_uncached
58
+ path = options_storage_path || default_storage_path
59
+ return path if ::File.exist?(path) && ::File.size(path).positive?
60
+
61
+ ::FileUtils.mkdir_p(::File.dirname(path))
62
+ ::File.write(path, {}.to_yaml)
63
+ path
64
+ end
65
+
66
+ def options_storage_path
67
+ options[:storage_path]
68
+ end
69
+
70
+ def default_storage_path
71
+ ::File.join(ENV['HOME'], '.config', configs_key, 'settings.yml')
72
+ end
73
+ end
74
+ 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.3.0'
5
5
  end
@@ -1,8 +1,6 @@
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'
@@ -16,11 +14,28 @@ module EacConfig
16
14
  end
17
15
 
18
16
  def data
19
- @data ||= ::EacRubyUtils::Yaml.load_file(path)
17
+ @data ||= ::EacRubyUtils::Yaml.load_file(assert_path)
18
+ end
19
+
20
+ def persist_data(new_data)
21
+ path.parent.mkpath
22
+ ::EacRubyUtils::Yaml.dump_file(path, new_data)
23
+ reset_cache(:data)
20
24
  end
21
25
 
22
26
  def url
23
27
  ::Addressable::URI.parse("file://#{path.expand_path}")
24
28
  end
29
+
30
+ private
31
+
32
+ def assert_path
33
+ unless path.file?
34
+ raise("\"#{path}\" is a not a file") if path.exist?
35
+
36
+ persist_data({})
37
+ end
38
+ path
39
+ end
25
40
  end
26
41
  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.3.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-16 00:00:00.000000000 Z
11
+ date: 2021-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.62'
33
+ version: '0.64'
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.64'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: eac_ruby_gem_support
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -61,9 +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/entry_search.rb
65
64
  - lib/eac_config/load_nodes_search.rb
65
+ - lib/eac_config/load_path.rb
66
66
  - lib/eac_config/node.rb
67
+ - lib/eac_config/node_entry.rb
68
+ - lib/eac_config/old_configs.rb
69
+ - lib/eac_config/old_configs/base.rb
70
+ - lib/eac_config/old_configs/file.rb
67
71
  - lib/eac_config/paths_hash.rb
68
72
  - lib/eac_config/paths_hash/entry_key_error.rb
69
73
  - lib/eac_config/paths_hash/node.rb
@@ -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