ultra_config 0.10.3 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b50e04004e0d87906974b4219a94a3dd017ba295
4
- data.tar.gz: 1644257a95dd792b4bbaabf48a90a10844421b57
3
+ metadata.gz: 52234a8d4ae01dd19e6828c14d09a2af1acab2e3
4
+ data.tar.gz: 2ad472cfa2349d68b08159f46794ffa6ca471cf5
5
5
  SHA512:
6
- metadata.gz: 773c90ae3b237cb57182b090e27f6e92318627bac547cbbee26437c217841a685742bab8d5af4dbb55151ad95db8f331a2bd9752f89648c5534007b7494545b6
7
- data.tar.gz: 0add8b7940f40224edea51a25fdcef0aab0fc9e99898d81863f8b6bc6dcf71e366c7803700ffe4078e949f95209dbbf3fb1ee31862a34c9a93e9912353066445
6
+ metadata.gz: f195f6529ae3f645dee2e71535b22849b759763662f9b2675370fd020b0e92d77c834c857341c6042b66dfac270f24231b00899f79e582b3404b3f87652d8235
7
+ data.tar.gz: b8f9d9b9bf802db40187d93865dd3e5414b97f165e7369357f03ef23f6321401241d6e969a570d10344d99c94d0588c779aa75dfa77557901812dd662911f1f5
@@ -1,26 +1,34 @@
1
- require_relative 'validator'
1
+ require_relative 'validation'
2
2
  require_relative 'settings'
3
3
 
4
4
  module UltraConfig
5
5
  class Config
6
+ include Validation
7
+
6
8
  attr_reader :value
7
9
 
8
- def initialize(default_value, options = {}, &block)
9
- @validation = block
10
+ def initialize(options = {}, &block)
11
+ @config_block = block
10
12
 
13
+ @value = options[:default] || nil
11
14
  @sanitize = options[:sanitize] || false
12
-
13
- self.value=(default_value)
15
+ @error_msg = options[:error_msg]
14
16
  end
15
17
 
16
18
  def value=(value)
17
- validate(value)
18
-
19
- @value = value
19
+ @intermediate_value = value
20
+ self.instance_eval(&@config_block) if @config_block
21
+ type_safety(Settings.type_safety) unless @type_safety_checked
22
+ @value = @intermediate_value
23
+ rescue UltraConfig::Validation::ValidationError
24
+ raise UltraConfig::Validation::ValidationError.new(msg: @error_msg)
25
+ ensure
26
+ @type_safety_checked = false
27
+ @intermediate_value = nil
20
28
  end
21
29
 
22
- def validate(new_value)
23
- Validator.validate(@value, new_value, &@validation)
30
+ def set(&block)
31
+ @intermediate_value = yield(@intermediate_value)
24
32
  end
25
33
 
26
34
  def sanitize?
@@ -25,8 +25,8 @@ module UltraConfig
25
25
  define_singleton_method(name) { @objects[name] }
26
26
  end
27
27
 
28
- def config(name, default = nil, options = {}, &block)
29
- @objects[name] = Config.new(default, options, &block)
28
+ def config(name, options = {}, &block)
29
+ @objects[name] = Config.new(options, &block)
30
30
  define_singleton_method("#{name}=") { |value| @objects[name].value = value }
31
31
  define_singleton_method(name) { @objects[name].value }
32
32
  end
@@ -0,0 +1,41 @@
1
+ require_relative 'utilities/boolean'
2
+
3
+ module UltraConfig
4
+ module Validation
5
+ include Boolean
6
+
7
+ class ValidationError < StandardError;
8
+ def initialize(msg: nil)
9
+ super
10
+ end
11
+ end
12
+
13
+ class TypeValidationError < ValidationError; end
14
+
15
+ def type_safety(type)
16
+ @type_safety_checked = true
17
+
18
+ return unless type == :strong
19
+ return if @value.nil?
20
+ return if @value.is_a?(Boolean) && @intermediate_value.is_a?(Boolean)
21
+
22
+ raise TypeValidationError if @value.class != @intermediate_value.class
23
+ end
24
+
25
+ def one_of(list)
26
+ raise ValidationError unless list.include?(@intermediate_value)
27
+ end
28
+
29
+ def match(regexp)
30
+ raise ValidationError unless regexp.match(@intermediate_value)
31
+ end
32
+
33
+ def range(low, high)
34
+ raise ValidationError unless (@intermediate_value >= low && @intermediate_value <= high)
35
+ end
36
+
37
+ def custom(&block)
38
+ raise ValidationError unless block.call(@intermediate_value)
39
+ end
40
+ end
41
+ end
data/ultra_config.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'ultra_config'
7
- spec.version = '0.10.3'
7
+ spec.version = '0.11.0'
8
8
  spec.authors = ['Gregory Kuruc']
9
9
  spec.email = ['gjk910327@gmail.com']
10
10
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ultra_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.3
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregory Kuruc
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-07-13 00:00:00.000000000 Z
11
+ date: 2018-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -69,7 +69,7 @@ files:
69
69
  - lib/ultra_config/namespace.rb
70
70
  - lib/ultra_config/settings.rb
71
71
  - lib/ultra_config/utilities/boolean.rb
72
- - lib/ultra_config/validator.rb
72
+ - lib/ultra_config/validation.rb
73
73
  - ultra_config.gemspec
74
74
  homepage: https://github.com/SpyMachine/UltraConfig
75
75
  licenses:
@@ -1,51 +0,0 @@
1
- require_relative 'utilities/boolean'
2
-
3
- module UltraConfig
4
- class Validator
5
- extend Boolean
6
-
7
- class ValidationError < StandardError; end
8
- class TypeValidationError < ValidationError; end
9
-
10
- def self.validate(old, new, &validation)
11
- @old_value = old
12
- @test_value = new
13
-
14
- return if @test_value.nil?
15
-
16
- self.instance_eval(&validation) if validation
17
-
18
- type_safety(Settings.type_safety) unless @explicit_type_safety
19
- ensure
20
- @test_value = nil
21
- @old_value = nil
22
- @explicit_type_safety = false
23
- end
24
-
25
- def self.type_safety(type)
26
- @explicit_type_safety = true
27
- return unless type == :strong
28
-
29
- return if @old_value.nil?
30
- return if @old_value.is_a?(Boolean) && @test_value.is_a?(Boolean)
31
-
32
- raise TypeValidationError if @old_value.class != @test_value.class
33
- end
34
-
35
- def self.one_of(list)
36
- raise ValidationError unless list.include?(@test_value)
37
- end
38
-
39
- def self.match(regexp)
40
- raise ValidationError unless regexp.match(@test_value)
41
- end
42
-
43
- def self.range(low, high)
44
- raise ValidationError unless (@test_value >= low && @test_value <= high)
45
- end
46
-
47
- def self.custom(&block)
48
- raise ValidationError unless block.call(@test_value)
49
- end
50
- end
51
- end