jsonapi-serializer-formats 0.0.1 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jsonapi-serializer-formats.rb +75 -42
  3. metadata +10 -38
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7f917e258df3aad22f72d01a67138242f42c30ea2d7560b08292a83c8da211c0
4
- data.tar.gz: 84269917b784f0989a195b62cdcd3a48fb77e8b72ee4b183e9096c45873d7f65
3
+ metadata.gz: 46af0de9ebc9db137bf79ec1782170647c0644178a7680278a5427b6d5ea4e31
4
+ data.tar.gz: 8a1698007a3790c49e9952778b40fffab279fb45b141cfad8daa3f422441bdab
5
5
  SHA512:
6
- metadata.gz: 03e2d49eaf7b158960c031a1b859f2704850e795340182844e9c0f72e158288553d8a159c0469ff1049f8aee1bde56976f42cfbba5f47bf433a96ccf371db9c7
7
- data.tar.gz: be240fa11c1bf11221c6ee711c461b57cc547dc9d6c29ec58dee640cd7c70c34eb889e3ea17d9ae94885b0ee2c513dacd142eb2aece205fd9d4a2591312f3b07
6
+ metadata.gz: 5f4dd9574fb1eb90413e69bb044437a8a564f3ca4e4cc5167ee9285e50dd3c09c81516f08e67f6ada4494bbc7e55b02167543e30a88c1366cc5d74a8a8168dcd
7
+ data.tar.gz: bdaeb23994576e785d90e09b0e38b24f8fa1982b4d2fe34157834bdba267cb8872a04747e0ef260c9badb715192e52a5aa16470c1c4e906bcd21a21b1d40e77b
@@ -1,54 +1,87 @@
1
1
  module JSONAPI
2
2
  module Formats
3
- def hello
4
- "hi"
5
- end
3
+ extend ActiveSupport::Concern
6
4
 
7
- module_function :hello
5
+ included do
6
+ class << self
7
+
8
+ def scoped_formats
9
+ @scoped_formats ||= []
10
+ end
11
+
12
+ def formats_per_attr
13
+ @formats_per_attr ||= {}
14
+ end
15
+
16
+ # --- Override the attribute and relationship methods to support contexts
17
+
18
+ [
19
+ :attributes,
20
+ :belongs_to,
21
+ :has_many,
22
+ :has_one
23
+ ].each do |method_name|
24
+
25
+ original_method_name = "jsonapi_#{method_name}".to_sym
26
+
27
+ alias_method original_method_name, method_name
28
+
29
+ define_method(method_name) do |*attributes_list, &block|
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?
34
+
35
+ # --- Read options hash (if present)
36
+
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]
48
+
49
+ # --- Inject an :if condition
50
+
51
+ field_opts = opts.dup
52
+ field_opts[:if] = Proc.new do |_, params = {}|
53
+ if !user_condition.call(_, params)
54
+ next false # --- The user's condition failed
55
+ end
8
56
 
9
- def self.included(base)
10
- base.class_eval do
11
- class << self
12
- def scoped_formats
13
- @@scoped_formats ||= []
14
- end
15
-
16
- alias_method :jsonapi_attributes, :attributes
17
-
18
- #
19
- # Override the attribute method to support contexts
20
- #
21
- def attributes(*attributes_list, &block)
22
- formats = [*scoped_formats]
23
-
24
- if formats.length.positive?
25
- opts = attributes_list.last
26
- unless opts.is_a?(Hash)
27
- opts = {}
28
- attributes_list << opts
29
- end
30
-
31
- cond = opts[:if]
32
- opts[:if] = Proc.new do |_, params = {}|
33
- next false if cond.present? && !cond.call(_, params)
34
57
  render_formats = [params[:format]].compact.flatten
35
-
36
- formats.all? { |f| render_formats.include?(f) }
58
+
59
+ # --- Return true if the user passed the require formats as params
60
+
61
+ formats_per_attr[field].any? do |formats|
62
+ formats.all? { |f| render_formats.include?(f) }
63
+ end
37
64
  end
65
+
66
+ send(original_method_name, *[field, field_opts], &block)
38
67
  end
39
-
40
- jsonapi_attributes(*attributes_list, &block)
41
68
  end
42
-
43
- def format(fmt)
44
- scoped_formats.push(fmt)
45
- yield
46
- ensure
47
- scoped_formats.pop
48
- end
49
-
50
- alias_method :attribute, :attributes
51
69
  end
70
+
71
+ #
72
+ # Defines a named format with a block
73
+ #
74
+ # @param [Symbol] fmt the name of the format you wish to create
75
+ #
76
+ #
77
+ def format(fmt)
78
+ scoped_formats.push(fmt)
79
+ yield
80
+ ensure
81
+ scoped_formats.pop
82
+ end
83
+
84
+ alias_method :attribute, :attributes
52
85
  end
53
86
  end
54
87
  end
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.1
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Rabier
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-19 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
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 2.1.0
19
+ version: '2.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 2.1.0
26
+ version: '2.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,20 +38,6 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '4.2'
41
- - !ruby/object:Gem::Dependency
42
- name: activerecord
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: bundler
57
43
  requirement: !ruby/object:Gem::Requirement
@@ -122,20 +108,6 @@ dependencies:
122
108
  - - ">="
123
109
  - !ruby/object:Gem::Version
124
110
  version: '0'
125
- - !ruby/object:Gem::Dependency
126
- name: sqlite3
127
- requirement: !ruby/object:Gem::Requirement
128
- requirements:
129
- - - ">="
130
- - !ruby/object:Gem::Version
131
- version: '0'
132
- type: :development
133
- prerelease: false
134
- version_requirements: !ruby/object:Gem::Requirement
135
- requirements:
136
- - - ">="
137
- - !ruby/object:Gem::Version
138
- version: '0'
139
111
  description: " A module to enrich JSON:API Serializers (https://github.com/jsonapi-serializer)
140
112
  with configurable formats\n"
141
113
  email: patrick@tronica.io
@@ -144,11 +116,11 @@ extensions: []
144
116
  extra_rdoc_files: []
145
117
  files:
146
118
  - lib/jsonapi-serializer-formats.rb
147
- homepage: https://rubygems.org/gems/jsonapi-serializer-formats
119
+ homepage: https://github.com/patrixr/jsonapi-serializer-formats
148
120
  licenses:
149
121
  - MIT
150
122
  metadata: {}
151
- post_install_message:
123
+ post_install_message:
152
124
  rdoc_options: []
153
125
  require_paths:
154
126
  - lib
@@ -163,8 +135,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
135
  - !ruby/object:Gem::Version
164
136
  version: '0'
165
137
  requirements: []
166
- rubygems_version: 3.2.3
167
- signing_key:
138
+ rubygems_version: 3.1.4
139
+ signing_key:
168
140
  specification_version: 4
169
- summary: Adds support for formats to serializers
141
+ summary: Adds 'formats' to JSON::API Serializers
170
142
  test_files: []