loadable_config 1.0.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: 2f9e359a1aa612416d1e8c84476e0f2e955208816710333d0d94e88dbcdd07a0
4
+ data.tar.gz: 5cea5fb4d8af5246fd5b36906f4120ea9a1e128a58772fe856c2ddeb467fab44
5
+ SHA512:
6
+ metadata.gz: ebe69bc54c01a15a12e0611aabace5ed1ed5fd269b0ca2ae66b1bffd291128410f2d8efa634c6ddaa8241af4a16ed559d32a8b3bc4f36dd97fbf73f133294240
7
+ data.tar.gz: cc068e42ecbf05a68307929ece847b5b3da1a8c525b1a8062df16a23c23f791f86cf833eed87741c10b4bd75fdc339a4ca4d3f8adf050e7b3ec08adc26cac735
@@ -0,0 +1,98 @@
1
+ version: 2.1
2
+
3
+ executors:
4
+ ruby:
5
+ parameters:
6
+ ruby-version:
7
+ type: string
8
+ default: "2.6"
9
+ gemfile:
10
+ type: string
11
+ default: "Gemfile"
12
+ docker:
13
+ - image: circleci/ruby:<< parameters.ruby-version >>
14
+ environment:
15
+ BUNDLE_JOBS: 3
16
+ BUNDLE_RETRY: 3
17
+ BUNDLE_PATH: vendor/bundle
18
+ RAILS_ENV: test
19
+ BUNDLE_GEMFILE: << parameters.gemfile >>
20
+
21
+ jobs:
22
+ test:
23
+ parameters:
24
+ ruby-version:
25
+ type: string
26
+ executor:
27
+ name: ruby
28
+ ruby-version: << parameters.ruby-version >>
29
+ parallelism: 1
30
+ steps:
31
+ - checkout
32
+
33
+ - run:
34
+ # Remove the non-appraisal gemfile for safety: we never want to use it.
35
+ name: Prepare bundler
36
+ command: bundle -v
37
+
38
+ - run:
39
+ name: Compute a gemfile lock
40
+ command: bundle lock && cp "${BUNDLE_GEMFILE}.lock" /tmp/gem-lock
41
+
42
+ - restore_cache:
43
+ keys:
44
+ - loadable_config-<< parameters.ruby-version >>-{{ checksum "/tmp/gem-lock" }}
45
+ - loadable_config-
46
+
47
+ - run:
48
+ name: Bundle Install
49
+ command: bundle check || bundle install
50
+
51
+ - save_cache:
52
+ key: loadable_config-<< parameters.ruby-version >>-{{ checksum "/tmp/gem-lock" }}
53
+ paths:
54
+ - vendor/bundle
55
+
56
+ - run:
57
+ name: Run rspec
58
+ command: bundle exec rspec --profile 10 --format RspecJunitFormatter --out test_results/rspec.xml --format progress
59
+
60
+ - store_test_results:
61
+ path: test_results
62
+
63
+ publish:
64
+ executor: ruby
65
+ steps:
66
+ - checkout
67
+ - run:
68
+ name: Setup Rubygems
69
+ command: |
70
+ mkdir ~/.gem &&
71
+ echo -e "---\r\n:rubygems_api_key: $RUBYGEMS_API_KEY" > ~/.gem/credentials &&
72
+ chmod 0600 ~/.gem/credentials
73
+ - run:
74
+ name: Publish to Rubygems
75
+ command: |
76
+ gem build loadable_config.gemspec
77
+ gem push loadable_config-*.gem
78
+
79
+
80
+ workflows:
81
+ version: 2.1
82
+ build:
83
+ jobs:
84
+ - test:
85
+ name: 'ruby 2.5'
86
+ ruby-version: "2.5"
87
+ - test:
88
+ name: 'ruby 2.6'
89
+ ruby-version: "2.6"
90
+ - publish:
91
+ requires:
92
+ - 'ruby 2.5'
93
+ - 'ruby 2.6'
94
+ filters:
95
+ branches:
96
+ only: master
97
+ tags:
98
+ ignore: /.*/
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 @@
1
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ dist: trusty
2
+ sudo: false
3
+ language: ruby
4
+ rvm:
5
+ - 2.5
6
+ notifications:
7
+ email: false
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in loadable_config.gemspec
4
+ gemspec
5
+ gem 'rspec_junit_formatter'
data/LICENSE.txt ADDED
@@ -0,0 +1,9 @@
1
+ Copyright (c) 2017, DMM.com. All rights reserved.
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ [![Build Status](https://travis-ci.org/iknow/loadable_config.svg?branch=master)](https://travis-ci.org/iknow/loadable_config)
2
+
3
+ # LoadableConfig
4
+ LoadableConfig is a tool you can use to load and parse configuration
5
+ from YAML. It validates your declaration of the configuration against
6
+ the provided file and gives you a singleton to access configuration.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'loadable_config'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install loadable_config
23
+
24
+ ## Usage
25
+
26
+ Subclass from LoadableConfig:
27
+
28
+ ```ruby
29
+ class MyConfig < LoadableConfig
30
+ attribute :size, type: :integer
31
+ attribute :name # implictly a string
32
+
33
+ config_file 'config/my_config.yml'
34
+ end
35
+ ```
36
+
37
+ You can then access the configuration through the instance of the class:
38
+
39
+ `MyConfig.instance.size # => 3`
40
+
41
+ If you store configuration keyed by environment, or want to set a
42
+ project root for all subclasses, you can set global options:
43
+
44
+ ```ruby
45
+ LoadableConfig.configure! do |config|
46
+ config.config_path_prefix = Rails.root
47
+ config.environment_key = Rails.env
48
+ end
49
+ ```
50
+
51
+ Attributes are validated according to [JSON Schema](http://json-schema.org)
52
+
53
+ # Development
54
+
55
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
56
+
57
+ 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).
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at https://github.com/iknow/loadable_config.
62
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "loadable_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
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,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'loadable_config/version'
4
+ require 'loadable_config/options'
5
+ require 'json_schema'
6
+ require 'yaml'
7
+ require 'singleton'
8
+
9
+ class LoadableConfig
10
+ Attribute = Struct.new(:name, :type, :schema, :optional, :serializer)
11
+
12
+ class << self
13
+ attr_reader :_attributes, :_config_file
14
+
15
+ def inherited(subclass)
16
+ subclass.send(:include, Singleton)
17
+ end
18
+
19
+ def config_file(path)
20
+ @_config_file = path
21
+ end
22
+
23
+ def attribute(attr, type: :string, schema: {}, optional: false, serializer: nil)
24
+ @_attributes ||= []
25
+ attr = attr.to_sym
26
+ if ATTRIBUTE_BLACKLIST.include?(attr)
27
+ raise ArgumentError.new("Illegal attribute name '#{attr}': "\
28
+ 'attributes must not collide with class methods of LoadableConfig')
29
+ end
30
+
31
+ type = [type] unless type.kind_of?(Array)
32
+ type.map! { |t| t.to_s }
33
+
34
+ _attributes << Attribute.new(attr.to_s, type, schema, optional, serializer)
35
+ attr_accessor attr
36
+ define_singleton_method(attr) { instance.send(attr) }
37
+ end
38
+
39
+ def attributes(*attrs, type: :string, optional: false, serializer: nil)
40
+ attrs.each do |attr|
41
+ attribute(attr, type: type, optional: optional, serializer: serializer)
42
+ end
43
+ end
44
+
45
+ def _configuration
46
+ @@_configuration
47
+ end
48
+
49
+ def configure!
50
+ if @@_configuration.frozen?
51
+ raise ArgumentError.new('Cannot configure LoadableConfig: already configured')
52
+ end
53
+ yield(@@_configuration)
54
+ @@_configuration.freeze
55
+ end
56
+
57
+ private
58
+
59
+ def _reinitialize_configuration!
60
+ @@_configuration = LoadableConfig::Options.new
61
+ end
62
+ end
63
+
64
+ _reinitialize_configuration!
65
+
66
+ ATTRIBUTE_BLACKLIST = Set.new(self.methods - Object.instance_methods)
67
+
68
+ def initialize
69
+ if self.class._config_file.nil? || self.class._config_file.empty?
70
+ raise RuntimeError.new("Incomplete LoadableConfig '#{self.class.name}': config_file not set")
71
+ end
72
+
73
+ config_file_path = self.class._config_file
74
+
75
+ if (prefix = LoadableConfig._configuration.config_path_prefix)
76
+ config_file_path = File.join(prefix, config_file_path)
77
+ end
78
+
79
+ unless File.exist?(config_file_path)
80
+ raise RuntimeError.new("Cannot configure #{self.class.name}: "\
81
+ "configuration file '#{config_file_path}' missing")
82
+ end
83
+
84
+ config_data = File.read(config_file_path)
85
+
86
+ if (preprocessor = LoadableConfig._configuration.preprocessor)
87
+ config_data = preprocessor.call(config_data)
88
+ end
89
+
90
+ config = YAML.safe_load(config_data, [], [], true)
91
+ unless config
92
+ raise RuntimeError.new("Cannot configure #{self.class.name}: "\
93
+ "Configuration file empty for #{self.class.name}.")
94
+ end
95
+
96
+ if (env = LoadableConfig._configuration.environment_key)
97
+ config = config.fetch(env) do
98
+ raise RuntimeError.new("Cannot configure #{self.class.name}: "\
99
+ "Configuration missing for environment '#{env}'")
100
+ end
101
+ end
102
+
103
+ unless config
104
+ raise RuntimeError.new("Configuration file missing config for #{self.class.name}.")
105
+ end
106
+
107
+ valid, errors = _schema.validate(config)
108
+ unless valid
109
+ raise ArgumentError.new("Errors parsing #{self.class.name}:\n" +
110
+ errors.map { |e| "#{e.pointer}: #{e.message}" }.join("\n"))
111
+ end
112
+
113
+ self.class._attributes.each do |attr|
114
+ value = config[attr.name]
115
+ if attr.serializer
116
+ value = attr.serializer.load(value)
117
+ end
118
+ self.public_send(:"#{attr.name}=", value)
119
+ end
120
+
121
+ self.freeze
122
+ end
123
+
124
+ private
125
+
126
+ def _schema
127
+ JsonSchema.parse!(
128
+ 'type' => 'object',
129
+ 'description' => "#{self.class.name} Configuration",
130
+ 'properties' => self.class._attributes.each_with_object({}) do |attr, h|
131
+ h[attr.name] = { 'type' => attr.type }.merge!(attr.schema)
132
+ end,
133
+ 'required' => self.class._attributes.reject(&:optional).map(&:name),
134
+ 'additionalProperties' => false,
135
+ )
136
+ end
137
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ class LoadableConfig::Options
4
+ attr_reader :config_path_prefix, :environment_key, :preprocessor
5
+
6
+ def initialize
7
+ # Prefix for configuration file paths. Must be a valid directory, for
8
+ # example `Rails.root`. If unset, configuration files are resolved relative
9
+ # to the current working directory.
10
+ @config_path_prefix = nil
11
+
12
+ # If set, assumes that all LoadableConfig configuration files are structured
13
+ # with top-level keys specifying different environments, each of which is a
14
+ # hash of the configuration attributes. Setting this specifies which top
15
+ # level key to select. For example, `Rails.env`.
16
+ @environment_key = nil
17
+
18
+ # If set, uses the provided block to preprocess the configuration file
19
+ # before YAML parsing.
20
+ @preprocessor = nil
21
+ end
22
+
23
+ def config_path_prefix=(val)
24
+ unless File.directory?(val)
25
+ raise ArgumentError.new("Config path prefix '#{val}' is not a valid directory")
26
+ end
27
+
28
+ @config_path_prefix = File.expand_path(val)
29
+ end
30
+
31
+ def environment_key=(val)
32
+ @environment_key = val.to_s
33
+ end
34
+
35
+ def preprocess(&block)
36
+ @preprocessor = block
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ class LoadableConfig
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'loadable_config/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "loadable_config"
8
+ spec.version = LoadableConfig::VERSION
9
+ spec.authors = ["iKnow Team"]
10
+ spec.email = ["dev@iknow.jp"]
11
+
12
+ spec.summary = %q{Simple declarative configuration files}
13
+ spec.homepage = "http://github.com/iknow/loadable_config"
14
+ spec.license = "BSD-2-Clause"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "json_schema"
22
+
23
+ spec.add_development_dependency "bundler"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec"
26
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: loadable_config
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - iKnow Team
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-04-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json_schema
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
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
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'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - dev@iknow.jp
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".circleci/config.yml"
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/loadable_config.rb
87
+ - lib/loadable_config/options.rb
88
+ - lib/loadable_config/version.rb
89
+ - loadable_config.gemspec
90
+ homepage: http://github.com/iknow/loadable_config
91
+ licenses:
92
+ - BSD-2-Clause
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubygems_version: 3.0.3
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Simple declarative configuration files
113
+ test_files: []