Module: JSONAPI::Realizer::Resource
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActiveModel::Model
- Defined in:
- lib/jsonapi/realizer/resource.rb,
lib/jsonapi/realizer/resource/relation.rb,
lib/jsonapi/realizer/resource/attribute.rb,
lib/jsonapi/realizer/resource/configuration.rb
Defined Under Namespace
Classes: Attribute, Configuration, Relation
Instance Attribute Summary collapse
Instance Method Summary
collapse
Instance Attribute Details
#context ⇒ Object
209
210
211
|
# File 'lib/jsonapi/realizer/resource.rb', line 209
def context
self.class.const_get(:Context).new(**@context || {})
end
|
Returns the value of attribute headers.
41
42
43
|
# File 'lib/jsonapi/realizer/resource.rb', line 41
def
@headers
end
|
#intent ⇒ Object
205
206
207
|
# File 'lib/jsonapi/realizer/resource.rb', line 205
def intent
@intent.to_sym
end
|
#parameters ⇒ Object
Returns the value of attribute parameters.
40
41
42
|
# File 'lib/jsonapi/realizer/resource.rb', line 40
def parameters
@parameters
end
|
#scope=(value) ⇒ Object
43
44
45
|
# File 'lib/jsonapi/realizer/resource.rb', line 43
def scope=(value)
@scope = value
end
|
Instance Method Details
#attributes ⇒ Object
179
180
181
182
183
184
|
# File 'lib/jsonapi/realizer/resource.rb', line 179
def attributes
@attributes ||= data
.fetch("attributes")
.transform_keys(&:underscore)
.transform_keys { |key| attribute(key).as }
end
|
#filtering? ⇒ Boolean
115
116
117
|
# File 'lib/jsonapi/realizer/resource.rb', line 115
def filtering?
parameters.key?("filter") && parameters.fetch("filter").respond_to?(:transform_keys)
end
|
#filters ⇒ Object
119
120
121
122
123
124
125
126
|
# File 'lib/jsonapi/realizer/resource.rb', line 119
def filters
@filters ||= parameters.
fetch("filter").
transform_keys(&:underscore)
end
|
#include? ⇒ Boolean
128
129
130
|
# File 'lib/jsonapi/realizer/resource.rb', line 128
def include?
parameters.key?("include") && parameters.fetch("include").respond_to?(:split) && parameters.fetch("include").split(/\s*,\s*/).any?
end
|
#includes ⇒ Object
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/jsonapi/realizer/resource.rb', line 132
def includes
@includes ||= parameters.
fetch("include").
split(/\s*,\s*/).
map { |chain| chain.split(".") }.
map { |list| list.map(&:underscore) }.
map do |relationship_chain|
relationship_chain.reduce([[], self.class]) do |(normalized_relationship_chain, realizer_class), relationship_link|
[
[
*normalized_relationship_chain,
realizer_class.relation(relationship_link).as
],
realizer_class.relation(relationship_link).realizer_class
]
end.first
end
rescue Error::ResourceRelationshipNotFound => _relationship_not_found
raise(Error::ResourceRelationshipNotFound, name:, materializer: self, key: "include")
end
|
#initialize(**keyword_arguments) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/jsonapi/realizer/resource.rb', line 45
def initialize(**keyword_arguments)
super(**keyword_arguments)
context.validate!
validate!
@scope = adapter.filtering(scope, filters) if filtering?
@scope = adapter.include_relationships(scope, includes) if include?
@scope = adapter.sorting(scope, sorts) if sorting?
@scope = adapter.paginate(scope, *) if paginate?
adapter.write_attributes(object, attributes) if writing? && data? && attributes?
return unless writing? && data? && relationships?
adapter.write_relationships(object, relationships)
end
|
#object ⇒ Object
194
195
196
197
198
199
200
201
202
203
|
# File 'lib/jsonapi/realizer/resource.rb', line 194
def object
@object ||= case intent
when :create
scope.new
when :show, :update, :destroy
adapter.find_one(scope, parameters.fetch("id"))
else
scope
end
end
|
#paginate? ⇒ Boolean
75
76
77
|
# File 'lib/jsonapi/realizer/resource.rb', line 75
def paginate?
parameters.key?("page") && parameters.fetch("page").respond_to?(:fetch) && (paginate_limit? || paginate_offset?)
end
|
87
88
89
90
91
92
|
# File 'lib/jsonapi/realizer/resource.rb', line 87
def
[
parameters.fetch("page").fetch("limit", nil),
parameters.fetch("page").fetch("offset", nil)
]
end
|
#relationships ⇒ Object
186
187
188
189
190
191
192
|
# File 'lib/jsonapi/realizer/resource.rb', line 186
def relationships
@relationships ||= data
.fetch("relationships")
.transform_keys(&:underscore)
.map(&method(:as_relationship)).to_h
.transform_keys { |key| relation(key).as }
end
|
#selects ⇒ Object
166
167
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/jsonapi/realizer/resource.rb', line 166
def selects
@selects ||= parameters.
fetch("fields").
transform_keys(&:underscore).
transform_values { |value| value.split(/\s*,\s*/) }.
transform_values { |value| value.map(&:underscore) }
end
|
#selects? ⇒ Boolean
162
163
164
|
# File 'lib/jsonapi/realizer/resource.rb', line 162
def selects?
parameters.key?("fields") && parameters.fetch("fields").respond_to?(:transform_keys)
end
|
#sorting? ⇒ Boolean
94
95
96
|
# File 'lib/jsonapi/realizer/resource.rb', line 94
def sorting?
parameters.key?("sort") && parameters.fetch("sort").respond_to?(:split)
end
|
#sorts ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/jsonapi/realizer/resource.rb', line 98
def sorts
@sorts ||= parameters.
fetch("sort").
split(",").
map do |token|
token.start_with?("-") ? [token.sub(/^-/, "").underscore, "-"] : [token.underscore, "+"]
end.
map do |(path, direction)|
[path.include?(".") ? path.split(".") : [self.class.configuration.type, path], direction]
end
end
|
#to_hash ⇒ Object
66
67
68
69
70
71
72
73
|
# File 'lib/jsonapi/realizer/resource.rb', line 66
def to_hash
@to_hash ||= {
pagination: ( if paginate?),
selects: (selects if selects?),
includes: (includes if include?),
object:
}.compact
end
|