jsonapi-materializer 1.1.0 → 1.2.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: a2f61a86bda571eb041aa543a16338d80c82b742708f90be0c321e45f27eb97e
4
- data.tar.gz: fb22fbb7a3998c2ca6563c85a65b12980fae019feb96972523850c8074160485
3
+ metadata.gz: b51f422840742a6b80b48de2c10d75b2530a630d13e0caecb0c598f79089081e
4
+ data.tar.gz: ffbf502f4377b1814cb6ba7c8f7d5b3cd01d792ccc4fa047ccd1e2c448f83f19
5
5
  SHA512:
6
- metadata.gz: 1a27fc6d325e69296ae5ce9e73a21caf13194eba084b71f127a81574c63fe839cba0be3701b2be43f812efa7e9d22428f20891d46703eb30fe7d2cba73d2f3b8
7
- data.tar.gz: 7c661026d63ac185e4f7d01db31b83547945d3903c14775273dbe06b05455a57209286efc5237c764b26853c3a8bfe733c4064aa3b081e1fd38e287a675a36c3
6
+ metadata.gz: 6bb8dc1d59f884f503c4e6f993c331a69d41b7e7619f34fa2296f5cb0597e82cf128d55f27b15f2523e6d7ea1c603ed4cb8abb87c65da3d78dd2197484e4348e
7
+ data.tar.gz: 7689dec65ec8f3a81751c054345ae19e51d5ef1dd64245ce7bccc6226b373f168f69eeb1c51254f635a69d89e5ae70ea176eb708d98f7e234ef013a507c57756
@@ -22,38 +22,16 @@ module JSONAPI
22
22
 
23
23
  def as_json(*)
24
24
  {
25
- links: if pagination
26
- {
27
- first: (links_pagination.expand(offset: 1, limit: limit_value).to_s unless total_pages.zero? || first_page?),
28
- prev: (links_pagination.expand(offset: prev_page, limit: limit_value).to_s unless total_pages.zero? || first_page?),
29
- self: (links_self unless total_pages.zero?),
30
- next: (links_pagination.expand(offset: next_page, limit: limit_value).to_s unless total_pages.zero? || last_page?),
31
- last: (links_pagination.expand(offset: total_pages, limit: limit_value).to_s unless total_pages.zero? || last_page?)
32
- }.compact
33
- else
34
- {}
35
- end,
36
- data: resources,
37
- included:
38
- }.transform_values(&:presence).compact
25
+ links: pagination,
26
+ included:,
27
+ meta:
28
+ }.transform_values(&:presence).compact.merge(data: resources)
39
29
  end
40
30
 
41
31
  private def materializers
42
32
  @materializers ||= object.map { |subobject| self.class.module_parent.new(object: subobject, selects:, includes:) }
43
33
  end
44
34
 
45
- private def links_pagination
46
- Addressable::Template.new(
47
- "#{origin}/#{type}?page[offset]={offset}&page[limit]={limit}"
48
- )
49
- end
50
-
51
- private def links_self
52
- Addressable::Template.new(
53
- "#{origin}/#{type}"
54
- ).pattern
55
- end
56
-
57
35
  private def origin
58
36
  self.class.module_parent.instance_variable_get(:@origin)
59
37
  end
@@ -74,6 +52,36 @@ module JSONAPI
74
52
  @includes || []
75
53
  end
76
54
 
55
+ private def pagination
56
+ if @pagination
57
+ {
58
+ first: (pagination_link_template.expand(offset: 1, limit: limit_value).to_s unless total_pages.zero? || first_page?),
59
+ prev: (pagination_link_template.expand(offset: prev_page, limit: limit_value).to_s unless total_pages.zero? || first_page?),
60
+ self: (self_link_template unless total_pages.zero?),
61
+ next: (pagination_link_template.expand(offset: next_page, limit: limit_value).to_s unless total_pages.zero? || last_page?),
62
+ last: (pagination_link_template.expand(offset: total_pages, limit: limit_value).to_s unless total_pages.zero? || last_page?)
63
+ }.compact
64
+ else
65
+ {}
66
+ end
67
+ end
68
+
69
+ private def pagination_link_template
70
+ Addressable::Template.new(
71
+ "#{origin}/#{type}?page[offset]={offset}&page[limit]={limit}"
72
+ )
73
+ end
74
+
75
+ private def self_link_template
76
+ Addressable::Template.new(
77
+ "#{origin}/#{type}"
78
+ ).pattern
79
+ end
80
+
81
+ private def meta
82
+ {}
83
+ end
84
+
77
85
  private def included
78
86
  @included ||= materializers.flat_map do |materializer|
79
87
  includes.flat_map do |path|
@@ -24,8 +24,7 @@ module JSONAPI
24
24
  links: {
25
25
  self: links_self,
26
26
  related: links_related
27
- },
28
- meta: {}
27
+ }
29
28
  }.transform_values(&:presence).compact
30
29
  end
31
30
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module JSONAPI
4
4
  module Materializer
5
- VERSION = "1.1.0"
5
+ VERSION = "1.2.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-materializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kurtis Rainbolt-Greene
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-09-08 00:00:00.000000000 Z
11
+ date: 2023-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport