definition 0.5.0 → 0.6.1

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
- SHA1:
3
- metadata.gz: 2e17d7407705fa6a48ed0a3852b73abe91688fbe
4
- data.tar.gz: 118a8ad2dc4663c8666a25b68614f89f8546ab16
2
+ SHA256:
3
+ metadata.gz: 4c7bb937fe678e70bbb9427ca24b8153471294150a657388b913aa095c0b5864
4
+ data.tar.gz: 579e9943b959417867e641284c0b94cde45ceb56c9d3738a98a37b6f100cd854
5
5
  SHA512:
6
- metadata.gz: fe00bb7ab22dbc910ab68df1e054e5d15618d3e02d91a266c2f1fb07c48a4d95553c0071fbe3f5e3274d446d833491efd1753cca8d6ef625c25aa17747c129c6
7
- data.tar.gz: 5cee41aff7c8c40cb54c533f6424046152fdcdfd9a971e17fc11209ec833b21e3834d77158c200430678d9683e9fcdc510aac0527838276fc502a74ab91b5451
6
+ metadata.gz: 7319c413e131eeef99ac40eb4b57cead39f27d86912651049bbe3276a333fc8aedafa77f08e13ce4fca4e973baf02e48a6ab49b07cbafb561f04fd3cf3224055
7
+ data.tar.gz: 34ffc1084ad2d22fb38248d9e88f9d4b13349c94e7376ec94eabd6765ff6986230612dc0f864f0b900fd56203cb8fb5f415889b47ab6427f0c85b1acf8ada029
@@ -0,0 +1,43 @@
1
+ # Use the latest 2.1 version of CircleCI pipeline process engine.
2
+ # See: https://circleci.com/docs/2.0/configuration-reference
3
+ version: 2.1
4
+
5
+ # Orbs are reusable packages of CircleCI configuration that you may share across projects, enabling you to create encapsulated, parameterized commands, jobs, and executors that can be used across multiple projects.
6
+ # See: https://circleci.com/docs/2.0/orb-intro/
7
+ orbs:
8
+ ruby: circleci/ruby@1.2.0
9
+
10
+ # Define a job to be invoked later in a workflow.
11
+ # See: https://circleci.com/docs/2.0/configuration-reference/#jobs
12
+ jobs:
13
+ test:
14
+ parameters:
15
+ image:
16
+ type: string
17
+ docker:
18
+ - image: << parameters.image>>
19
+ steps:
20
+ - checkout
21
+ - ruby/install-deps
22
+ - ruby/rspec-test
23
+ checking:
24
+ docker:
25
+ - image: 'cimg/ruby:2.7'
26
+ steps:
27
+ - checkout
28
+ - ruby/install-deps
29
+ - ruby/rubocop-check:
30
+ format: progress
31
+ label: Inspecting with Rubocop
32
+
33
+ # Invoke jobs via workflows
34
+ # See: https://circleci.com/docs/2.0/configuration-reference/#workflows
35
+ workflows:
36
+ test: # This is the name of the workflow, feel free to change it to better match your workflow.
37
+ # Inside the workflow, you define the jobs you want to run.
38
+ jobs:
39
+ - test:
40
+ matrix:
41
+ parameters:
42
+ image: ["cimg/ruby:2.5", "cimg/ruby:2.6", "cimg/ruby:2.7", "circleci/jruby:9.3.1-jdk11", "circleci/jruby:9.2.20-jdk11"]
43
+ - checking
data/.gitignore CHANGED
@@ -1,6 +1,5 @@
1
1
  /.bundle/
2
2
  /.yardoc
3
- /Gemfile.lock
4
3
  /_yardoc/
5
4
  /coverage/
6
5
  /doc/
data/Changelog.md CHANGED
@@ -4,6 +4,22 @@ 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.6.1] - 2020-03-21
8
+ ### Fixed
9
+ - The `Keys` definition crashed with an error if the input was not a Hash
10
+
11
+ ## [0.6.0] - 2020-03-21
12
+ ### Added
13
+ - Added include method to Keys Definition that allows to inline other `Keys` Definitions into each other
14
+
15
+ ## [0.5.2] - 2019-06-03
16
+ ### Fixed
17
+ - added missing require for "pathname"
18
+
19
+ ## [0.5.1] - 2019-04-27
20
+ ### Fixed
21
+ - Typo in error debug output
22
+
7
23
  ## [0.5.0] - 2019-03-28
8
24
  ### Added
9
25
  - CoercibleValueObject Definition for better nesting of ValueObjects
data/Gemfile.lock ADDED
@@ -0,0 +1,153 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ definition (0.6.1)
5
+ activesupport
6
+ i18n
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activesupport (6.1.4.1)
12
+ concurrent-ruby (~> 1.0, >= 1.0.2)
13
+ i18n (>= 1.6, < 2)
14
+ minitest (>= 5.1)
15
+ tzinfo (~> 2.0)
16
+ zeitwerk (~> 2.3)
17
+ approvals (0.0.25)
18
+ nokogiri (~> 1.8)
19
+ thor (~> 1.0)
20
+ ast (2.4.2)
21
+ awesome_print (1.9.2)
22
+ benchmark-ips (2.9.2)
23
+ coderay (1.1.3)
24
+ concurrent-ruby (1.1.9)
25
+ diff-lcs (1.4.4)
26
+ ffi (1.15.4)
27
+ ffi (1.15.4-java)
28
+ formatador (0.3.0)
29
+ fuubar (2.5.1)
30
+ rspec-core (~> 3.0)
31
+ ruby-progressbar (~> 1.4)
32
+ guard (2.18.0)
33
+ formatador (>= 0.2.4)
34
+ listen (>= 2.7, < 4.0)
35
+ lumberjack (>= 1.0.12, < 2.0)
36
+ nenv (~> 0.1)
37
+ notiffany (~> 0.0)
38
+ pry (>= 0.13.0)
39
+ shellany (~> 0.0)
40
+ thor (>= 0.18.1)
41
+ guard-compat (1.2.1)
42
+ guard-rspec (4.7.3)
43
+ guard (~> 2.1)
44
+ guard-compat (~> 1.1)
45
+ rspec (>= 2.99.0, < 4.0)
46
+ i18n (1.8.11)
47
+ concurrent-ruby (~> 1.0)
48
+ jar-dependencies (0.4.1)
49
+ jaro_winkler (1.5.4)
50
+ jaro_winkler (1.5.4-java)
51
+ listen (3.7.0)
52
+ rb-fsevent (~> 0.10, >= 0.10.3)
53
+ rb-inotify (~> 0.9, >= 0.9.10)
54
+ lumberjack (1.2.8)
55
+ method_source (1.0.0)
56
+ mini_portile2 (2.6.1)
57
+ minitest (5.14.4)
58
+ nenv (0.3.0)
59
+ nokogiri (1.12.5)
60
+ mini_portile2 (~> 2.6.1)
61
+ racc (~> 1.4)
62
+ nokogiri (1.12.5-java)
63
+ racc (~> 1.4)
64
+ nokogiri (1.12.5-x86_64-linux)
65
+ racc (~> 1.4)
66
+ notiffany (0.1.3)
67
+ nenv (~> 0.1)
68
+ shellany (~> 0.0)
69
+ parallel (1.21.0)
70
+ parser (3.0.3.2)
71
+ ast (~> 2.4.1)
72
+ pry (0.14.1)
73
+ coderay (~> 1.1)
74
+ method_source (~> 1.0)
75
+ pry (0.14.1-java)
76
+ coderay (~> 1.1)
77
+ method_source (~> 1.0)
78
+ spoon (~> 0.0)
79
+ psych (4.0.2)
80
+ psych (4.0.2-java)
81
+ jar-dependencies (>= 0.1.7)
82
+ racc (1.6.0)
83
+ racc (1.6.0-java)
84
+ rainbow (3.0.0)
85
+ rake (13.0.6)
86
+ rb-fsevent (0.11.0)
87
+ rb-inotify (0.10.1)
88
+ ffi (~> 1.0)
89
+ rspec (3.10.0)
90
+ rspec-core (~> 3.10.0)
91
+ rspec-expectations (~> 3.10.0)
92
+ rspec-mocks (~> 3.10.0)
93
+ rspec-core (3.10.1)
94
+ rspec-support (~> 3.10.0)
95
+ rspec-expectations (3.10.1)
96
+ diff-lcs (>= 1.2.0, < 2.0)
97
+ rspec-support (~> 3.10.0)
98
+ rspec-its (1.3.0)
99
+ rspec-core (>= 3.0.0)
100
+ rspec-expectations (>= 3.0.0)
101
+ rspec-mocks (3.10.2)
102
+ diff-lcs (>= 1.2.0, < 2.0)
103
+ rspec-support (~> 3.10.0)
104
+ rspec-support (3.10.3)
105
+ rspec_junit_formatter (0.4.1)
106
+ rspec-core (>= 2, < 4, != 2.12.0)
107
+ rubocop (0.66.0)
108
+ jaro_winkler (~> 1.5.1)
109
+ parallel (~> 1.10)
110
+ parser (>= 2.5, != 2.5.1.1)
111
+ psych (>= 3.1.0)
112
+ rainbow (>= 2.2.2, < 4.0)
113
+ ruby-progressbar (~> 1.7)
114
+ unicode-display_width (>= 1.4.0, < 1.6)
115
+ rubocop-rspec (1.32.0)
116
+ rubocop (>= 0.60.0)
117
+ rubocop_runner (2.2.0)
118
+ ruby-progressbar (1.11.0)
119
+ shellany (0.0.1)
120
+ spoon (0.0.6)
121
+ ffi
122
+ thor (1.1.0)
123
+ timecop (0.9.4)
124
+ tzinfo (2.0.4)
125
+ concurrent-ruby (~> 1.0)
126
+ unicode-display_width (1.5.0)
127
+ zeitwerk (2.5.1)
128
+
129
+ PLATFORMS
130
+ ruby
131
+ universal-java-11
132
+ x86_64-linux
133
+
134
+ DEPENDENCIES
135
+ approvals (~> 0.0)
136
+ awesome_print
137
+ benchmark-ips
138
+ definition!
139
+ fuubar
140
+ guard
141
+ guard-rspec
142
+ pry
143
+ rake (~> 13.0)
144
+ rspec (~> 3.0)
145
+ rspec-its (~> 1.2)
146
+ rspec_junit_formatter
147
+ rubocop (= 0.66.0)
148
+ rubocop-rspec (= 1.32.0)
149
+ rubocop_runner
150
+ timecop
151
+
152
+ BUNDLED WITH
153
+ 2.2.33
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Definition
2
2
 
