open_config 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4b06d211bb7c9b53084433d03ea7a088c82613e130ef35bb9ea1f28310cd273a
4
+ data.tar.gz: ca95f738c6edd0cb589a42f58d6279d78b33de03368b2b15338973d366b04c52
5
+ SHA512:
6
+ metadata.gz: 3f4beb18ba70d288d99e90191dfc51afab9292e3bd9730dc165dc6244a07eac2fd7b254ffd6535535bd647135e35f6b928b5a4e99abb2b91a3e5c96049cb8b8f
7
+ data.tar.gz: f6a6e7a9753a45dbc3e9074f39837b59f43a7dc30a95ba0f6f8a23ee609568a009bb0fe1a30dd449fe1df6e3add36b4909ee0332217b4aacbb02bbf3d2250532
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,23 @@
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 ADDED
@@ -0,0 +1,21 @@
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 ADDED
@@ -0,0 +1,29 @@
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 ADDED
@@ -0,0 +1,8 @@
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
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'open_config/version'
4
+ require_relative 'open_config/errors'
5
+ require_relative 'open_config/files'
6
+
7
+ module OpenConfig
8
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenConfig
4
+ module Errors
5
+ class FileNotFound < StandardError
6
+ def initialize(path)
7
+ super("#{path} doesn't exist!")
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'files/base'
4
+ require_relative 'files/yaml'
5
+ require_relative 'files/json'
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
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)
13
+
14
+ tree.public_send(method_name, *arguments, &block)
15
+ end
16
+
17
+ def respond_to_missing?(method_name, include_private = false)
18
+ configuration_method?(method_name) || super
19
+ end
20
+
21
+ private
22
+
23
+ def configuration_method?(method_name)
24
+ tree.respond_to?(method_name)
25
+ end
26
+
27
+ def tree
28
+ @tree ||= parse_file(read_file)
29
+ end
30
+
31
+ def parse_file(_file_content)
32
+ raise NotImplementedError, 'Subclassed must implement #parse_file'
33
+ end
34
+
35
+ def read_file
36
+ raise OpenConfig::Errors::FileNotFound, file_path unless File.exists?(file_path)
37
+
38
+ File.new(file_path).read
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module OpenConfig
6
+ class JSON < OpenConfig::Base
7
+ private
8
+
9
+ def parse_file(file_content)
10
+ ::JSON.parse(file_content, object_class: OpenStruct)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+ require 'json'
5
+ require 'erb'
6
+
7
+ module OpenConfig
8
+ class YAML < OpenConfig::Base
9
+ private
10
+
11
+ def parse_file(file_content)
12
+ ::JSON.parse(build_json(file_content), object_class: OpenStruct)
13
+ end
14
+
15
+ def build_json(file_content)
16
+ ::YAML.safe_load(load_erb(file_content)).to_json
17
+ end
18
+
19
+ def load_erb(file_content)
20
+ ::ERB.new(file_content).result
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenConfig
4
+ VERSION = '1.0.0'
5
+ end
@@ -0,0 +1,25 @@
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
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open_config
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Egor Iskrenkov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-06-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.13.1
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.13.1
27
+ description:
28
+ email:
29
+ - e.iskrenkov@gmail.com
30
+ executables:
31
+ - console
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".gitignore"
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - LICENSE
39
+ - README.md
40
+ - bin/console
41
+ - lib/open_config.rb
42
+ - lib/open_config/errors.rb
43
+ - lib/open_config/files.rb
44
+ - lib/open_config/files/base.rb
45
+ - lib/open_config/files/json.rb
46
+ - lib/open_config/files/yaml.rb
47
+ - lib/open_config/version.rb
48
+ - open_config.gemspec
49
+ homepage: https://github.com/eiskrenkov/open_config
50
+ licenses:
51
+ - MIT
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.0.0
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubygems_version: 3.2.3
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Building OpenStruct trees from configuration files
72
+ test_files: []