definition 0.5.2 → 0.6.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
  SHA256:
3
- metadata.gz: db6d3a697d9641388ce9045cd7b9504d485e165480339e864dca6fe096289c6f
4
- data.tar.gz: ef5adc969fde0a0ca0a3e3b9a365cbce588379d2e0bd47be36567b2f4568e7e9
3
+ metadata.gz: b4e5ac14f31ff6bdd8bab587f652fa408ac28285c92bedf457ded9383cc8a5a9
4
+ data.tar.gz: 8ed978aa993d0b68fd95dbc24e5800ab38b416c39798a33c8f6f039dfce201d7
5
5
  SHA512:
6
- metadata.gz: 800d13aa9d79318e639eaa969ab6d5d5e8737160f0852df873e38e18685ac58b6133f81e8327de2a937e6a9432e8e68ef26bf0b3758fad4a6c757db5b134572d
7
- data.tar.gz: 52082265fba3abe62846ccf4236888c9d5f6f2e04a9e265282da1526572b3e39b5c3bed3e6490fb5e98525165f74caed5064bdbc479053471e913175313ab353
6
+ metadata.gz: 63f912d53159f378ec03ff69d9907978e739f96bb994f89f9131d3ee53f2b07ce0621aa2cae6ddf5b81d475dc63543b24f1da2474476e96c62f22e3f7d5ae3b3
7
+ data.tar.gz: 60234129f0693be1e83b1265c3a435bbf3e3cea4990a882a722722f022f31b02736378c53cfc9f17d4ada50163d0bfc33f14c21b92899466110ab9ce53461bac
@@ -1,21 +1,18 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - jruby-9.1.17.0 # ruby 2.3
4
- - jruby-9.2.0.0 # ruby 2.5
5
- - jruby-head
4
+ - jruby-9.2.11.1 # ruby 2.5.7
6
5
  - 2.3.0
7
- - 2.4.0
8
- - 2.5.0
9
- - ruby-head
6
+ - 2.7.0
10
7
  jobs:
11
8
  include:
12
9
  - stage: linting
13
- rvm: ruby-head
10
+ rvm: 2.7.0
14
11
  script: bundle exec rake rubocop
15
12
  - stage: benchmark
16
13
  script: bundle exec ruby benchmark/complex_example.rb
17
- rvm: ruby-head
14
+ rvm: 2.7.0
18
15
  - script: bundle exec ruby benchmark/coercion.rb
19
- rvm: ruby-head
16
+ rvm: 2.7.0
20
17
  - script: bundle exec ruby benchmark/validation_only.rb
21
- rvm: ruby-head
18
+ rvm: 2.7.0
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [0.4.0] - 2020-03-21
8
+ ### Added
9
+ - Added include method to Keys Definition that allows to inline other `Keys` Definitions into each other
10
+
7
11
  ## [0.5.2] - 2019-06-03
8
12
  ### Fixed
9
13
  - added missing require for "pathname"
data/README.md CHANGED
@@ -279,6 +279,35 @@ order = Definition.Keys do
279
279
  end
280
280
  ```
281
281
 
282
+ ### Extending Key definitions with include
283
+
284
+ Besides composing Definitions, you can also include `Keys` Definitions in each
285
+ other. This will basically copy all required and optional keys as well as defaults into the other definition.
286
+
287
+ ```ruby
288
+ address_definition = Definition.Keys do
289
+ required :street, Definition.Type(String)
290
+ required :postal_code, Definition.Type(String)
291
+ required :country_code, Definition.Type(String)
292
+ end
293
+
294
+ user_definition = Definition.Keys do
295
+ required :user, user_definition
296
+
297
+ include address_definition
298
+ end
299
+ ```
300
+ Above Definition will equal the following:
301
+ ```ruby
302
+ user_definition = Definition.Keys do
303
+ required :user, user_definition
304
+
305
+ required :street, Definition.Type(String)
306
+ required :postal_code, Definition.Type(String)
307
+ required :country_code, Definition.Type(String)
308
+ end
309
+ ```
310
+
282
311
  ### Predefined Definitions
283
312
 
284
313
  #### Strings and Arrays
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # frozen_string_literal: true
4
-
5
3
  require "definition/types/base"
6
4
  require "definition/types/include"
7
5
  require "definition/key_conform_error"
@@ -19,10 +17,6 @@ module Definition
19
17
  default(key, opts[:default]) if opts.key?(:default)
20
18
  end
21
19
 
22
- def default(key, value)
23
- defaults[key] = value
24
- end
25
-
26
20
  def option(option_name)
27
21
  case option_name
28
22
  when :ignore_extra_keys
@@ -31,6 +25,36 @@ module Definition
31
25
  raise "Option #{option_name} is not defined"
32
26
  end
33
27
  end
28
+
29
+ def include(other)
30
+ raise ArgumentError.new("Included Definition can only be a Keys Definition") unless other.is_a?(Types::Keys)
31
+
32
+ ensure_keys_do_not_interfere(other)
33
+ other.required_definitions.each do |key, definition|
34
+ required(key, definition)
35
+ end
36
+ other.optional_definitions.each do |key, definition|
37
+ optional(key, definition)
38
+ end
39
+ other.defaults.each do |key, default|
40
+ default(key, default)
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def default(key, value)
47
+ defaults[key] = value
48
+ end
49
+
50
+ def ensure_keys_do_not_interfere(other)
51
+ overlapping_keys = keys & other.keys
52
+ return if overlapping_keys.empty?
53
+
54
+ raise ArgumentError.new(
55
+ "Included definition tries to redefine already defined fields: #{overlapping_keys.join(', ')}"
56
+ )
57
+ end
34
58
  end
35
59
 
36
60
  include Dsl
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Definition
4
- VERSION = "0.5.2"
4
+ VERSION = "0.6.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: definition
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dominik Goltermann
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-12 00:00:00.000000000 Z
11
+ date: 2020-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport