config_files 0.0.4 → 0.1.5

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: f0a3001450205f4734271598f6b0cd51ca6aba8808434b734d031d3f7c7851b6
4
+ data.tar.gz: d62a7f9bf5fc8e4f8a299b7fe9f8eba9f7f547917298a8e0e8b9c7d0944e15f1
5
5
  SHA512:
6
- metadata.gz: 70ad5d3313cb4a8a623bf07a0a113ab945b0ef729393d474cb076a392c5d5dbcdb5e87cc7c8dc4da0118a7f64afa31e4a5c8987f08185982590127fb94e9b731
7
- data.tar.gz: 15b8aa187cdcd0ce70123ad2c98eb8a7f30652fac7455ea8a7792b570847f7b3923668b415dca9a9f6fbc9db89e008299e22dd7ca694a4d18fdd6fcf29f8548d
6
+ metadata.gz: 689ac43514f81720d263e6e5f707139b451a32703a5d361937626ce653f3c0494019aeed282d5b3e7e384e6b5a217dbd3f67d1927128a3cd12180148f32aef66
7
+ data.tar.gz: '08c8db4aff8046683fafdfa71ef7cc4717a09b0165cac63a97a3ec660f6f344d0432c8823d7d662dddad221ea8202f83aef7d189cf06a70adc7729dc0aa86eaa'
@@ -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
@@ -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.
@@ -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
@@ -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,25 @@
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_runtime_dependency 'activesupport', '>=2.2.1'
24
+ spec.add_development_dependency 'rake'
25
+ end
@@ -1,3 +1,10 @@
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
+ require 'active_support/core_ext/object/blank'
7
+
1
8
  require 'meta'
2
9
  require 'yaml'
3
10
  module ConfigFiles
@@ -14,8 +21,8 @@ module ConfigFiles
14
21
  include Meta
15
22
  attr_accessor :directories
16
23
 
17
- def yaml_extension
18
- '.yml'
24
+ def any_extension
25
+ '*'
19
26
  end
20
27
 
21
28
  def config_key
@@ -23,39 +30,53 @@ module ConfigFiles
23
30
  end
24
31
 
25
32
  def config_directories(*arr)
26
- self.directories||={ :etc => ['etc', '/etc'] }
33
+ self.directories||={ :etc => ['config', 'etc', '/etc'] }
27
34
  arr.each do |directory_list|
28
35
  directory_list.each do |key, value|
29
- self.directories[key]=value.map { |dir| File.expand_path(dir) }
36
+ self.directories[key]=value.map { |dir| ::File.expand_path(dir) }
30
37
  meta_def("#{key}_dir") { @directories[key] }
31
38
  end
32
39
  end
33
40
  end
34
41
 
42
+ def merged_hash(file)
43
+ config_files(file).inject({}) { |master, file| master.deep_merge(FileFactory.(file)) }
44
+ end
45
+
46
+ def build_combined(file)
47
+ merged_hash(file)
48
+ end
49
+
35
50
  def static_config_files(*arr)
36
51
  arr.each do |file|
37
- content=YAML.load_file(config_file(file))
52
+ content=build_combined(file)
38
53
  meta_def(file) { content }
39
54
  end
40
55
  end
41
56
 
42
57
  def dynamic_config_files(*arr)
43
58
  arr.each do |file|
44
- meta_def(file) { YAML.load_file(config_file(file)) }
59
+ meta_def(file) { build_combined(file) }
45
60
  end
46
61
  end
47
62
 
48
63
  alias_method :config_files, :dynamic_config_files
49
64
 
50
65
  private
66
+ def directory_listing(directory, file)
67
+ ::Dir.glob(::File.join(directory, "#{file}.*"))
68
+ end
69
+
51
70
  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]}")
71
+ self.directories[key]&.detect { |directory| directory_listing(directory, file).presence } || ''
72
+ end
73
+
74
+ def files(file, key=config_key)
75
+ directory_listing(first_directory(file, key), file)
55
76
  end
56
77
 
57
- def config_file(file, key=config_key)
58
- File.join(first_directory(file, key), "#{file}#{yaml_extension}")
78
+ def config_files(file, key=config_key)
79
+ files(file, key)
59
80
  end
60
81
  end
61
82
  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)
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,3 @@
1
+ require_relative 'loaders/json'
2
+ require_relative 'loaders/yaml'
3
+ require_relative 'loaders/default'
@@ -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: ::Hash)
8
+ ::JSON.load(::File.open(file_name), nil, {object_class: object_class, quirks_mode: true})
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)
6
+ ::YAML.load_file(file_name)
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module ConfigFiles
2
+ VERSION='0.1.5'
3
+ 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)
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,13 @@
1
+ {
2
+ "testId": "123123-323-radasd",
3
+ "testTitle": "string",
4
+ "testNest": [
5
+ {
6
+ "hash": {
7
+ "innerId": "27811a4c-9cb5-4e6d-a069-5c19288fae58",
8
+ "personGivenName": "Paul",
9
+ "personFamilyName": "Mak"
10
+ }
11
+ }
12
+ ]
13
+ }
@@ -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,28 +1,76 @@
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.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul McKibbin
8
- autorequire:
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: 2.2.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.2.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
13
41
  description: A configuration tool for using multiple configuration files with multiple
14
42
  formats by presenting them as a hash.
15
- email: pmckibbin@gmail.com
43
+ email:
44
+ - pmckibbin@gmail.com
16
45
  executables: []
17
46
  extensions: []
18
47
  extra_rdoc_files: []
19
48
  files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENCE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - config_files.gemspec
20
55
  - lib/config_files.rb
56
+ - lib/config_files/file_factory.rb
57
+ - lib/config_files/loader_factory.rb
58
+ - lib/config_files/loaders.rb
59
+ - lib/config_files/loaders/default.rb
60
+ - lib/config_files/loaders/json.rb
61
+ - lib/config_files/loaders/yaml.rb
62
+ - lib/config_files/version.rb
21
63
  - lib/meta.rb
64
+ - test/config_files_test.rb
65
+ - test/etc/dummy.conf
66
+ - test/etc/dummy.json
67
+ - test/etc/dummy.yml
68
+ - test/loader_factory_test.rb
22
69
  homepage: https://github.com/blackrat/config_files
23
- licenses: []
70
+ licenses:
71
+ - MIT
24
72
  metadata: {}
25
- post_install_message:
73
+ post_install_message:
26
74
  rdoc_options: []
27
75
  require_paths:
28
76
  - lib
@@ -37,9 +85,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
37
85
  - !ruby/object:Gem::Version
38
86
  version: '0'
39
87
  requirements: []
40
- rubyforge_project:
41
- rubygems_version: 2.6.14
42
- signing_key:
88
+ rubygems_version: 3.1.2
89
+ signing_key:
43
90
  specification_version: 4
44
- summary: Configuration File Manager
45
- test_files: []
91
+ summary: ConfigFiles is a configuration file access tool. It parses multiple configuration
92
+ files in multiple formats and presents a consistent block to the application with
93
+ options to cache or use the files dynamically
94
+ test_files:
95
+ - test/config_files_test.rb
96
+ - test/etc/dummy.conf
97
+ - test/etc/dummy.json
98
+ - test/etc/dummy.yml
99
+ - test/loader_factory_test.rb