open_config 1.0.0 → 2.0.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: 4b06d211bb7c9b53084433d03ea7a088c82613e130ef35bb9ea1f28310cd273a
4
- data.tar.gz: ca95f738c6edd0cb589a42f58d6279d78b33de03368b2b15338973d366b04c52
3
+ metadata.gz: 7485c04b85ec19a76557a59fa05580e389331654256034244355f7170f0c40ee
4
+ data.tar.gz: 005d269f4976b68b409d066191b084bed432ee56ea68778a9f9ad40e45c83180
5
5
  SHA512:
6
- metadata.gz: 3f4beb18ba70d288d99e90191dfc51afab9292e3bd9730dc165dc6244a07eac2fd7b254ffd6535535bd647135e35f6b928b5a4e99abb2b91a3e5c96049cb8b8f
7
- data.tar.gz: f6a6e7a9753a45dbc3e9074f39837b59f43a7dc30a95ba0f6f8a23ee609568a009bb0fe1a30dd449fe1df6e3add36b4909ee0332217b4aacbb02bbf3d2250532
6
+ metadata.gz: bdfce39a72ffb67790e54a84a9bc0c5f1257ce81baeb28fcef4061798b992ecc6dfcfca8e13267f0b6b67a408ad4c0289cfd6bc94b1c614dc41b86d57190d7a8
7
+ data.tar.gz: bc6fcf31e3082f58112750eb39ca6d7d2d3d2b643190651b98fc200f27cedc96ba49a56085a2bef05e9feba482a71fd1907a4c70b915dc0bff72b7afbd953210
@@ -1,41 +1,33 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module OpenConfig
4
- class Base
5
- attr_reader :file_path
6
-
7
- def initialize(root:, file:)
8
- @file_path = File.join(root, file)
9
- end
10
-
11
- def method_missing(method_name, *arguments, &block)
12
- return super unless configuration_method?(method_name)
3
+ require 'delegate'
13
4
 
14
- tree.public_send(method_name, *arguments, &block)
5
+ module OpenConfig
6
+ class Base < DelegateClass(Node)
7
+ def initialize(path)
8
+ @path = path
9
+ super(build_config)
15
10
  end
16
11
 
17
- def respond_to_missing?(method_name, include_private = false)
18
- configuration_method?(method_name) || super
12
+ def ==(other)
13
+ return false unless other.kind_of?(OpenConfig::Base)
14
+ table! == other.table!
19
15
  end
20
16
 
21
17
  private
22
18
 
23
- def configuration_method?(method_name)
24
- tree.respond_to?(method_name)
25
- end
26
-
27
- def tree
28
- @tree ||= parse_file(read_file)
19
+ def build_config
20
+ parse_file(read_file)
29
21
  end
30
22
 
31
23
  def parse_file(_file_content)
32
- raise NotImplementedError, 'Subclassed must implement #parse_file'
24
+ raise NotImplementedError, 'Subclasses must implement #parse_file'
33
25
  end
34
26
 
35
27
  def read_file
36
- raise OpenConfig::Errors::FileNotFound, file_path unless File.exists?(file_path)
28
+ raise OpenConfig::Errors::FileNotFound, @path unless File.exists?(@path)
37
29
 
38
- File.new(file_path).read
30
+ File.read(@path)
39
31
  end
40
32
  end
41
33
  end
@@ -3,11 +3,11 @@
3
3
  require 'json'
4
4
 
5
5
  module OpenConfig
6
- class JSON < OpenConfig::Base
6
+ class JSON < Base
7
7
  private
8
8
 
9
9
  def parse_file(file_content)
10
- ::JSON.parse(file_content, object_class: OpenStruct)
10
+ ::JSON.parse(file_content, object_class: OpenConfig::Node)
11
11
  end
12
12
  end
13
13
  end
@@ -5,19 +5,23 @@ require 'json'
5
5
  require 'erb'
6
6
 
7
7
  module OpenConfig
8
- class YAML < OpenConfig::Base
8
+ class YAML < Base
9
9
  private
10
10
 
11
11
  def parse_file(file_content)
12
- ::JSON.parse(build_json(file_content), object_class: OpenStruct)
12
+ ::JSON.parse(load_yaml(file_content).to_json, object_class: OpenConfig::Node)
13
13
  end
14
14
 
15
- def build_json(file_content)
16
- ::YAML.safe_load(load_erb(file_content)).to_json
15
+ def load_yaml(file_content)
16
+ begin
17
+ ::YAML.load(compile(file_content), aliases: true)
18
+ rescue ArgumentError
19
+ ::YAML.load(compile(file_content))
20
+ end
17
21
  end
18
22
 
19
- def load_erb(file_content)
20
- ::ERB.new(file_content).result
23
+ def compile(file_content)
24
+ defined?(::ERB) ? ::ERB.new(file_content).result : file_content
21
25
  end
22
26
  end
23
27
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'files/base'
4
- require_relative 'files/yaml'
5
- require_relative 'files/json'
3
+ require 'open_config/files/base'
4
+ require 'open_config/files/yaml'
5
+ require 'open_config/files/json'
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ostruct'
4
+
5
+ module OpenConfig
6
+ class Node < OpenStruct
7
+ def fetch(name, *args, &block)
8
+ begin
9
+ name = name.to_sym
10
+ rescue NoMethodError
11
+ raise! TypeError, "#{name} is not a symbol nor a string"
12
+ end
13
+
14
+ table.fetch(name, *args, &block)
15
+ end
16
+
17
+ def to_h
18
+ super.transform_values do |value|
19
+ value.instance_of?(self.class) ? value.to_h : value
20
+ end
21
+ end
22
+
23
+ def pretty_print(q)
24
+ q.object_group self do
25
+ q.nest(1) do
26
+ q.breakable
27
+ q.seplist(table, nil, :each) do |key, value|
28
+ q.group do
29
+ q.text key.to_s
30
+ q.text ': '
31
+ q.pp value
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenConfig
4
- VERSION = '1.0.0'
4
+ VERSION = '2.0.0'
5
5
  end
