jsonapi-serializer-formats 0.0.2 → 0.0.3

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: dced1f014610ec4006f0a789a52738f6bb309ea554e95d3ff4c294248fafc17d
4
- data.tar.gz: 36b9ef0c909ca0cae7a81072c80588dc11c499687e6b4afcfcfa115baa26be95
3
+ metadata.gz: 5e468d2540d03110000fb78f815e77e4f504e7d658c26b69ebe702e336b04a7b
4
+ data.tar.gz: ad8a4b37c9299456f3377ccafef60f013af66cab63cacec4b186365160d01491
5
5
  SHA512:
6
- metadata.gz: 34e5176a471bb83e247ccc24a54a48b42aa26edf7d68358446663bcc82ee82bf088e0681975b08136f68f478ab80c2a42a5d3883020bcbaf14727db2a210b6db
7
- data.tar.gz: 1e546716fc865db5f08c4a569e2879f4687c8bb6f7b41b536f01098922db042238cab391e529da6b9d62a71629a896a11daf02cfbde211bc6d69cb815aef5c9f
6
+ metadata.gz: 3fb1e17e45b0e95532701c2cadde4e8f6265f2e085f416e3c0dbbf13a009dd088ed6b3e52de05c58cb8c5450328033f7657b07fddf145c918e2b0a7e56b96265
7
+ data.tar.gz: f9760c0f1e2dd4d96c3a3429d150e1e42159546c136d2421ee44a31cda5fb87e7afb7a065c4659887d286905fb440b03c6c637a9649fe34efeace4e62ab6758f
@@ -1,54 +1,76 @@
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
8
7
 
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)
8
+ def scoped_formats
9
+ @@scoped_formats ||= []
10
+ end
11
+
12
+ # --- Override the attribute and relationship methods to support contexts
13
+
14
+ [
15
+ :attributes,
16
+ :belongs_to,
17
+ :has_many,
18
+ :has_one
19
+ ].each do |method_name|
20
+
21
+ original_method_name = "jsonapi_#{method_name}".to_sym
22
+
23
+ alias_method original_method_name, method_name
24
+
25
+ define_method(method_name) do |*attributes_list, &block|
22
26
  formats = [*scoped_formats]
23
27
 
24
28
  if formats.length.positive?
29
+
30
+ # --- Read options hash (if present)
31
+
25
32
  opts = attributes_list.last
26
33
  unless opts.is_a?(Hash)
27
34
  opts = {}
28
35
  attributes_list << opts
29
36
  end
30
-
37
+
38
+ # --- Inject an :if condition
39
+
31
40
  cond = opts[:if]
32
41
  opts[:if] = Proc.new do |_, params = {}|
33
- next false if cond.present? && !cond.call(_, params)
42
+ if cond.present? && !cond.call(_, params)
43
+ next false # --- The user's condition failed
44
+ end
45
+
34
46
  render_formats = [params[:format]].compact.flatten
35
-
47
+
48
+ # --- Return true if the user passed the require formats as params
49
+
36
50
  formats.all? { |f| render_formats.include?(f) }
37
51
  end
38
52
  end
39
53
 
40
- jsonapi_attributes(*attributes_list, &block)
41
- end
42
-
43
- def format(fmt)
44
- scoped_formats.push(fmt)
45
- yield
46
- ensure
47
- scoped_formats.pop
54
+ # --- Call the original method
55
+
56
+ send(original_method_name, *attributes_list, &block)
48
57
  end
49
-
50
- alias_method :attribute, :attributes
51
58
  end
59
+
60
+ #
61
+ # Defines a named format with a block
62
+ #
63
+ # @param [Symbol] fmt the name of the format you wish to create
64
+ #
65
+ #
66
+ def format(fmt)
67
+ scoped_formats.push(fmt)
68
+ yield
69
+ ensure
70
+ scoped_formats.pop
71
+ end
72
+
73
+ alias_method :attribute, :attributes
52
74
  end
53
75
  end
54
76
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-serializer-formats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Rabier
@@ -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
@@ -166,5 +138,5 @@ requirements: []
166
138
  rubygems_version: 3.2.3
167
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: []