3
- [![Build Status](https://travis-ci.org/Goltergaul/definition.svg?branch=master)][travis]
3
+ [![Build Status](https://circleci.com/gh/Goltergaul/definition.svg?style=svg)][circleci]
4
4
  [![Gem Version](https://badge.fury.io/rb/definition.svg)][rubygems]
5
5
 
6
6
  Simple and composable validation and coercion of data structures. It also includes a ValueObject for convenience.
@@ -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
@@ -343,11 +372,24 @@ Definition.Boolean.conform(tru) # => pass
343
372
  ```ruby
344
373
  Definition.Equal(5).conform(5) # => pass
345
374
  Definition.Equal("foo").conform("foo") # => pass
375
+ ```
346
376
 
377
+ The Nilable Definition allows a value to be nil or to conform
378
+ with the definition you pass it as argument:
379
+
380
+ ```ruby
347
381
  Definition.Nilable(Definition.Type(String)).conform(nil) # => pass
348
382
  Definition.Nilable(Definition.Type(String)).conform("foo") # => pass
349
383
  ```
350
384
 
385
+ The Enum Definition checks if the input equals one of the values you pass it as argument. You can pass in as many arguments as you like:
386
+
387
+ ```ruby
388
+ Definition.Enum("foo", 1, 2.0).conform("foo") # => pass
389
+ Definition.Enum("foo", 1, 2.0).conform(1) # => pass
390
+ Definition.Enum("foo", 1, 2.0).conform("bar) # => fail
391
+ ```
392
+
351
393
  ### Examples
352
394
 
353
395
  Check out the [integration specs](./spec/integration) for more usage examples.
@@ -381,5 +423,5 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
381
423
 
382
424
  Bug reports and pull requests are welcome on GitHub at https://github.com/Goltergaul/definition.
383
425
 
384
- [travis]: https://travis-ci.org/Goltergaul/definition
426
+ [circleci]: https://circleci.com/gh/Goltergaul/definition
385
427
  [rubygems]: https://rubygems.org/gems/definition
@@ -20,3 +20,4 @@ en:
20
20
  keys:
21
21
  has_extra_key: "Hash has unexpected key %{key}"
22
22
  has_missing_key: "Hash is missing key %{key}"
23
+ not_a_hash: "Is not a Hash"
data/definition.gemspec CHANGED
@@ -31,9 +31,10 @@ Gem::Specification.new do |spec|
31
31
  spec.add_development_dependency "guard"
32
32
  spec.add_development_dependency "guard-rspec"
33
33
  spec.add_development_dependency "pry"
34
- spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rake", "~> 13.0"
35
35
  spec.add_development_dependency "rspec", "~> 3.0"
36
36
  spec.add_development_dependency "rspec-its", "~> 1.2"
37
+ spec.add_development_dependency "rspec_junit_formatter"
37
38
  spec.add_development_dependency "rubocop", "0.66.0"
38
39
  spec.add_development_dependency "rubocop-rspec", "1.32.0"
39
40
  spec.add_development_dependency "rubocop_runner"
@@ -24,7 +24,7 @@ module Definition
24
24
  end
25
25
 
26
26
  def to_s
27
- "<Definition::ConformError \n\t desciption: \"#{message}\", \n\t json_pointer: \"#{json_pointer}\">"
27
+ "<Definition::ConformError \n\t message: \"#{message}\", \n\t json_pointer: \"#{json_pointer}\">"
28
28
  end
29
29
 
30
30
  alias inspect to_s
@@ -93,7 +93,7 @@ module Definition
93
93
  # Example:
94
94
  # Nilable(Definition.Type(Integer))
95
95
  def Nilable(definition) # rubocop:disable Style/MethodName
96
- Types::Or.new(:nullable, Nil(), definition)
96
+ Types::Or.new(:nilable, Nil(), definition)
97
97
  end
98
98
  end
99
99
  end
@@ -1,3 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "pathname"
4
+
3
5
  I18n.load_path += Dir[File.join(File.expand_path(Pathname(__dir__).join("../../config/locales/")), "*.yml")]
@@ -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
@@ -60,9 +84,15 @@ module Definition
60
84
  end
61
85
 
62
86
  def conform
63
- add_extra_key_errors unless definition.ignore_extra_keys
64
- add_missing_key_errors
65
- values = conform_all_keys
87
+ if valid_input_type?
88
+ add_extra_key_errors unless definition.ignore_extra_keys
89
+ add_missing_key_errors
90
+ values = conform_all_keys
91
+ else
92
+ errors.push(ConformError.new(definition,
93
+ "#{definition.name} is not a Hash",
94
+ i18n_key: "keys.not_a_hash"))
95
+ end
66
96
 
67
97
  ConformResult.new(values, errors: errors)
68
98
  end
@@ -71,6 +101,10 @@ module Definition
71
101
 
72
102
  attr_accessor :errors
73
103
 
104
+ def valid_input_type?
105
+ value.is_a?(Hash)
106
+ end
107
+
74
108
  def add_extra_key_errors
75
109
  extra_keys = value.keys - all_keys
76
110
  return if extra_keys.empty?
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Definition
4
- VERSION = "0.5.0"
4
+ VERSION = "0.6.1"
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.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dominik Goltermann
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-03-29 00:00:00.000000000 Z
11
+ date: 2021-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -142,14 +142,14 @@ dependencies:
142
142
  requirements:
143
143
  - - "~>"
144
144
  - !ruby/object:Gem::Version
145
- version: '10.0'
145
+ version: '13.0'
146
146
  type: :development
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
- version: '10.0'
152
+ version: '13.0'
153
153
  - !ruby/object:Gem::Dependency
154
154
  name: rspec
155
155
  requirement: !ruby/object:Gem::Requirement
@@ -178,6 +178,20 @@ dependencies:
178
178
  - - "~>"
179
179
  - !ruby/object:Gem::Version
180
180
  version: '1.2'
181
+ - !ruby/object:Gem::Dependency
182
+ name: rspec_junit_formatter
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
181
195
  - !ruby/object:Gem::Dependency
182
196
  name: rubocop
183
197
  requirement: !ruby/object:Gem::Requirement
@@ -234,7 +248,7 @@ dependencies:
234
248
  - - ">="
235
249
  - !ruby/object:Gem::Version
236
250
  version: '0'
237
- description:
251
+ description:
238
252
  email:
239
253
  - dominik@goltermann.cc
240
254
  executables: []
@@ -242,12 +256,13 @@ extensions: []
242
256
  extra_rdoc_files: []
243
257
  files:
244
258
  - ".approvals"
259
+ - ".circleci/config.yml"
245
260
  - ".gitignore"
246
261
  - ".rspec"
247
262
  - ".rubocop.yml"
248
- - ".travis.yml"
249
263
  - Changelog.md
250
264
  - Gemfile
265
+ - Gemfile.lock
251
266
  - Guardfile
252
267
  - LICENSE
253
268
  - README.md
@@ -283,7 +298,7 @@ homepage: https://github.com/Goltergaul/definition
283
298
  licenses:
284
299
  - MIT
285
300
  metadata: {}
286
- post_install_message:
301
+ post_install_message:
287
302
  rdoc_options: []
288
303
  require_paths:
289
304
  - lib
@@ -298,9 +313,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
298
313
  - !ruby/object:Gem::Version
299
314
  version: '0'
300
315
  requirements: []
301
- rubyforge_project:
302
- rubygems_version: 2.2.2
303
- signing_key:
316
+ rubygems_version: 3.1.2
317
+ signing_key:
304
318
  specification_version: 4
305
319
  summary: Simple and composable validation and coercion of data structures inspired
306
320
  by clojure specs
data/.travis.yml DELETED
@@ -1,21 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - jruby-9.1.17.0 # ruby 2.3
4
- - jruby-9.2.0.0 # ruby 2.5
5
- - jruby-head
6
- - 2.3.0
7
- - 2.4.0
8
- - 2.5.0
9
- - ruby-head
10
- jobs:
11
- include:
12
- - stage: linting
13
- rvm: ruby-head
14
- script: bundle exec rake rubocop
15
- - stage: benchmark
16
- script: bundle exec ruby benchmark/complex_example.rb
17
- rvm: ruby-head
18
- - script: bundle exec ruby benchmark/coercion.rb
19
- rvm: ruby-head
20
- - script: bundle exec ruby benchmark/validation_only.rb
21
- rvm: ruby-head