data/lib/open_config.rb CHANGED
@@ -1,8 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'open_config/version'
4
- require_relative 'open_config/errors'
5
- require_relative 'open_config/files'
3
+ require 'open_config/version'
4
+ require 'open_config/node'
5
+ require 'open_config/files'
6
+ require 'open_config/errors'
6
7
 
7
8
  module OpenConfig
8
9
  end
metadata CHANGED
@@ -1,72 +1,93 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Egor Iskrenkov
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-28 00:00:00.000000000 Z
11
+ date: 2022-05-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: pry
14
+ name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 0.13.1
19
+ version: '1.15'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 0.13.1
27
- description:
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '3.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '3.8'
55
+ description:
28
56
  email:
29
- - e.iskrenkov@gmail.com
30
- executables:
31
- - console
57
+ - egor@iskrenkov.me
58
+ executables: []
32
59
  extensions: []
33
60
  extra_rdoc_files: []
34
61
  files:
35
- - ".gitignore"
36
- - Gemfile
37
- - Gemfile.lock
38
- - LICENSE
39
- - README.md
40
- - bin/console
41
62
  - lib/open_config.rb
42
63
  - lib/open_config/errors.rb
43
64
  - lib/open_config/files.rb
44
65
  - lib/open_config/files/base.rb
45
66
  - lib/open_config/files/json.rb
46
67
  - lib/open_config/files/yaml.rb
68
+ - lib/open_config/node.rb
47
69
  - lib/open_config/version.rb
48
- - open_config.gemspec
49
70
  homepage: https://github.com/eiskrenkov/open_config
50
71
  licenses:
51
72
  - MIT
52
73
  metadata: {}
53
- post_install_message:
74
+ post_install_message:
54
75
  rdoc_options: []
55
76
  require_paths:
56
77
  - lib
57
78
  required_ruby_version: !ruby/object:Gem::Requirement
58
79
  requirements:
59
- - - "~>"
80
+ - - ">="
60
81
  - !ruby/object:Gem::Version
61
- version: 3.0.0
82
+ version: '2.4'
62
83
  required_rubygems_version: !ruby/object:Gem::Requirement
63
84
  requirements:
64
85
  - - ">="
65
86
  - !ruby/object:Gem::Version
66
87
  version: '0'
67
88
  requirements: []
68
- rubygems_version: 3.2.3
69
- signing_key:
89
+ rubygems_version: 3.3.7
90
+ signing_key:
70
91
  specification_version: 4
71
92
  summary: Building OpenStruct trees from configuration files
72
93
  test_files: []
data/.gitignore DELETED
@@ -1 +0,0 @@
1
- *.gem
data/Gemfile DELETED
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source 'https://rubygems.org'
4
-
5
- gemspec
data/Gemfile.lock DELETED
@@ -1,23 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- open_config (0.1.0)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- coderay (1.1.3)
10
- method_source (1.0.0)
11
- pry (0.13.1)
12
- coderay (~> 1.1)
13
- method_source (~> 1.0)
14
-
15
- PLATFORMS
16
- x86_64-darwin-20
17
-
18
- DEPENDENCIES
19
- open_config!
20
- pry (~> 0.13.1)
21
-
22
- BUNDLED WITH
23
- 2.2.3
data/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2021 Egor Iskrenkov
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
data/README.md DELETED
@@ -1,29 +0,0 @@
1
- # OpenConfig
2
- It allows you to build OpenStruct trees from your configuration files
3
-
4
- ## Installation
5
- Install OpenConfig on your machine as any other Ruby gem
6
-
7
- ```sh
8
- $ gem install open_config
9
- ```
10
-
11
- ## Usage
12
- Imagine, you have file, called `configuration.yml` in your project's config folder:
13
-
14
- ```yaml
15
- development:
16
- key: value
17
- another_key: 123
18
- production:
19
- key: production_value
20
- another_key: 321
21
- ```
22
- You can create OpenConfig instance `open_config = OpenConfig::YAML.new(root: 'config', file: 'configuration.yml')`, and access configuration keys on any depth by simple method calls:
23
-
24
- ```ruby
25
- pry(main)> open_config.production.another_key
26
- => 321
27
- ```
28
-
29
- Same thing will work with `configuration.json`, just use `OpenConfig::JSON` instead
data/bin/console DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require 'bundler/setup'
5
- require 'open_config'
6
- require 'pry'
7
-
8
- Pry.start
data/open_config.gemspec DELETED
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'lib/open_config/version'
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = 'open_config'
7
- spec.version = OpenConfig::VERSION
8
- spec.license = 'MIT'
9
-
10
- spec.authors = ['Egor Iskrenkov']
11
- spec.email = ['e.iskrenkov@gmail.com']
12
-
13
- spec.summary = 'Building OpenStruct trees from configuration files'
14
- spec.homepage = 'https://github.com/eiskrenkov/open_config'
15
-
16
- spec.files = `git ls-files`.split($RS).reject { |f| f.match(%r{^spec/}) }
17
- spec.bindir = 'bin'
18
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
- spec.require_paths = ['lib']
20
-
21
- spec.required_ruby_version = Gem::Requirement.new('~> 3.0.0')
22
-
23
- # OpenConfig dependencies
24
- spec.add_development_dependency 'pry', '~> 0.13.1'
25
- end