config_files 0.0.4 → 0.1.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: ba83f6bade45d06bfa8a9ea74ba48915cfa9c908790f635aea7c53e5e12ffb2d
4
- data.tar.gz: 969daeff600eb0fd4e6e084e33b3d83159e9aded3cae3df02f46528c073182dd
3
+ metadata.gz: 69666324c397bf59a18efef190d2660ce376143b246bda3752409af21fe1b51b
4
+ data.tar.gz: 79c42d11ba5617e182e6b6cdb56074036c99f7bcd32fe0f5f0d0022b971f3194
5
5
  SHA512:
6
- metadata.gz: 70ad5d3313cb4a8a623bf07a0a113ab945b0ef729393d474cb076a392c5d5dbcdb5e87cc7c8dc4da0118a7f64afa31e4a5c8987f08185982590127fb94e9b731
7
- data.tar.gz: 15b8aa187cdcd0ce70123ad2c98eb8a7f30652fac7455ea8a7792b570847f7b3923668b415dca9a9f6fbc9db89e008299e22dd7ca694a4d18fdd6fcf29f8548d
6
+ metadata.gz: e53a814869eaebf6ed6b6ecc55425dedbaca918434168720e96eed9812a84bce6b64f0818c5b751343b20ce4adc42d7d23ab3b01b857542443fb8da68a5a7eca
7
+ data.tar.gz: 72ea7cc041cf8405429244a42c96a5a2f275de969b9fd183b62bca9423271847d38a43ae3970b23b64679643a3acdc26ff594c82a75456509c9a7a94cb76e830
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ # Created by .ignore support plugin (hsz.mobi)
2
+ .idea/
3
+
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENCE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) [2012] [Paul McKibbin]
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,38 @@
1
+ *Description*
2
+
3
+ Since it no longer just deals with yaml files as an input, *blackrat_yaml_config* has been renamed to *config_files*
4
+ Configuration file manager.
5
+
6
+ *Features*
7
+
8
+ Searches for first match in multiple directories for configuration file
9
+ Allows for dynamically updated or static config files
10
+
11
+ *Example*
12
+
13
+ require 'config_files'
14
+
15
+ class Dummy
16
+ include ConfigFiles #mixin the config_directories and config_files generators
17
+
18
+ #search directories (in order). The system will search for the file in the following directories
19
+ config_directories :etc=>['~/.dummy','/opt/dummy/config','/etc/default/dummy','/etc']
20
+
21
+ #The dummy.yml and another_yaml_file.yml will be pre-loaded.
22
+ static_config_files :dummy, :another_yaml_file
23
+
24
+ #yet_another_yaml_file.yml will be read every time the .yet_another_yaml_file method is accessed.
25
+ dynamic_config_files :yet_another_yaml_file
26
+
27
+ def use_config
28
+ some_method(Dummy.dummy[:key]) #extract the constant values from the :key in dummy.yml
29
+ another_method(Dummy.yet_another_yaml_file[:another_key]) #extract the constant value from the :another_key in yet_another_yaml_file.yml
30
+ end
31
+
32
+ end
33
+
34
+
35
+
36
+ *Todo*
37
+
38
+ Allow for different keys to be stored in files in different subdirectories to allow for overridable defaults
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
@@ -0,0 +1,24 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'config_files/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.authors = ['Paul McKibbin']
7
+ spec.email = ['pmckibbin@gmail.com']
8
+ spec.description = 'A configuration tool for using multiple configuration files with multiple formats by presenting them as a hash.'
9
+ spec.summary = <<-SUMMARY
10
+ ConfigFiles is a configuration file access tool. It parses multiple configuration files in multiple formats and
11
+ presents a consistent block to the application with options to cache or use the files dynamically
12
+ SUMMARY
13
+
14
+ spec.homepage = 'https://github.com/blackrat/config_files'
15
+ spec.files = `git ls-files`.split($OUTPUT_RECORD_SEPARATOR)
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.name = 'config_files'
18
+ spec.require_paths = ['lib']
19
+ spec.version = ConfigFiles::VERSION
20
+ spec.license = 'MIT'
21
+ spec.date = '2012-11-11'
22
+
23
+ spec.add_dependency 'activesupport'
24
+ end
@@ -0,0 +1,15 @@
1
+ module ConfigFiles
2
+ class FileFactory
3
+ class << self
4
+ private
5
+ def loader(file_name, options)
6
+ LoaderFactory.(file_name, options)
7
+ end
8
+
9
+ public
10
+ def call(file_name, options={})
11
+ loader(file_name, options).(file_name, object_class: ::Hash)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,52 @@
1
+ module ConfigFiles
2
+ class LoaderFactory
3
+ class << self
4
+ public
5
+ def call(file_name, options={include_default: true})
6
+ new(options).call(file_name)
7
+ end
8
+ end
9
+
10
+ private
11
+ attr_reader :options
12
+
13
+ def default_loaders
14
+ {
15
+ Loaders::Yaml => ['yaml', 'yml'],
16
+ Loaders::Json => ['json']
17
+ }
18
+ end
19
+
20
+ def default_loader
21
+ options[:default_loader]
22
+ end
23
+
24
+ def default_options
25
+ {
26
+ include_default: true,
27
+ default_loader: Loaders::Yaml,
28
+ loaders: default_loaders
29
+ }
30
+ end
31
+
32
+ def include_default_loaders?
33
+ options[:include_default]
34
+ end
35
+
36
+ def loaders
37
+ options[:loaders]
38
+ end
39
+
40
+ def initialize(options) #Note the check below is necessary, because we only want to do it if it is explicity set
41
+ @options=default_options.merge(options)
42
+ if include_default_loaders?
43
+ @options[:loaders]=default_loaders.merge(loaders)
44
+ end
45
+ end
46
+
47
+ public
48
+ def call(file_name)
49
+ loaders.detect{|_, extensions| extensions.include?(::File.extname(file_name).strip.downcase[1..-1])}&.first || default_loader
50
+ end
51
+ end
52
+ end
@@ -0,0 +1 @@
1
+ ConfigFiles::Loaders::Default=ConfigFiles::Loaders::Yaml
@@ -0,0 +1,13 @@
1
+ require 'json'
2
+
3
+ module ConfigFiles
4
+ module Loaders
5
+ class Json
6
+ class << self
7
+ def call(file_name, object_class: ::OpenStruct)
8
+ ::JSON.load(file_name, nil, {object_class: object_class})
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module ConfigFiles
2
+ module Loaders
3
+ class Yaml
4
+ class << self
5
+ def call(file_name, object_class: OpenStruct)
6
+ JSON.parse(::YAML.load_file(file_name).to_json, object_class: object_class)
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ require_relative 'loaders/json'
2
+ require_relative 'loaders/yaml'
3
+ require_relative 'loaders/default'
@@ -0,0 +1,3 @@
1
+ module ConfigFiles
2
+ VERSION='0.1.0'
3
+ end
data/lib/config_files.rb CHANGED
@@ -1,3 +1,9 @@
1
+ require 'config_files/file_factory'
2
+ require 'config_files/loader_factory'
3
+ require 'config_files/loaders'
4
+ require 'config_files/version'
5
+ require 'active_support/core_ext/hash/deep_merge'
6
+
1
7
  require 'meta'
2
8
  require 'yaml'
3
9
  module ConfigFiles
@@ -14,8 +20,8 @@ module ConfigFiles
14
20
  include Meta
15
21
  attr_accessor :directories
16
22
 
17
- def yaml_extension
18
- '.yml'
23
+ def any_extension
24
+ '*'
19
25
  end
20
26
 
21
27
  def config_key
@@ -32,16 +38,24 @@ module ConfigFiles
32
38
  end
33
39
  end
34
40
 
41
+ def merged_hash(file)
42
+ config_files(file).inject({}) { |master, file| master.deep_merge(FileFactory.(file)) }
43
+ end
44
+
45
+ def build_combined(file)
46
+ JSON.parse(merged_hash(file).to_json, object_class: OpenStruct)
47
+ end
48
+
35
49
  def static_config_files(*arr)
36
50
  arr.each do |file|
37
- content=YAML.load_file(config_file(file))
51
+ content=build_combined(file)
38
52
  meta_def(file) { content }
39
53
  end
40
54
  end
41
55
 
42
56
  def dynamic_config_files(*arr)
43
57
  arr.each do |file|
44
- meta_def(file) { YAML.load_file(config_file(file)) }
58
+ meta_def(file) { build_combined(file) }
45
59
  end
46
60
  end
47
61
 
@@ -49,13 +63,15 @@ module ConfigFiles
49
63
 
50
64
  private
51
65
  def first_directory(file, key=config_key)
52
- self.directories[key].find { |directory|
53
- File.exists?(File.join(directory, "#{file}#{yaml_extension}"))
54
- } || (raise Errno::ENOENT, "No #{file}#{yaml_extension} in #{self.directories[key]}")
66
+ self.directories[key]&.detect { |directory| Dir.glob(File.join(directory, "#{file}.*")) } || (raise Errno::ENOENT, "No #{file}.* in #{self.directories[key]}")
67
+ end
68
+
69
+ def files(file, key=config_key)
70
+ Dir.glob(File.join(first_directory(file, key), "#{file}.*"))
55
71
  end
56
72
 
57
- def config_file(file, key=config_key)
58
- File.join(first_directory(file, key), "#{file}#{yaml_extension}")
73
+ def config_files(file, key=config_key)
74
+ files(file, key)
59
75
  end
60
76
  end
61
77
  end
@@ -0,0 +1,47 @@
1
+ $:.push(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require 'minitest/autorun'
3
+ require 'config_files'
4
+ class Dummy
5
+ include ConfigFiles
6
+ config_directories :etc => ['etc', 'nofiles/etc']
7
+ static_config_files :dummy, :broken
8
+ end
9
+
10
+ class Dummy2 < Dummy
11
+ config_directories 'config' => ['etc', 'nofiles/etc']
12
+ class << self
13
+ def config_key
14
+ "config"
15
+ end
16
+ end
17
+ end
18
+
19
+ class ConfigFilesTest < MiniTest::Test
20
+ def test_config_key
21
+ assert_equal(:etc, Dummy.config_key)
22
+ end
23
+
24
+ def test_directory_is_initialized
25
+ assert(Dummy.directories)
26
+ end
27
+
28
+ def test_has_created_methods
29
+ assert(Dummy.etc_dir)
30
+ end
31
+
32
+ def test_created_paths_for_directories
33
+ assert_equal([File.join(__dir__, 'etc'), File.join(__dir__, 'nofiles/etc')], Dummy.etc_dir)
34
+ end
35
+
36
+ def test_created_variables
37
+ assert_equal('test', Dummy.dummy[:config_test])
38
+ end
39
+
40
+ def test_empty_for_missing_files
41
+ assert_equal({}, Dummy.broken.to_h)
42
+ end
43
+
44
+ def test_yaml_and_config_override
45
+ assert_equal('test', Dummy2.dummy[:config_test])
46
+ end
47
+ end
@@ -0,0 +1,2 @@
1
+ :config_test: test2
2
+ :only_in_conf: conf_file
@@ -0,0 +1,2 @@
1
+ :config_test: test
2
+ :only_in_yaml: yaml_file
@@ -0,0 +1,40 @@
1
+ $:.push(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require 'minitest/autorun'
3
+ require 'config_files'
4
+ class LoaderFactoryDummy
5
+ class << self
6
+ def call(file_name, options={})
7
+ nil
8
+ end
9
+ end
10
+ end
11
+
12
+ class LoaderFactoryTest < MiniTest::Test
13
+ def file_locations
14
+ File.join(__dir__, 'etc')
15
+ end
16
+
17
+ def test_detect_yaml_file_type
18
+ assert_equal(::ConfigFiles::Loaders::Yaml, ::ConfigFiles::LoaderFactory.(File.join(file_locations,'dummy.yml')))
19
+ end
20
+
21
+ def test_detect_json_file_type
22
+ assert_equal(::ConfigFiles::Loaders::Json, ::ConfigFiles::LoaderFactory.(File.join(file_locations,'dummy.json')))
23
+ end
24
+
25
+ def test_detect_unknown_file_type
26
+ assert_nil(::ConfigFiles::LoaderFactory.(File.join(file_locations,'dummy.wibble'), default_loader: nil))
27
+ end
28
+
29
+ def test_allow_addition_of_file_types_does_not_break_existing
30
+ assert_equal(::ConfigFiles::Loaders::Yaml, ::ConfigFiles::LoaderFactory.(File.join(file_locations,'dummy.yml'), loaders: {LoaderFactoryDummy=>['wibble']}))
31
+ end
32
+
33
+ def test_allow_addition_of_file_types_breaks_existing_if_specified
34
+ assert_nil(::ConfigFiles::LoaderFactory.(File.join(file_locations,'dummy.yml'), loaders: {LoaderFactoryDummy=>['wibble']}, include_default: false, default_loader: nil))
35
+ end
36
+
37
+ def test_addition_of_new_loaders
38
+ assert_equal(LoaderFactoryDummy, ::ConfigFiles::LoaderFactory.(File.join(file_locations,'dummy.wibble'), loaders: {LoaderFactoryDummy=>['wibble']}, include_default: false))
39
+ end
40
+ end
metadata CHANGED
@@ -1,26 +1,59 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: config_files
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul McKibbin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-08 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2012-11-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: A configuration tool for using multiple configuration files with multiple
14
28
  formats by presenting them as a hash.
15
- email: pmckibbin@gmail.com
29
+ email:
30
+ - pmckibbin@gmail.com
16
31
  executables: []
17
32
  extensions: []
18
33
  extra_rdoc_files: []
19
34
  files:
35
+ - ".gitignore"
36
+ - Gemfile
37
+ - LICENCE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - config_files.gemspec
20
41
  - lib/config_files.rb
42
+ - lib/config_files/file_factory.rb
43
+ - lib/config_files/loader_factory.rb
44
+ - lib/config_files/loaders.rb
45
+ - lib/config_files/loaders/default.rb
46
+ - lib/config_files/loaders/json.rb
47
+ - lib/config_files/loaders/yaml.rb
48
+ - lib/config_files/version.rb
21
49
  - lib/meta.rb
50
+ - test/config_files_test.rb
51
+ - test/etc/dummy.conf
52
+ - test/etc/dummy.yml
53
+ - test/loader_factory_test.rb
22
54
  homepage: https://github.com/blackrat/config_files
23
- licenses: []
55
+ licenses:
56
+ - MIT
24
57
  metadata: {}
25
58
  post_install_message:
26
59
  rdoc_options: []
@@ -38,8 +71,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
38
71
  version: '0'
39
72
  requirements: []
40
73
  rubyforge_project:
41
- rubygems_version: 2.6.14
74
+ rubygems_version: 2.7.6
42
75
  signing_key:
43
76
  specification_version: 4
44
- summary: Configuration File Manager
45
- test_files: []
77
+ summary: ConfigFiles is a configuration file access tool. It parses multiple configuration
78
+ files in multiple formats and presents a consistent block to the application with
79
+ options to cache or use the files dynamically
80
+ test_files:
81
+ - test/config_files_test.rb
82
+ - test/etc/dummy.conf
83
+ - test/etc/dummy.yml
84
+ - test/loader_factory_test.rb