ultra_config 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +75 -0
- data/Rakefile +6 -0
- data/lib/ultra_config/config.rb +23 -0
- data/lib/ultra_config/namespace.rb +30 -0
- data/lib/ultra_config/settings.rb +21 -0
- data/lib/ultra_config/validator.rb +33 -0
- data/lib/ultra_config.rb +7 -0
- data/ultra_config.gemspec +25 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c79ae06d3abf8defa8d9d2c384c7ceafcac835ed
|
4
|
+
data.tar.gz: 2e66a67f0e192c9a441f058cc86ddeb2ea93b738
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 468e472ebd07dbd27e9612abb73af26bba1d0a8200d0d3272bd9b364cce643fdbd43c174dffda7636b31b1acb580c58e530c386f769e1a5774a496c5517b13cb
|
7
|
+
data.tar.gz: e1ce646a4abcee89d2646311ae824d34fbdc8bbe47cd345437b735768394067b639d0deeabab5fb1dcf6b856bd552ae1f06b877e45f3e481c3edbaad34bec028
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Gregory Kuruc
|
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,75 @@
|
|
1
|
+
# UltraConfig
|
2
|
+
|
3
|
+
Ruby gem for application configuration with validation. UltraConfig provides an
|
4
|
+
all-in-one solution for defining your configuration with default values,
|
5
|
+
namespacing and validation.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'ultra_config'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install ultra_config
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Example Usage:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'ultra_config'
|
29
|
+
|
30
|
+
ConfigTest = UltraConfig.define do
|
31
|
+
config :blank
|
32
|
+
config :default, :value
|
33
|
+
|
34
|
+
config :one_of, :this do
|
35
|
+
one_of %i[this that]
|
36
|
+
end
|
37
|
+
|
38
|
+
config :match, 'this' do
|
39
|
+
match /this/
|
40
|
+
end
|
41
|
+
|
42
|
+
config :range, 4 do
|
43
|
+
range 1, 9
|
44
|
+
end
|
45
|
+
|
46
|
+
namespace :space1 do
|
47
|
+
config :default, :another_value
|
48
|
+
end
|
49
|
+
|
50
|
+
namespace :space2 do
|
51
|
+
namespace :space3 do
|
52
|
+
config :default, :a_third_value
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# It can then be used like:
|
58
|
+
|
59
|
+
ConfigTest.space1.space2.default
|
60
|
+
|
61
|
+
```
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
## Development
|
66
|
+
|
67
|
+
After checking out the repo, run `bundle install` to install dependencies. Then, run `rake spec` to run the tests.
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ultra_config.
|
72
|
+
|
73
|
+
## License
|
74
|
+
|
75
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require_relative 'validator'
|
2
|
+
require_relative 'settings'
|
3
|
+
|
4
|
+
module UltraConfig
|
5
|
+
class Config
|
6
|
+
attr_reader :value
|
7
|
+
|
8
|
+
def initialize(default_value, &block)
|
9
|
+
@validation = block
|
10
|
+
self.value=(default_value)
|
11
|
+
end
|
12
|
+
|
13
|
+
def value=(value)
|
14
|
+
validate(value)
|
15
|
+
|
16
|
+
@value = value
|
17
|
+
end
|
18
|
+
|
19
|
+
def validate(new_value)
|
20
|
+
Validator.validate(@value, new_value, &@validation)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require_relative 'config'
|
2
|
+
|
3
|
+
module UltraConfig
|
4
|
+
class Namespace
|
5
|
+
def initialize(&block)
|
6
|
+
@objects = {}
|
7
|
+
self.instance_eval(&block)
|
8
|
+
end
|
9
|
+
|
10
|
+
def namespace(name, &block)
|
11
|
+
@objects[name] = Namespace.new(&block)
|
12
|
+
end
|
13
|
+
|
14
|
+
def config(name, default = nil, &block)
|
15
|
+
@objects[name] = Config.new(default, &block)
|
16
|
+
end
|
17
|
+
|
18
|
+
def method_missing(m, *args)
|
19
|
+
if m.to_s.end_with?('=')
|
20
|
+
@objects[m.to_s[0...-1].to_sym].value=(args[0])
|
21
|
+
else
|
22
|
+
@objects[m].is_a?(Config) ? @objects[m].value : @objects[m]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def setting(name, value)
|
27
|
+
Settings.set(name, value)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module UltraConfig
|
2
|
+
class Settings
|
3
|
+
def self.default
|
4
|
+
{
|
5
|
+
type_safety: :weak
|
6
|
+
}
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.settings
|
10
|
+
@settings ||= default
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.set(setting, value)
|
14
|
+
settings[setting] = value
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.method_missing(m)
|
18
|
+
settings[m]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module UltraConfig
|
2
|
+
class Validator
|
3
|
+
class ValidationError < StandardError; end
|
4
|
+
class TypeValidationError < ValidationError; end
|
5
|
+
|
6
|
+
def self.validate(old, new, &validation)
|
7
|
+
@test_value = new
|
8
|
+
type_safety(old) if Settings.type_safety == :strong
|
9
|
+
|
10
|
+
self.instance_eval(&validation) if validation
|
11
|
+
ensure
|
12
|
+
@test_value = nil
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.type_safety(old)
|
16
|
+
return if old.nil?
|
17
|
+
|
18
|
+
raise TypeValidationError if old.class != @test_value.class
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.one_of(list)
|
22
|
+
raise ValidationError unless list.include?(@test_value)
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.match(regexp)
|
26
|
+
raise ValidationError unless regexp.match(@test_value)
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.range(low, high)
|
30
|
+
raise ValidationError unless (@test_value >= low && @test_value <= high)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/ultra_config.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'ultra_config'
|
7
|
+
spec.version = '0.1.0'
|
8
|
+
spec.authors = ['Gregory Kuruc']
|
9
|
+
spec.email = ['gjk910327@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'Provides a simple way to define a configuration with validation'
|
12
|
+
spec.homepage = 'https://github.com/SpyMachine/UltraConfig'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ultra_config
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gregory Kuruc
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-05-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- gjk910327@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/ultra_config.rb
|
68
|
+
- lib/ultra_config/config.rb
|
69
|
+
- lib/ultra_config/namespace.rb
|
70
|
+
- lib/ultra_config/settings.rb
|
71
|
+
- lib/ultra_config/validator.rb
|
72
|
+
- ultra_config.gemspec
|
73
|
+
homepage: https://github.com/SpyMachine/UltraConfig
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.6.10
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Provides a simple way to define a configuration with validation
|
97
|
+
test_files: []
|