nxt_config 0.1.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: 99bdd425a2f8fad0172fcb0abed1b1a0c4c4b6c73f689020bd980018f6c9b405
4
+ data.tar.gz: 3a61b5deacd727ee4e932b0b82abdc6e31d1bbad3bc29623721f97d33f237996
5
+ SHA512:
6
+ metadata.gz: d863dac54820171a4211ef9540510486dbcd5f95a792d0315d50db65a943c9e59b1248c1aac7e5111a73d068440d03049cf60e8773eed47b00c6304f8aab1578
7
+ data.tar.gz: '0832012a16cc951d9da8edb459c3d513414718121487df658150e384f87575d925e12e1dd3b83f3fa61a8243776d94282ce85922d7d55de4b52208667af3f656'
@@ -0,0 +1,56 @@
1
+ # Ruby CircleCI 2.0 configuration file
2
+ #
3
+ # Check https://circleci.com/docs/2.0/language-ruby/ for more details
4
+ #
5
+ version: 2
6
+ jobs:
7
+ build:
8
+ docker:
9
+ # specify the version you desire here
10
+ - image: circleci/ruby:2.7.0-node
11
+ environment:
12
+ BUNDLER_VERSION: 2.1.3
13
+
14
+ working_directory: ~/repo
15
+
16
+ steps:
17
+ - checkout
18
+
19
+ # Download and cache dependencies
20
+ - restore_cache:
21
+ keys:
22
+ - v1-dependencies-{{ checksum "Gemfile.lock" }}
23
+
24
+ - run: gem install bundler --version $BUNDLER_VERSION
25
+
26
+ - run:
27
+ name: install dependencies
28
+ command: |
29
+ bundle install --jobs=4 --retry=3 --path vendor/bundle
30
+
31
+ - save_cache:
32
+ paths:
33
+ - ./vendor/bundle
34
+ key: v1-dependencies-{{ checksum "Gemfile.lock" }}
35
+
36
+ # run tests!
37
+ - run:
38
+ name: run tests
39
+ command: |
40
+ mkdir /tmp/test-results
41
+ TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | \
42
+ circleci tests split --split-by=timings)"
43
+
44
+ bundle exec rspec \
45
+ --format progress \
46
+ --format RspecJunitFormatter \
47
+ --out /tmp/test-results/rspec.xml \
48
+ --format progress \
49
+ $TEST_FILES
50
+
51
+ # collect reports
52
+ - store_test_results:
53
+ path: /tmp/test-results
54
+ - store_artifacts:
55
+ path: /tmp/test-results
56
+ destination: test-results
data/.editorconfig ADDED
@@ -0,0 +1,15 @@
1
+ # EditorConfig is awesome: http://EditorConfig.org
2
+
3
+ # top-most EditorConfig file
4
+ root = true
5
+
6
+ # Unix-style newlines with a newline ending every file
7
+ [*]
8
+ end_of_line = lf
9
+ insert_final_newline = true
10
+ charset = utf-8
11
+
12
+ [*.{rb,yml,json,rake,erb}]
13
+ indent_style = space
14
+ indent_size = 2
15
+ trim_trailing_whitespace = true
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.0
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in nxt_config.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+
9
+ group :test do
10
+ gem 'rspec_junit_formatter' # for CircleCI
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,52 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ nxt_config (0.1.0)
5
+ activesupport
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (6.0.2.1)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 0.7, < 2)
13
+ minitest (~> 5.1)
14
+ tzinfo (~> 1.1)
15
+ zeitwerk (~> 2.2)
16
+ concurrent-ruby (1.1.5)
17
+ diff-lcs (1.3)
18
+ i18n (1.7.0)
19
+ concurrent-ruby (~> 1.0)
20
+ minitest (5.13.0)
21
+ rake (12.3.3)
22
+ rspec (3.9.0)
23
+ rspec-core (~> 3.9.0)
24
+ rspec-expectations (~> 3.9.0)
25
+ rspec-mocks (~> 3.9.0)
26
+ rspec-core (3.9.1)
27
+ rspec-support (~> 3.9.1)
28
+ rspec-expectations (3.9.0)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.9.0)
31
+ rspec-mocks (3.9.1)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.9.0)
34
+ rspec-support (3.9.2)
35
+ rspec_junit_formatter (0.4.1)
36
+ rspec-core (>= 2, < 4, != 2.12.0)
37
+ thread_safe (0.3.6)
38
+ tzinfo (1.2.6)
39
+ thread_safe (~> 0.1)
40
+ zeitwerk (2.2.2)
41
+
42
+ PLATFORMS
43
+ ruby
44
+
45
+ DEPENDENCIES
46
+ nxt_config!
47
+ rake (~> 12.0)
48
+ rspec (~> 3.0)
49
+ rspec_junit_formatter
50
+
51
+ BUNDLED WITH
52
+ 2.1.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Nils Sommer
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,77 @@
1
+ [![CircleCI](https://circleci.com/gh/nxt-insurance/nxt_config.svg?style=svg)](https://circleci.com/gh/nxt-insurance/nxt_config) [![Depfu](https://badges.depfu.com/badges/55572b7950c22f7f472a0adbf81b2ea4/count.svg)](https://depfu.com/github/nxt-insurance/nxt_config?project_id=10455)
2
+
3
+ # NxtConfig
4
+
5
+ This is a very simple tool to load YAML files into strict configuration structs, accessible through global constants. This is inspired by the famous [config](https://github.com/railsconfig/config) gem. The core features are:
6
+
7
+ * Load the content of a YAML file as a configuration object
8
+ * Strict attribute accessors
9
+ * Infinite amount of YAML files/configuration objects loadable (not just one)
10
+ * Configuration objects can be registered in a given namespace (especially useful when in use in ruby gems loaded by other applications)
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'nxt_config'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle install
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install nxt_config
27
+
28
+ ## Usage
29
+
30
+ You can load YAML files and populate their content into a configuration object using `NxtConfig.load_and_constantize`. If you are in a rails application, you can do this in an initializer (e.g. `config/initializers/nxt_config.rb`).
31
+
32
+ ```ruby
33
+ NxtConfig.load_and_constantize(
34
+ source: Rails.root.join('config', 'external_api.yml.erb'),
35
+ constant_name: :ExternalApiConfig,
36
+ namespace: MyRailsApp
37
+ )
38
+ ```
39
+
40
+ This assumes your rails app defines the `MyRailsApp` constant in `config/aplication.rb` and populates the configuration to `MyRailsApp::ExternalApiConfig`. You can also skip the namespace parameter alltogether or pass in any other module that is defined at the time of calling `::load_and_constantize`.
41
+
42
+ ```ruby
43
+ # Use struct like method chaining to access nested data
44
+ MyRailsApp::ExternalApiConfig.http.headers.user_agent
45
+ # => "MyRailsApp 1.0.0"
46
+
47
+ # The struct methods are strict, so they raise an error if the attribute does not exist
48
+ MyRailsApp::ExternalApiConfig.non_existent_key
49
+ # => raises NoMethodError
50
+
51
+ # You can also use hash like #[] calls with symbols
52
+ MyRailsApp::ExternalApiConfig[:http][:headers][:user_agent]
53
+ # => "MyRailsApp 1.0.0"
54
+
55
+ # You can also use hash like #[] calls with strings
56
+ MyRailsApp::ExternalApiConfig["http"]["headers"]["user_agent"]
57
+ # => "MyRailsApp 1.0.0"
58
+
59
+ # If you don't walk through the struct until its leaves, you will get a sub struct
60
+ MyRailsApp::ExternalApiConfig.http
61
+ # => #<NxtConfig::Struct:0x00007fe657467680 @hash={"headers"=>#<NxtConfig::Struct:0x00007fe657467518 @hash={"user_agent"=>"my cool app", "api_key"=>"secret123"}>}>
62
+ ```
63
+
64
+ ## Development
65
+
66
+ 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.
67
+
68
+ 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).
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nxt-insurance/nxt_config.
73
+
74
+
75
+ ## License
76
+
77
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
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,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "nxt_config"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/rspec ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rspec' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rspec-core", "rspec")
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,40 @@
1
+ module NxtConfig
2
+ class Struct
3
+ def initialize(hash)
4
+ @hash = hash.with_indifferent_access
5
+ define_key_accessor_methods
6
+ hash.freeze
7
+ end
8
+
9
+ delegate :[], to: :hash
10
+
11
+ private
12
+
13
+ attr_reader :hash
14
+
15
+ def define_key_accessor_methods
16
+ hash.transform_values! do |value|
17
+ transform_hash_value(value)
18
+ end
19
+ end
20
+
21
+ def transform_hash_value(value)
22
+ if value.is_a?(ActiveSupport::HashWithIndifferentAccess)
23
+ Struct.new(value)
24
+ elsif value.is_a?(Array)
25
+ value.map { |item| transform_hash_value(item) }
26
+ else
27
+ value
28
+ end
29
+ end
30
+
31
+ def method_missing(name, *args)
32
+ # Use #has_key? because even when the key exists, the value can be nil
33
+ if hash.has_key?(name)
34
+ hash.fetch(name)
35
+ else
36
+ super(name, *args)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module NxtConfig
2
+ VERSION = "0.1.0".freeze
3
+ end
data/lib/nxt_config.rb ADDED
@@ -0,0 +1,29 @@
1
+ require "active_support/all"
2
+ require "erb"
3
+ require "yaml"
4
+
5
+ require "nxt_config/struct"
6
+ require "nxt_config/version"
7
+
8
+ module NxtConfig
9
+ class ConstantNameAlreadyTaken < StandardError; end
10
+
11
+ def load_and_constantize(source:, constant_name: nil, namespace: nil)
12
+ source_hash = YAML.safe_load(ERB.new(File.read(source)).result)
13
+ struct = Struct.new(source_hash)
14
+ namespace_constant = namespace || Object
15
+
16
+ begin
17
+ if namespace_constant.const_get(constant_name)
18
+ raise ConstantNameAlreadyTaken,
19
+ "Cannot define constant #{constant_name} on #{namespace_constant.name} because it already exists."
20
+ end
21
+ rescue NameError
22
+ # If the constant does not exist => fine, let's set it!
23
+ end
24
+
25
+ namespace_constant.const_set(constant_name, struct)
26
+ end
27
+
28
+ module_function :load_and_constantize
29
+ end
@@ -0,0 +1,30 @@
1
+ require_relative 'lib/nxt_config/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "nxt_config"
5
+ spec.version = NxtConfig::VERSION
6
+ spec.authors = ["Nils Sommer"]
7
+ spec.email = ["mail@nilssommer.de"]
8
+
9
+ spec.summary = "Simple configuration objects, loadable from YAML files"
10
+ spec.description = "Create namespaced, immutable objects serving configuration properties to your ruby application."
11
+ spec.homepage = "https://github.com/nxt-insurance/nxt_config"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/nxt-insurance/nxt_config"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_dependency "activesupport"
30
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nxt_config
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nils Sommer
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-01-04 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'
27
+ description: Create namespaced, immutable objects serving configuration properties
28
+ to your ruby application.
29
+ email:
30
+ - mail@nilssommer.de
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".circleci/config.yml"
36
+ - ".editorconfig"
37
+ - ".gitignore"
38
+ - ".rspec"
39
+ - ".ruby-version"
40
+ - Gemfile
41
+ - Gemfile.lock
42
+ - LICENSE.txt
43
+ - README.md
44
+ - Rakefile
45
+ - bin/console
46
+ - bin/rspec
47
+ - bin/setup
48
+ - lib/nxt_config.rb
49
+ - lib/nxt_config/struct.rb
50
+ - lib/nxt_config/version.rb
51
+ - nxt_config.gemspec
52
+ homepage: https://github.com/nxt-insurance/nxt_config
53
+ licenses:
54
+ - MIT
55
+ metadata:
56
+ allowed_push_host: https://rubygems.org
57
+ homepage_uri: https://github.com/nxt-insurance/nxt_config
58
+ source_code_uri: https://github.com/nxt-insurance/nxt_config
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 2.3.0
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.1.2
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Simple configuration objects, loadable from YAML files
78
+ test_files: []