wbconfigurator 0.2

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3ffd332828a7b6fd0f0bbecc8687fa6c8106a23784edce28b8e8cf1795579f70
4
+ data.tar.gz: 8766eb08427c53b35f28208831b901987f15b3b42d548711cce537371d40684e
5
+ SHA512:
6
+ metadata.gz: 5b79a2ea876cbe2349a093d0877a41c8cb3340ee20959e15476d21e9a649d7dab492236e1abf3a5314a479716d11d7aad4ffab76f6058358c8e0252ec3b0aaff
7
+ data.tar.gz: ac9656c2ef10c9fddd1b1e4f58999f5f1370d5be6b3ee447f1c97f05644b108c01048d3d895f0fdbd18493c7f925dc9946d5baea47cc22df7e9a119340cf57c7
@@ -0,0 +1,6 @@
1
+ **/*.log
2
+ **/*.rvm
3
+ **/*.DS_Store
4
+ **/*.idea
5
+ *.gem
6
+
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A sample Gemfile
4
+ source 'https://rubygems.org'
5
+
6
+ gem 'secure_yaml'
@@ -0,0 +1,13 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ secure_yaml (2.0.2)
5
+
6
+ PLATFORMS
7
+ ruby
8
+
9
+ DEPENDENCIES
10
+ secure_yaml
11
+
12
+ BUNDLED WITH
13
+ 1.17.3
@@ -0,0 +1,31 @@
1
+ # Configurator Ruby Gem
2
+
3
+ Configurator is a very simple configuration tool inspired by [rubyconfig gem](https://github.com/rubyconfig/config).
4
+ It allows easy management of specific environment settings with YAML config files.
5
+
6
+ ## Usage
7
+
8
+ Please see the [wiki](https://github.com/wildbit/configurator/wiki) for detailed instructions about how to use the tool.
9
+
10
+ ## Installation
11
+
12
+ You can use the library with a Bundler.
13
+
14
+ With Bundler:
15
+
16
+ ``` ruby
17
+ gem 'configurator', git: 'https://github.com/wildbit/configurator.git'
18
+ ```
19
+
20
+ ## Issues & Comments
21
+
22
+ Feel free to contact us if you encounter any issues with the library.
23
+ Please leave all comments, bugs, requests and issues on the Issues page.
24
+
25
+ ## License
26
+
27
+ Configurator library is released under the [MIT](http://www.opensource.org/licenses/mit-license.php) license.
28
+
29
+ ## Copyright
30
+
31
+ Copyright © 2020 Wildbit LLC.
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'config/version'
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = 'wbconfigurator'
9
+ s.version = Configurator::VERSION
10
+ s.platform = Gem::Platform::RUBY
11
+ s.license = 'MIT'
12
+
13
+ s.authors = ['Igor Balos']
14
+ s.email = ['ibalosh@gmail.com', 'igor@wildbit.com']
15
+
16
+ s.summary = 'Config tool.'
17
+ s.description = 'Configuration tool for simple management with yaml files.'
18
+
19
+ s.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
20
+ s.test_files = `git ls-files -- {spec}/*`.split("\n")
21
+ s.homepage = 'https://github.com/wildbit/configurator'
22
+ s.require_paths = ['lib']
23
+ s.required_rubygems_version = '>= 1.9.3'
24
+ s.add_dependency 'secure_yaml', '~> 2.0'
25
+ end
@@ -0,0 +1,158 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+ require 'secure_yaml'
5
+ require 'singleton'
6
+ require 'ostruct'
7
+
8
+ # App::Configuration class helps loading configuration files.
9
+ #
10
+ # In order to load config files, you need to provide encryption password to the environment
11
+ #
12
+ # To encrypt content, use the following command:
13
+ #
14
+ # encrypt_property_for_yaml encrypt [SECRET_PASSWORD] [CONTENT_TO_ENCRYPT]
15
+ module App
16
+ # Configuratior namespace
17
+ class Configurator
18
+ # OpenStruct class adaptation
19
+ class Struct < OpenStruct
20
+ alias each each_pair
21
+
22
+ # convert struct to hash, taken from:
23
+ # https://github.com/rubyconfig/config
24
+ def to_hash
25
+ result = {}
26
+ marshal_dump.each do |k, v|
27
+ if v.instance_of? Configurator::Struct
28
+ result[k] = v.to_hash
29
+ elsif v.instance_of? Array
30
+ result[k] = descend_array(v)
31
+ else
32
+ result[k] = v
33
+ end
34
+ end
35
+ result
36
+ end
37
+
38
+ private
39
+
40
+ def descend_array(array)
41
+ array.map do |value|
42
+ if value.instance_of? Configurator::Struct
43
+ value.to_hash
44
+ elsif value.instance_of? Array
45
+ descend_array(value)
46
+ else
47
+ value
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ FILES_EXTENSION = 'yaml'
54
+ DEFAULT_ENVIRONMENT = 'production'
55
+ attr_accessor :environment,
56
+ :files_path
57
+
58
+ def initialize
59
+ @environment = DEFAULT_ENVIRONMENT
60
+ end
61
+
62
+ def load_all!
63
+ files = Dir.glob("#{files_path}/*.#{FILES_EXTENSION}")
64
+ names = files.map { |f| File.basename(f).split(".").first }.uniq
65
+
66
+ names.each { |name| load!(name) }
67
+ end
68
+
69
+ # options allowed:
70
+ #
71
+ # environment: 'production'
72
+ # filter: 'filter character'
73
+ def load!(name, filter: nil )
74
+ filename = config_filename(name)
75
+ generate_attr_reader(name, load_formatted_data(filename, filter))
76
+ end
77
+
78
+ def global_name=(name)
79
+ Object.send(:remove_const, name) if Object.const_defined?(name)
80
+ Object.const_set(name, App.config)
81
+ end
82
+
83
+ private
84
+
85
+ def load_formatted_data(filename, filter)
86
+ hash_data = load_from_yaml(filename)
87
+ formatted_data(hash_data, filter: filter, struct: true)
88
+ end
89
+
90
+ def config_filename(name)
91
+ filename = "#{name}.yaml"
92
+ filename_with_env = filename.gsub(FILES_EXTENSION, "#{environment}.#{FILES_EXTENSION}")
93
+
94
+ found_file = get_file_that_exists([filename, filename_with_env])
95
+ return found_file unless found_file.nil?
96
+
97
+ raise "Configuration file '#{filename}' or '#{filename_with_env}' doesn't exist on path '#{files_path}'"
98
+ end
99
+
100
+ def formatted_data(data, filter:, struct:)
101
+ data = filtered_data(data, filter)
102
+ struct ? convert_to_struct(data) : data
103
+ end
104
+
105
+ def filtered_data(data, filter)
106
+ filter.nil? ? data : data[filter]
107
+ end
108
+
109
+ def get_file_that_exists(filenames)
110
+ found_file = nil
111
+
112
+ filenames.each do |filename|
113
+ found_file = filename if file_exists?(file_full_path(filename))
114
+ break if found_file
115
+ end
116
+
117
+ found_file
118
+ end
119
+
120
+ def file_exists?(path)
121
+ File.exist? path
122
+ end
123
+
124
+ def file_full_path(filename)
125
+ "#{files_path}/#{filename}"
126
+ end
127
+
128
+ def load_from_yaml(filename)
129
+ SecureYaml.load(File.open(file_full_path(filename)))
130
+ end
131
+
132
+ # generate accessors for loaded data
133
+ # accessors will return objects which are deep copy of themselves
134
+ def generate_attr_reader(name, data)
135
+ define_singleton_method(name) do
136
+ Marshal.load(Marshal.dump(data))
137
+ end
138
+
139
+ send(name)
140
+ end
141
+
142
+ def convert_to_struct(object)
143
+ case object
144
+ when Hash
145
+ object.each { |key, value| object[key] = convert_to_struct(value) }
146
+ Configurator::Struct.new(object)
147
+ when Array
148
+ object.map!(&method(:convert_to_struct))
149
+ else
150
+ object
151
+ end
152
+ end
153
+ end
154
+
155
+ def self.config
156
+ @config ||= Configurator.new
157
+ end
158
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Configurator
4
+ VERSION = '0.2'
5
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'config/app'
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wbconfigurator
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.2'
5
+ platform: ruby
6
+ authors:
7
+ - Igor Balos
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-08-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: secure_yaml
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description: Configuration tool for simple management with yaml files.
28
+ email:
29
+ - ibalosh@gmail.com
30
+ - igor@wildbit.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".gitignore"
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - README.md
39
+ - configurator.gemspec
40
+ - lib/config/app.rb
41
+ - lib/config/version.rb
42
+ - lib/configurator.rb
43
+ homepage: https://github.com/wildbit/configurator
44
+ licenses:
45
+ - MIT
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 1.9.3
61
+ requirements: []
62
+ rubygems_version: 3.0.8
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Config tool.
66
+ test_files: []