adaptiveconfiguration 1.0.0.beta08 → 1.0.0.beta09

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
  SHA256:
3
- metadata.gz: cca3cc7af7f052e713d6d1065610f79fe1857f4c677c9cf860098b6136aad366
4
- data.tar.gz: 28a8496792f95133a62b7a3696d5dfff4b96fa5ed2d8214f50831b4a8c78996b
3
+ metadata.gz: b83c4f1458551684c09f324ecbb13a64ad2fa51ba83747d2a21f835fa706555f
4
+ data.tar.gz: f75a7d19d43814c81755f27096e707dbe7ab50ee55354ba42039c5f94fd51143
5
5
  SHA512:
6
- metadata.gz: 2e8df5aa99cdc79ce4aedac99a737f09c814df5e6287f30c4fb3595ce93fff7417d1311d150e9eddaca8fc6ea60dd82ff7d7ffed946f49463b30411356eeb147
7
- data.tar.gz: ac67be155f79e5261c75bd805d0eaec9825cc2aa354b556227c4f9d8968b0cdd2ae634ed0377f024acdf2d4b518a5811a90a6006c42057dbe6911fb9d412c0a0
6
+ metadata.gz: 3427a2d7edbfc38bd479b461844c43d6f53e61fdde7e479446d87408cf758124879b76c3b4860aacd8f382eacc961c399b64958c8302d1bc1620fca78437fb94
7
+ data.tar.gz: 19fe7d3db4c51159706c9ec982b5b56694b2c124ef661bf19417606e01463f53f2252b025b5832afdb995ced2fbd857465c9de36c8354ae72a4ff0234a2868d4
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do | spec |
2
2
 
3
3
  spec.name = 'adaptiveconfiguration'
4
- spec.version = '1.0.0.beta08'
4
+ spec.version = '1.0.0.beta09'
5
5
  spec.authors = [ 'Kristoph Cichocki-Romanov' ]
6
6
  spec.email = [ 'rubygems.org@kristoph.net' ]
7
7
 
@@ -1,13 +1,13 @@
1
- require_relative 'group_builder'
1
+ require_relative 'properties_builder'
2
2
  require_relative 'scaffold'
3
3
 
4
- # types must be included to support conversation
4
+ # types must be included to support coersion
5
5
  require 'time'
6
6
  require 'date'
7
7
  require 'uri'
8
8
 
9
9
  module AdaptiveConfiguration
10
- class Builder < GroupBuilder
10
+ class Builder < PropertiesBuilder
11
11
 
12
12
  DEFAULT_CONVERTERS = {
13
13
 
@@ -106,8 +106,7 @@ module AdaptiveConfiguration
106
106
 
107
107
  path.chomp( '/' ) if path
108
108
  unless values.is_a?( Hash )
109
- # TODO: raise error
110
- return
109
+ raise ArgumentError, "The values must always be a Hash."
111
110
  end
112
111
 
113
112
  definitions.each do | key, definition |
@@ -117,10 +116,14 @@ module AdaptiveConfiguration
117
116
 
118
117
  if definition[ :required ] &&
119
118
  ( !value || ( value.respond_to?( :empty ) && value.empty? ) )
120
- block.call( RequirementUnmetError.new( path: path, key: key ) )
119
+ block.call( RequiredOptionError.new( path: path, key: key ) )
120
+ elsif definition[ :in ] && !definition[ :in ].include?( value )
121
+ block.call(
122
+ InOptionError.new( path: path, key: key, option: definition[ :in ], value: value )
123
+ )
121
124
  elsif !definition[ :default_assigned ] && !value.nil?
122
125
  unless definition[ :array ]
123
- if definition[ :type ] == :group
126
+ if definition[ :type ] == Object
124
127
  traverse_and_validate_values(
125
128
  values[ name ],
126
129
  definitions: definition[ :definitions ],
@@ -137,7 +140,7 @@ module AdaptiveConfiguration
137
140
  end
138
141
  end
139
142
  else
140
- if definition[ :type ] == :group
143
+ if definition[ :type ] == Object
141
144
  groups = Array( value )
142
145
  groups.each do | group |
143
146
  traverse_and_validate_values(
@@ -2,7 +2,6 @@ module AdaptiveConfiguration
2
2
 
3
3
  class Error < StandardError; end
4
4
 
5
-
6
5
  class IncompatibleTypeError < Error
7
6
 
8
7
  attr_reader :keypath
@@ -17,12 +16,12 @@ module AdaptiveConfiguration
17
16
  @type = type
18
17
  type_text = @type.respond_to?( :join ) ? type.join( ', ' ) : type
19
18
 
20
- super( "The parameter '#{@keypath}' expects #{type_text} but received incompatible #{value.class.name}." )
19
+ super( "The parameter '#{@keypath}' expects '#{type_text}' but incompatible '#{value.class.name}' was given." )
21
20
  end
22
21
 
23
22
  end
24
23
 
25
- class RequirementUnmetError < Error
24
+ class RequiredOptionError < Error
26
25
 
27
26
  attr_reader :keypath
28
27
  attr_reader :key
@@ -31,7 +30,21 @@ module AdaptiveConfiguration
31
30
  path = path ? path.chomp( '/' ) : nil
32
31
  @key = key
33
32
  @keypath = path ? ( path + '/' + @key.to_s ) : key.to_s
34
- super( "The parameter #{@keypath} is required." )
33
+ super( "The parameter '#{@keypath}' is required but no value was given." )
34
+ end
35
+
36
+ end
37
+
38
+ class InOptionError < Error
39
+
40
+ attr_reader :keypath
41
+ attr_reader :key
42
+
43
+ def initialize( path: nil, key:, option:, value: )
44
+ path = path ? path.chomp( '/' ) : nil
45
+ @key = key
46
+ @keypath = path ? ( path + '/' + @key.to_s ) : key.to_s
47
+ super( "The parameter '#{@keypath}' must be in '#{option.to_s}' but '#{value.to_s}' was given." )
35
48
  end
36
49
 
37
50
  end
@@ -1,7 +1,7 @@
1
1
  require_relative 'scaffold'
2
2
 
3
3
  module AdaptiveConfiguration
4
- class GroupBuilder
4
+ class PropertiesBuilder
5
5
 
6
6
  attr_reader :definitions
7
7
 
@@ -25,23 +25,33 @@ module AdaptiveConfiguration
25
25
  options = args[ 1 ] || {}
26
26
  options[ :type ] = args.first
27
27
  end
28
+
29
+ validate_in!( name, options[ :type ], options[ :in ] ) if options[ :in ]
28
30
 
29
31
  @definitions[ name ] = options
30
32
  end
31
33
 
32
- def group( name, options = {}, &block )
34
+ def parameters( name, options = {}, &block )
33
35
 
34
36
  raise NameError, "The name '#{name}' is reserved and cannot be used for parameters." \
35
37
  if AdaptiveConfiguration::Scaffold.instance_methods.include?( name )
36
38
 
37
- builder = GroupBuilder.new
39
+ builder = PropertiesBuilder.new
38
40
  builder.instance_eval( &block ) if block
39
41
  @definitions[ name ] = options.merge( {
40
- type: :group,
42
+ type: Object,
41
43
  definitions: builder.definitions
42
44
  } )
43
45
 
44
46
  end
45
47
 
48
+ private
49
+
50
+ def validate_in!( name, type, in_option )
51
+ raise TypeError,
52
+ "The parameter '#{name}' includes the :in option but it does not respond to 'include?'." \
53
+ unless in_option.respond_to?( :include? )
54
+ end
55
+
46
56
  end
47
57
  end
@@ -85,7 +85,7 @@ module AdaptiveConfiguration
85
85
  name = definition[ :as ] || method
86
86
 
87
87
  unless definition[ :array ]
88
- if definition[ :type ] == :group
88
+ if definition[ :type ] == ::Object
89
89
  value = args.first
90
90
  context = @values[ name ]
91
91
  if context.nil? || value
@@ -107,7 +107,7 @@ module AdaptiveConfiguration
107
107
  @values[ name ] = definition[ :default_assigned ] ?
108
108
  ::Array.new :
109
109
  @values[ name ] || ::Array.new
110
- if definition[ :type ] == :group
110
+ if definition[ :type ] == ::Object
111
111
  values = [ args.first ].flatten
112
112
  values = values.map do | v |
113
113
  context = Scaffold.new(
@@ -0,0 +1,26 @@
1
+ require_relative 'builder'
2
+
3
+ module AdaptiveConfiguration
4
+ module Configurable
5
+
6
+ def configuration( &block )
7
+ @configuration_builder ||= AdaptiveConfiguration::Builder.new
8
+ @configuration_builder.instance_eval( &block )
9
+ @configuration_builder
10
+ end
11
+
12
+ def configure( attributes = nil, &block )
13
+ raise RuntimeError, "The configuration has not been defined." \
14
+ if @configuration_builder.nil?
15
+ configuration = @configuration_builder.build( attributes, &block )
16
+ end
17
+
18
+ def configure!( attributes = nil, &block )
19
+ raise RuntimeError, "The configuration has not been defined." \
20
+ if @configuration_builder.nil?
21
+ configuration = @configuration_builder.build!( attributes, &block )
22
+ end
23
+
24
+ end
25
+ end
26
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adaptiveconfiguration
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.beta08
4
+ version: 1.0.0.beta09
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kristoph Cichocki-Romanov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-13 00:00:00.000000000 Z
11
+ date: 2024-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -58,8 +58,9 @@ files:
58
58
  - lib/adaptive_configuration/builder.rb
59
59
  - lib/adaptive_configuration/configurable.rb
60
60
  - lib/adaptive_configuration/errors.rb
61
- - lib/adaptive_configuration/group_builder.rb
61
+ - lib/adaptive_configuration/properties_builder.rb
62
62
  - lib/adaptive_configuration/scaffold.rb
63
+ - lib/adaptive_configuration/validatable.rb
63
64
  homepage: https://github.com/EndlessInternational/adaptive_configuration
64
65
  licenses:
65
66
  - MIT