media_types 0.3.0 → 0.4.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: c5674ca1175e8811375a3d065219c87cb8268e86
4
- data.tar.gz: d945dc4c4326960d717be6f14350856b5bf596b2
3
+ metadata.gz: c410b6e7c2eaa9df298c4fef6306704921b75e1b
4
+ data.tar.gz: 9b21dfaf70f6d82c365a0309f5631565b5045ce9
5
5
  SHA512:
6
- metadata.gz: 95cab60688688522ed99735dfd030f133ef0a49ffe18cbaffad9dcde6f596c56a12446dadb9973803f77147b70ecf56e81a84ccb05d0260e0ac677edff1f78b1
7
- data.tar.gz: 219f62fb2a2c249326ae71e37dc7098db3b70b7e6f7af81509abf2afc0d5dd8782e7b7b7d360912b6553df86c9e2cb2777b6ee0759285e770865fc12dc13f95b
6
+ metadata.gz: da2ad8aa866bc5bc0aacceec506f9c0bd8c460fe5681f5beb80c6bbbfb9fa7795910889a6d4cbe9118122c05c9b5e9ffbec969a1708da10b965419e3093c0ffa
7
+ data.tar.gz: 2206345f8cca161de2cec0acde03b359cb5fd28fe1b2fa64fddbb14273c51a9e8bb806c1c6ff16daea9c33664ce82dde255b502c899d7efeead26a03a3b1a3a2
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ # 0.4.0
2
+
3
+ - Simplify `assert_media_type_format` by dumping views completely
4
+ - Add test for `merge`
5
+ - Add test for nested blocks in `validations` using `view` and `version` nesting
6
+ - Add test for `validatable?`
7
+ - Add block passing to `Scheme.new`
8
+
1
9
  # 0.3.0
2
10
 
3
11
  - Add `merge` dsl to merge two `Scheme`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- media_types (0.3.0)
4
+ media_types (0.4.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -77,13 +77,16 @@ module MediaTypes
77
77
 
78
78
  def to_str(qualifier = nil)
79
79
  # TODO: remove warning by slicing out these arguments if they don't appear in the format
80
- qualified(qualifier, @to_str ||= format(
81
- opts.fetch(:format),
82
- version: opts.fetch(:version),
83
- suffix: opts.fetch(:suffix) { :json },
84
- type: opts.fetch(:type),
85
- view: format_view(opts[:view])
86
- ))
80
+ qualified(
81
+ qualifier,
82
+ format(
83
+ opts.fetch(:format),
84
+ version: opts.fetch(:version),
85
+ suffix: opts.fetch(:suffix) { :json },
86
+ type: opts.fetch(:type),
87
+ view: format_view(opts[:view])
88
+ )
89
+ )
87
90
  end
88
91
 
89
92
  def valid?(output, **validation_opts)
@@ -102,6 +105,10 @@ module MediaTypes
102
105
  )
103
106
  end
104
107
 
108
+ def validatable?
109
+ __getobj__.validatable?(self)
110
+ end
111
+
105
112
  alias inspect to_str
106
113
  alias to_s to_str
107
114
 
@@ -116,7 +123,7 @@ module MediaTypes
116
123
  attr_accessor :opts
117
124
 
118
125
  def with(more_opts)
119
- Hash(opts).merge(more_opts).dup
126
+ Hash(opts).clone.merge(more_opts)
120
127
  end
121
128
 
122
129
  def qualified(qualifier, media_type)
@@ -21,7 +21,9 @@ module MediaTypes
21
21
  module ClassMethods
22
22
 
23
23
  def to_constructable
24
- media_type_constructable.dup
24
+ media_type_constructable.dup.tap do |constructable|
25
+ constructable.__setobj__(self)
26
+ end
25
27
  end
26
28
 
27
29
  def valid?(output, media_type = to_constructable, **opts)
@@ -32,6 +34,10 @@ module MediaTypes
32
34
  validations.find(String(media_type)).validate(output, backtrace: ['.'], **opts)
33
35
  end
34
36
 
37
+ def validatable?(media_type = to_constructable)
38
+ validations.find(String(media_type), -> { nil })
39
+ end
40
+
35
41
  def register
36
42
  registrations.to_a.map do |registerable|
37
43
  MediaTypes.register(registerable)
@@ -42,23 +48,25 @@ module MediaTypes
42
48
  private
43
49
 
44
50
  def media_type(name, defaults: {})
45
- self.media_type_constructable =
46
- Constructable.new(self, format: base_format, type: name)
47
- .version(defaults.fetch(:version) { nil })
48
- .suffix(defaults.fetch(:suffix) { nil })
49
- .view(defaults.fetch(:view) { nil })
51
+ self.media_type_constructable = Constructable.new(self, format: base_format, type: name)
52
+ .version(defaults.fetch(:version) { nil })
53
+ .suffix(defaults.fetch(:suffix) { nil })
54
+ .view(defaults.fetch(:view) { nil })
50
55
  end
51
56
 
52
57
  def defaults(&block)
58
+ return media_type_constructable unless block_given?
53
59
  self.media_type_constructable = Defaults.new(to_constructable, &block).to_constructable
54
60
  end
55
61
 
56
62
  def registrations(symbol = nil, &block)
57
- self.media_type_registrar = media_type_registrar || Registrar.new(self, symbol: symbol, &block)
63
+ return media_type_registrar unless block_given?
64
+ self.media_type_registrar = Registrar.new(self, symbol: symbol, &block)
58
65
  end
59
66
 
60
67
  def validations(&block)
61
- self.media_type_validations = media_type_validations || Validations.new(to_constructable, &block)
68
+ return media_type_validations unless block_given?
69
+ self.media_type_validations = Validations.new(to_constructable, &block)
62
70
  end
63
71
  end
64
72
  end
@@ -3,17 +3,7 @@
3
3
  module MediaTypes
4
4
  module Assertions
5
5
  def assert_media_type_format(media_type, output, **opts)
6
- if media_type.collection?
7
- output[:_embedded].each do |embedded|
8
- assert_media_type_format(media_type.view(nil), embedded, **opts)
9
- end
10
- return
11
- end
12
-
13
- if media_type.index?
14
- return output[:_index] # TODO: sub_schema the "self" link
15
- end
16
-
6
+ return pass unless media_type.validatable?
17
7
  assert media_type.validate!(output, **opts)
18
8
  end
19
9
  end
@@ -58,12 +58,14 @@ module MediaTypes
58
58
  #
59
59
  # @see MissingValidation
60
60
  #
61
- def initialize(allow_empty: false, force: nil)
61
+ def initialize(allow_empty: false, force: nil, &block)
62
62
  self.validations = {}
63
63
  self.allow_empty = allow_empty
64
64
  self.force = force
65
65
 
66
66
  validations.default = MissingValidation.new
67
+
68
+ instance_exec(&block) if block_given?
67
69
  end
68
70
 
69
71
  ##
@@ -213,10 +215,7 @@ module MediaTypes
213
215
  return validations.default = scheme
214
216
  end
215
217
 
216
- scheme = Scheme.new(allow_empty: allow_empty, force: force)
217
- scheme.instance_exec(&block)
218
-
219
- validations.default = scheme
218
+ validations.default = Scheme.new(allow_empty: allow_empty, force: force, &block)
220
219
  end
221
220
 
222
221
  ##
@@ -225,7 +224,7 @@ module MediaTypes
225
224
  # @param [Scheme] scheme the scheme to merge into this
226
225
  #
227
226
  def merge(scheme, &block)
228
- self.validations = validations.merge(scheme.send(:validations).dup)
227
+ self.validations = validations.merge(scheme.send(:validations).clone)
229
228
  instance_exec(&block) if block_given?
230
229
  end
231
230
 
@@ -307,10 +306,7 @@ module MediaTypes
307
306
  )
308
307
  end
309
308
 
310
- scheme = Scheme.new(allow_empty: allow_empty, force: force)
311
- scheme.instance_exec(&block)
312
-
313
- validations[String(key)] = scheme
309
+ validations[String(key)] = Scheme.new(allow_empty: allow_empty, force: force, &block)
314
310
  end
315
311
 
316
312
  ##
@@ -419,7 +415,7 @@ module MediaTypes
419
415
  return yield(->(_) {})
420
416
  end
421
417
 
422
- exhaustive_keys = keys.dup.map(&:to_s)
418
+ exhaustive_keys = keys.clone.map(&:to_s)
423
419
  # noinspection RubyScope
424
420
  result = yield ->(key) { exhaustive_keys.delete(String(key)) }
425
421
  return result if exhaustive_keys.empty?
@@ -38,8 +38,10 @@ module MediaTypes
38
38
  # @param [lambda] default the lambda if nothing can be found
39
39
  # @return [Scheme] the scheme for the given +media_type+
40
40
  #
41
- def find(media_type, default = -> { Scheme::NotStrict.new })
42
- registry.fetch(String(media_type)) { default.call }
41
+ def find(media_type, default = -> { Scheme.new(allow_empty: true) { not_strict } })
42
+ registry.fetch(String(media_type)) do
43
+ default.call
44
+ end
43
45
  end
44
46
 
45
47
  def method_missing(method_name, *arguments, &block)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MediaTypes
4
- VERSION = '0.3.0'
4
+ VERSION = '0.4.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: media_types
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derk-Jan Karrenbeld
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-10-01 00:00:00.000000000 Z
11
+ date: 2018-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: awesome_print