jsonapi-serializable 0.3.0 → 0.3.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: e06e5be75e2c53c3f257807493bb7c896a669241
4
- data.tar.gz: b0c29ac61252006bc763a1e08567037cff3f9e49
2
+ SHA256:
3
+ metadata.gz: feba6e17728c898c35d2ec3983c5a7b635d8e1911680fae0805b7c6942cc3dc3
4
+ data.tar.gz: 4f1dcf29ad0f95e5b89046655f71749eadbabb99fa64be0a88b497c869412fd1
5
5
  SHA512:
6
- metadata.gz: a3d7759a8a216e92a71e56f1324082a69b6f51ce69653136c39187b527a37dafd2dd17919c70b1389b48412938098af9d6f83fdfaa0167be257e09cb18b6c8fd
7
- data.tar.gz: 333ba881f8e1799bd2db5a0ed8c92b86f5906530550f0a0e03de63d451d41d66b0674198e91c572d312aee21c9f02d6048480fddc615451373828f06f62b504f
6
+ metadata.gz: 5f55e477c151d2db755ee4fe33f856c7610c57e7f9abbc3dfbdf889f02b60ae02d8d172fa662fbc1c0c1cbda6f5f4f03452170f751537133e3840cfb65ae2c09
7
+ data.tar.gz: bbc988fd88b92b4063871d69b5bfa076e3a302a7e0f2410826dd0f039cd5fc77756dee57998a71545afe4936f9acca463b5e3c78ee004a0072afb4bb5cc1386d
@@ -1,3 +1,35 @@
1
- require 'jsonapi/serializable/error'
2
- require 'jsonapi/serializable/resource'
3
- require 'jsonapi/serializable/renderer'
1
+ module JSONAPI
2
+ module Serializable
3
+ require 'jsonapi/serializable/error'
4
+ require 'jsonapi/serializable/resource'
5
+ require 'jsonapi/serializable/renderer'
6
+
7
+ # Error raised when there's no serializable class defined for resource.
8
+ class UndefinedSerializableClass < StandardError; end
9
+
10
+ # @api private
11
+ def self.resources_for(objects, options, inferrer)
12
+ return if objects.nil?
13
+
14
+ if objects.respond_to?(:to_ary)
15
+ Array(objects).map do |object|
16
+ resource_for(object, options, inferrer)
17
+ end
18
+ else
19
+ resource_for(objects, options, inferrer)
20
+ end
21
+ end
22
+
23
+ # @api private
24
+ def self.resource_for(object, options, inferrer)
25
+ class_name = object.class.name.to_sym
26
+
27
+ serializable_klass = inferrer[class_name] || (
28
+ raise UndefinedSerializableClass,
29
+ "No serializable class defined for #{class_name}"
30
+ )
31
+
32
+ serializable_klass.new(options.merge(object: object))
33
+ end
34
+ end
35
+ end
@@ -14,18 +14,7 @@ module JSONAPI
14
14
  # NOTE(beauby): Lazify computation since it is only needed when
15
15
  # the corresponding relationship is included.
16
16
  @_resources_block = proc do
17
- resources = yield
18
- if resources.nil?
19
- nil
20
- elsif resources.respond_to?(:to_ary)
21
- Array(resources).map do |obj|
22
- @_class[obj.class.name.to_sym]
23
- .new(@_exposures.merge(object: obj))
24
- end
25
- else
26
- @_class[resources.class.name.to_sym]
27
- .new(@_exposures.merge(object: resources))
28
- end
17
+ JSONAPI::Serializable.resources_for(yield, @_exposures, @_class)
29
18
  end
30
19
  end
31
20
 
@@ -41,7 +41,8 @@ module JSONAPI
41
41
  exposures = options.delete(:expose) || {}
42
42
  exposures = exposures.merge(_class: klass)
43
43
 
44
- resources = build_resources(resources, exposures, klass)
44
+ resources =
45
+ JSONAPI::Serializable.resources_for(resources, exposures, klass)
45
46
 
46
47
  @renderer.render(options.merge(data: resources))
47
48
  end
@@ -65,28 +66,11 @@ module JSONAPI
65
66
  klass = options.delete(:class) || {}
66
67
  exposures = options.delete(:expose) || {}
67
68
 
68
- errors = errors.map { |e| _build(e, exposures, klass) }
69
+ errors =
70
+ JSONAPI::Serializable.resources_for(errors, exposures, klass)
69
71
 
70
72
  @renderer.render(options.merge(errors: errors))
71
73
  end
72
-
73
- private
74
-
75
- # @api private
76
- def build_resources(resources, exposures, klass)
77
- if resources.nil?
78
- nil
79
- elsif resources.respond_to?(:to_ary)
80
- Array(resources).map { |obj| _build(obj, exposures, klass) }
81
- else
82
- _build(resources, exposures, klass)
83
- end
84
- end
85
-
86
- # @api private
87
- def _build(object, exposures, klass)
88
- klass[object.class.name.to_sym].new(exposures.merge(object: object))
89
- end
90
74
  end
91
75
  end
92
76
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-serializable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lucas Hosseini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-11 00:00:00.000000000 Z
11
+ date: 2019-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jsonapi-renderer
@@ -117,8 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
117
  - !ruby/object:Gem::Version
118
118
  version: '0'
119
119
  requirements: []
120
- rubyforge_project:
121
- rubygems_version: 2.6.13
120
+ rubygems_version: 3.0.2
122
121
  signing_key:
123
122
  specification_version: 4
124
123
  summary: Conveniently serialize JSON API resources.