jsonapi-serializer-formats 0.0.5 → 0.0.6

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: 1084dc152896ad4da2aedbc71a966548553ede8f5d6897112407c917da528bc8
4
- data.tar.gz: 239766aba4009f0b05697f34aa7b5e847197a0cc0e600f3b80a145701bb9fffa
3
+ metadata.gz: 46af0de9ebc9db137bf79ec1782170647c0644178a7680278a5427b6d5ea4e31
4
+ data.tar.gz: 8a1698007a3790c49e9952778b40fffab279fb45b141cfad8daa3f422441bdab
5
5
  SHA512:
6
- metadata.gz: 3a66408aadc65d5487572eae3d7d0e104f2cb743bfcc904f507acdfade2b4241dd82ff42441f7cbf469dfb6fc5f9bdffabc96fbce84521dddcda936cb3b034b0
7
- data.tar.gz: 2292765309f012cb9d4a36c61af3b8d4c4162ad5a5aaf2b188aa9bf92115b776aee45e2e2f4642d125ba0a80bf2b551ac6ccd5d3f09fd97a639d6ecb3376d859
6
+ metadata.gz: 5f4dd9574fb1eb90413e69bb044437a8a564f3ca4e4cc5167ee9285e50dd3c09c81516f08e67f6ada4494bbc7e55b02167543e30a88c1366cc5d74a8a8168dcd
7
+ data.tar.gz: bdaeb23994576e785d90e09b0e38b24f8fa1982b4d2fe34157834bdba267cb8872a04747e0ef260c9badb715192e52a5aa16470c1c4e906bcd21a21b1d40e77b
@@ -6,7 +6,11 @@ module JSONAPI
6
6
  class << self
7
7
 
8
8
  def scoped_formats
9
- @@scoped_formats ||= []
9
+ @scoped_formats ||= []
10
+ end
11
+
12
+ def formats_per_attr
13
+ @formats_per_attr ||= {}
10
14
  end
11
15
 
12
16
  # --- Override the attribute and relationship methods to support contexts
@@ -23,23 +27,30 @@ module JSONAPI
23
27
  alias_method original_method_name, method_name
24
28
 
25
29
  define_method(method_name) do |*attributes_list, &block|
26
- formats = [*scoped_formats]
27
-
28
- if formats.length.positive?
30
+
31
+ # --- If we're not in a format blocked, pass through
32
+
33
+ return send(original_method_name, *attributes_list, &block) unless scoped_formats.length.positive?
29
34
 
30
- # --- Read options hash (if present)
35
+ # --- Read options hash (if present)
31
36
 
32
- opts = attributes_list.last
33
- unless opts.is_a?(Hash)
34
- opts = {}
35
- attributes_list << opts
36
- end
37
+ opts = attributes_list.last
38
+ unless opts.is_a?(Hash)
39
+ opts = {}
40
+ attributes_list << opts
41
+ end
42
+
43
+ user_condition = opts[:if] || Proc.new { true }
44
+
45
+ attributes_list[0..-2].each do |field|
46
+
47
+ (formats_per_attr[field] ||= []) << [*scoped_formats]
37
48
 
38
49
  # --- Inject an :if condition
39
50
 
40
- cond = opts[:if]
41
- opts[:if] = Proc.new do |_, params = {}|
42
- if cond.present? && !cond.call(_, params)
51
+ field_opts = opts.dup
52
+ field_opts[:if] = Proc.new do |_, params = {}|
53
+ if !user_condition.call(_, params)
43
54
  next false # --- The user's condition failed
44
55
  end
45
56
 
@@ -47,13 +58,13 @@ module JSONAPI
47
58
 
48
59
  # --- Return true if the user passed the require formats as params
49
60
 
50
- formats.all? { |f| render_formats.include?(f) }
61
+ formats_per_attr[field].any? do |formats|
62
+ formats.all? { |f| render_formats.include?(f) }
63
+ end
51
64
  end
52
- end
53
-
54
- # --- Call the original method
55
65
 
56
- send(original_method_name, *attributes_list, &block)
66
+ send(original_method_name, *[field, field_opts], &block)
67
+ end
57
68
  end
58
69
  end
59
70
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-serializer-formats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Rabier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-20 00:00:00.000000000 Z
11
+ date: 2021-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jsonapi-serializer