1
1
require 'jsonapi/deserializable'
2
- require 'jsonapi/parser'
3
2
4
3
module JSONAPI
5
4
module Hanami
@@ -9,18 +8,20 @@ def self.included(base)
9
8
end
10
9
11
10
module ClassMethods
12
- def deserializable_resource ( key , klass = nil , &block )
13
- if klass . nil?
14
- klass = Class . new ( JSONAPI ::Deserializable ::Resource , &block )
15
- end
16
- use DeserializeResource , key , klass
11
+ def deserializable_resource ( key , options = { } , &block )
12
+ _deserializable ( key , options ,
13
+ JSONAPI ::Deserializable ::Resource , &block )
17
14
end
18
15
19
- def deserializable_relationship ( key , klass = nil , &block )
20
- if klass . nil?
21
- klass = Class . new ( JSONAPI ::Deserializable ::Relationship , &block )
22
- end
23
- use DeserializeRelationship , key , klass
16
+ def deserializable_relationship ( key , options = { } , &block )
17
+ _deserializable ( key , options ,
18
+ JSONAPI ::Deserializable ::Relationship , &block )
19
+ end
20
+
21
+ # @api private
22
+ def _deserializable ( key , options , fallback , &block )
23
+ klass = options [ :class ] || Class . new ( fallback , &block )
24
+ use DeserializationMiddleware , key , klass
24
25
end
25
26
end
26
27
@@ -37,7 +38,6 @@ def initialize(app, key, klass)
37
38
38
39
def call ( env )
39
40
body = env [ ROUTER_PARSED_BODY ]
40
- parser . parse! ( body )
41
41
deserialized_hash = @deserializable_class . call ( body )
42
42
params = env [ ROUTER_PARAMS ]
43
43
# TODO(beauby): Actually replace the request body upstream instead
@@ -51,18 +51,6 @@ def call(env)
51
51
@app . call ( env )
52
52
end
53
53
end
54
-
55
- class DeserializeResource < DeserializationMiddleware
56
- def parser
57
- JSONAPI ::Parser ::Resource
58
- end
59
- end
60
-
61
- class DeserializeRelationship < DeserializationMiddleware
62
- def parser
63
- JSONAPI ::Parser ::Relationship
64
- end
65
- end
66
54
end
67
55
end
68
56
end
0 commit comments