mimi-config 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ede4e9494fa2da839879c476231cb7b53e917717
4
+ data.tar.gz: d6f19fbe2a4f66d6ffd2260becc0c724e0fbadbf
5
+ SHA512:
6
+ metadata.gz: 552d80c961767e0b097abfc384d15629e39bbca6495ff3651087d900c822f1f743dcc3e81ec7764a6ecc3a0fb6647db45d3e415022cba780dd6c1f2c178ea031
7
+ data.tar.gz: e303ea389d4ca52aa61e9b30593ae3e6c18afddb563c93af400174d558ad5a046253aef55d47dc387069c70619d95bf9b49c07a2e24c14bb2f97fb4ef6e3b8e6
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at alex@kukushk.in. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ # Comment out the following gem instructions, if you not using gem sources
6
+ gem 'mimi-core', path: '/mimi-core'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Alex Kukushkin
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # mimi-config
2
+
3
+ A simple ENV based configuration for microservice applications.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'mimi-config'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install mimi-config
21
+
22
+ ## Usage
23
+
24
+ `Mimi::Config` allows you to define a set of configurable parameters and constants in a *manifest* file with a simple YAML format.
25
+
26
+ ### Example manifest.yml
27
+ ```yaml,name=manifest.yml
28
+ opt1:
29
+ desc: This is an optional configurable parameter
30
+ default: 123
31
+
32
+ opt2:
33
+ desc: This is an optional configurable parameter with the default value of nil
34
+ default:
35
+
36
+ const1:
37
+ desc: This is a constant parameter, that cannot be changed via an ENV variable
38
+ const: 456
39
+
40
+
41
+ req1: This is a required configurable parameter
42
+
43
+ req2:
44
+ desc: And so is this one
45
+
46
+ # And so is this one with no description
47
+ req3:
48
+ ```
49
+
50
+ ### How to load configuration from your app
51
+
52
+ Providing you have placed your manifest into `config/manifest.yml`:
53
+
54
+ ```ruby
55
+ config = Mimi::Config.new('config/manifest.yml')
56
+
57
+ config.const1 # => 456, from manifest.yml
58
+ config.opt1 # value from ENV['opt1'] or default from manifest.yml
59
+ config.req2 # value from ENV['req2']
60
+
61
+ # alternatively use [] syntax:
62
+ config.opt2 #
63
+ config[:opt2] # all three refer to the same configurable parameter
64
+ config['opt2'] #
65
+
66
+ # you cannot access parameters not defined in the manifest:
67
+ config.req5 # => NoMethodError
68
+ config['req5'] # => ArgumentError
69
+
70
+ # check, if parameter is defined:
71
+ config.include?('req5') # => false
72
+ ```
73
+
74
+ ## Development
75
+
76
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
77
+
78
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
79
+
80
+ ## Contributing
81
+
82
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mimi-config. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
83
+
84
+
85
+ ## License
86
+
87
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
88
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'mimi/config'
5
+
6
+ require 'pry'
7
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ module Mimi
2
+ class Config
3
+ VERSION = '0.1.0'.freeze
4
+ end # class Config
5
+ end # module Mimi
@@ -0,0 +1,177 @@
1
+ require 'pathname'
2
+ require 'yaml'
3
+ require 'dotenv'
4
+ require 'mimi/core'
5
+
6
+ module Mimi
7
+ #
8
+ # Config stores the manifest and reads and stores configurable parameters from ENV.
9
+ #
10
+ # @see README.md
11
+ #
12
+ class Config
13
+ include Mimi::Core::Module
14
+
15
+ # Current set of values for configurable and const parameters
16
+ attr_reader :params
17
+
18
+ default_options(
19
+ raise_on_missing_params: true,
20
+ use_dotenv: true
21
+ )
22
+
23
+ # Creates a Config object.
24
+ #
25
+ # Loads and parses manifest.yml, reads and sets configurable parameters
26
+ # from ENV.
27
+ #
28
+ # Raises an error if any of the required configurable parameters are missing.
29
+ #
30
+ # @param manifest_filename [String,nil] path to the manifest.yml or nil to skip loading manifest
31
+ #
32
+ def initialize(manifest_filename = nil, opts = {})
33
+ @manifest = {}
34
+ @params = {}
35
+ load(manifest_filename, opts) if manifest_filename
36
+ end
37
+
38
+ # Loads and parses manifest.yml, reads and sets configurable parameters
39
+ # from ENV.
40
+ #
41
+ def load(manifest_filename, opts = {})
42
+ opts = self.class.module_options.deep_merge(opts)
43
+ manifest_filename = Pathname.new(manifest_filename).expand_path
44
+ load_manifest(manifest_filename, opts)
45
+ load_params(opts)
46
+ if opts[:raise_on_missing_params] && !missing_params.empty?
47
+ raise "Missing required configurable parameters: #{missing_params.join(', ')}"
48
+ end
49
+ self
50
+ end
51
+
52
+ # Returns list of missing required params
53
+ #
54
+ def missing_params
55
+ required_params = manifest.select { |p| p[:required] }.map { |p| p[:name] }
56
+ required_params - @params.keys
57
+ end
58
+
59
+ # Returns annotated manifest
60
+ #
61
+ def manifest
62
+ @manifest.map do |k, v|
63
+ {
64
+ name: k,
65
+ desc: v[:desc],
66
+ required: !v.key?(:default),
67
+ const: v[:const],
68
+ default: v[:default]
69
+ }
70
+ end
71
+ end
72
+
73
+ # Returns true if the config manifest includes the parameter with the given name.
74
+ #
75
+ # If manifest includes the parameter name, it is safe to access paramter
76
+ # via #[] and #<name> methods.
77
+ #
78
+ def include?(name)
79
+ @manifest.key?(name.to_sym)
80
+ end
81
+
82
+ # Returns the parameter value
83
+ #
84
+ # @param key [String,Symbol] parameter name
85
+ #
86
+ def [](key)
87
+ raise ArgumentError, "Undefined parameter '#{key}'" unless include?(key)
88
+ @params[key.to_sym]
89
+ end
90
+
91
+ # Provides access to parameters as methods.
92
+ #
93
+ # Example:
94
+ # config['foo'] # => 'bar'
95
+ # config.foo # => 'bar'
96
+ #
97
+ # # missing parameter
98
+ # config['bar'] # => ArgumentError
99
+ # config.bar # => NoMethodError
100
+ #
101
+ def method_missing(name, *)
102
+ return self[name] if include?(name)
103
+ super
104
+ end
105
+
106
+ def respond_to_missing?(name, *)
107
+ include?(name) || super
108
+ end
109
+
110
+ private
111
+
112
+ # Reads manifest file and merges it with the current manifest.
113
+ #
114
+ def load_manifest(filename, _opts = {})
115
+ new_manifest = YAML.load(File.read(filename))
116
+ return manifest unless new_manifest
117
+ raise 'Invalid manifest file format' unless new_manifest.is_a?(Hash)
118
+ new_manifest.each do |k, v|
119
+ merge_manifest_key(k, v)
120
+ end
121
+ manifest
122
+ rescue StandardError => e
123
+ raise "Failed to load manifest file: #{e}"
124
+ end
125
+
126
+ # Reads parameters from the ENV according to the current manifest
127
+ #
128
+ def load_params(opts = {})
129
+ Dotenv.load if opts[:use_dotenv]
130
+ manifest.each do |p|
131
+ env_name = p[:name].to_s
132
+ if p[:const]
133
+ # const
134
+ @params[p[:name]] = p[:default]
135
+ elsif p[:required]
136
+ # required configurable
137
+ @params[p[:name]] = ENV[env_name] if ENV.key?(env_name)
138
+ else
139
+ # optional configurable
140
+ @params[p[:name]] = ENV.key?(env_name) ? ENV[env_name] : p[:default]
141
+ end
142
+ end
143
+ @params
144
+ end
145
+
146
+ def merge_manifest_key(k, v)
147
+ k = k.to_sym
148
+ @manifest[k] ||= {}
149
+ if v.nil?
150
+ # var:
151
+ elsif v.is_a?(String)
152
+ # var: A description
153
+ @manifest[k][:desc] = v
154
+ elsif v.is_a?(Hash)
155
+ merge_manifest_key_hash(k, v)
156
+ end
157
+ end
158
+
159
+ def merge_manifest_key_hash(k, v)
160
+ @manifest[k][:desc] = v['desc'] if v.key?('desc')
161
+
162
+ if v.key?('const') && v.key?('default')
163
+ raise "Invalid mix of 'const' and 'default' in parameter definition '#{k}'"
164
+ end
165
+
166
+ if v.key?('default')
167
+ @manifest[k][:const] = false
168
+ @manifest[k][:default] = v['default']
169
+ elsif v.key?('const')
170
+ @manifest[k][:const] = true
171
+ @manifest[k][:default] = v['const']
172
+ end
173
+ end
174
+ end # class Config
175
+ end # module Mimi
176
+
177
+ require_relative 'config/version'
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mimi/config/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'mimi-config'
8
+ spec.version = Mimi::Config::VERSION
9
+ spec.authors = ['Alex Kukushkin']
10
+ spec.email = ['alex@kukushk.in']
11
+
12
+ spec.summary = 'Simple ENV based configuration for microservice apps'
13
+ spec.description = 'Simple ENV based configuration for microservice apps'
14
+ spec.homepage = 'https://github.com/kukushkin/mimi-config'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
21
+ else
22
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_dependency 'mimi-core', '~> 0.1'
31
+ spec.add_dependency 'dotenv', '~> 2.1'
32
+
33
+ spec.add_development_dependency 'bundler', '~> 1.11'
34
+ spec.add_development_dependency 'rake', '~> 10.0'
35
+ spec.add_development_dependency 'rspec', '~> 3.0'
36
+ spec.add_development_dependency 'pry', '~> 0.10'
37
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mimi-config
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alex Kukushkin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mimi-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: dotenv
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.10'
97
+ description: Simple ENV based configuration for microservice apps
98
+ email:
99
+ - alex@kukushk.in
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - CODE_OF_CONDUCT.md
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - lib/mimi/config.rb
115
+ - lib/mimi/config/version.rb
116
+ - mimi-config.gemspec
117
+ homepage: https://github.com/kukushkin/mimi-config
118
+ licenses:
119
+ - MIT
120
+ metadata:
121
+ allowed_push_host: https://rubygems.org/
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.4.5.1
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Simple ENV based configuration for microservice apps
142
+ test_files: []