Class: JSONAPI::Processor
- Inherits:
-
Object
- Object
- JSONAPI::Processor
- Includes:
- Callbacks
- Defined in:
- lib/jsonapi/processor.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#operation_type ⇒ Object
readonly
Returns the value of attribute operation_type.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#resource_klass ⇒ Object
readonly
Returns the value of attribute resource_klass.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#result_options ⇒ Object
readonly
Returns the value of attribute result_options.
Instance Method Summary collapse
- #create_resource ⇒ Object
- #create_to_many_relationships ⇒ Object
- #find ⇒ Object
- #find_related_resource_set(resource, relationship_name, include_directives, options) ⇒ Object
- #find_resource_set(resource_klass, include_directives, options) ⇒ Object
-
#initialize(resource_klass, operation_type, params) ⇒ Processor
constructor
A new instance of Processor.
- #process ⇒ Object
- #remove_resource ⇒ Object
- #remove_to_many_relationships ⇒ Object
- #remove_to_one_relationship ⇒ Object
- #replace_fields ⇒ Object
- #replace_polymorphic_to_one_relationship ⇒ Object
- #replace_to_many_relationships ⇒ Object
- #replace_to_one_relationship ⇒ Object
- #show ⇒ Object
- #show_related_resource ⇒ Object
- #show_related_resources ⇒ Object
- #show_relationship ⇒ Object
- #top_level_meta_include_page_count ⇒ Object
- #top_level_meta_include_record_count ⇒ Object
Methods included from Callbacks
Constructor Details
#initialize(resource_klass, operation_type, params) ⇒ Processor
Returns a new instance of Processor.
22 23 24 25 26 27 28 29 |
# File 'lib/jsonapi/processor.rb', line 22 def initialize(resource_klass, operation_type, params) @resource_klass = resource_klass @operation_type = operation_type @params = params @context = params[:context] @result = nil @result_options = {} end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
20 21 22 |
# File 'lib/jsonapi/processor.rb', line 20 def context @context end |
#operation_type ⇒ Object (readonly)
Returns the value of attribute operation_type.
20 21 22 |
# File 'lib/jsonapi/processor.rb', line 20 def operation_type @operation_type end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
20 21 22 |
# File 'lib/jsonapi/processor.rb', line 20 def params @params end |
#resource_klass ⇒ Object (readonly)
Returns the value of attribute resource_klass.
20 21 22 |
# File 'lib/jsonapi/processor.rb', line 20 def resource_klass @resource_klass end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
20 21 22 |
# File 'lib/jsonapi/processor.rb', line 20 def result @result end |
#result_options ⇒ Object (readonly)
Returns the value of attribute result_options.
20 21 22 |
# File 'lib/jsonapi/processor.rb', line 20 def @result_options end |
Instance Method Details
#create_resource ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/jsonapi/processor.rb', line 229 def create_resource include_directives = params[:include_directives] fields = params[:fields] serializer = params[:serializer] data = params[:data] resource = resource_klass.create(context) result = resource.replace_fields(data) = { context: context, fields: fields, filters: { resource_klass._primary_key => resource.id }, include_directives: include_directives } resource_set = find_resource_set(resource_klass, include_directives, ) resource_set.populate!(serializer, context, ) return JSONAPI::ResourceSetOperationResult.new((result == :completed ? :created : :accepted), resource_set, ) end |
#create_to_many_relationships ⇒ Object
314 315 316 317 318 319 320 321 322 323 |
# File 'lib/jsonapi/processor.rb', line 314 def create_to_many_relationships resource_id = params[:resource_id] relationship_type = params[:relationship_type].to_sym data = params[:data] resource = resource_klass.find_by_key(resource_id, context: context) result = resource.create_to_many_links(relationship_type, data) return JSONAPI::OperationResult.new(result == :completed ? :no_content : :accepted, ) end |
#find ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/jsonapi/processor.rb', line 42 def find filters = params[:filters] include_directives = params[:include_directives] sort_criteria = params[:sort_criteria] paginator = params[:paginator] fields = params[:fields] serializer = params[:serializer] verified_filters = resource_klass.verify_filters(filters, context) = { context: context, sort_criteria: sort_criteria, paginator: paginator, fields: fields, filters: verified_filters, include_directives: include_directives } resource_set = find_resource_set(resource_klass, include_directives, ) resource_set.populate!(serializer, context, ) = if ( || (paginator && paginator.requires_record_count)) [:record_count] = resource_klass.count(verified_filters, context: context, include_directives: include_directives) end if ( && paginator && [:record_count]) [:page_count] = paginator ? paginator.calculate_page_count([:record_count]) : 1 end if JSONAPI.configuration.top_level_links_include_pagination && paginator [:pagination_params] = paginator.links_page_params(.merge(fetched_resources: resource_set)) end return JSONAPI::ResourcesSetOperationResult.new(:ok, resource_set, ) end |
#find_related_resource_set(resource, relationship_name, include_directives, options) ⇒ Object
377 378 379 380 381 382 383 |
# File 'lib/jsonapi/processor.rb', line 377 def (resource, relationship_name, include_directives, ) = include_directives.include_directives[:include_related] if include_directives resource_id_tree = find_resource_id_tree_from_resource_relationship(resource, relationship_name, , ) JSONAPI::ResourceSet.new(resource_id_tree) end |
#find_resource_set(resource_klass, include_directives, options) ⇒ Object
369 370 371 372 373 374 375 |
# File 'lib/jsonapi/processor.rb', line 369 def find_resource_set(resource_klass, include_directives, ) = include_directives.include_directives[:include_related] if include_directives resource_id_tree = find_resource_id_tree(resource_klass, , ) JSONAPI::ResourceSet.new(resource_id_tree) end |
#process ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/jsonapi/processor.rb', line 31 def process run_callbacks :operation do run_callbacks operation_type do @result = send(operation_type) end end rescue JSONAPI::Exceptions::Error => e @result = JSONAPI::ErrorsOperationResult.new(e.errors[0].code, e.errors) end |
#remove_resource ⇒ Object
254 255 256 257 258 259 260 261 |
# File 'lib/jsonapi/processor.rb', line 254 def remove_resource resource_id = params[:resource_id] resource = resource_klass.find_by_key(resource_id, context: context) result = resource.remove return JSONAPI::OperationResult.new(result == :completed ? :no_content : :accepted, ) end |
#remove_to_many_relationships ⇒ Object
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/jsonapi/processor.rb', line 336 def remove_to_many_relationships resource_id = params[:resource_id] relationship_type = params[:relationship_type].to_sym associated_keys = params[:associated_keys] resource = resource_klass.find_by_key(resource_id, context: context) complete = true associated_keys.each do |key| result = resource.remove_to_many_link(relationship_type, key) if complete && result != :completed complete = false end end return JSONAPI::OperationResult.new(complete ? :no_content : :accepted, ) end |
#remove_to_one_relationship ⇒ Object
353 354 355 356 357 358 359 360 361 |
# File 'lib/jsonapi/processor.rb', line 353 def remove_to_one_relationship resource_id = params[:resource_id] relationship_type = params[:relationship_type].to_sym resource = resource_klass.find_by_key(resource_id, context: context) result = resource.remove_to_one_link(relationship_type) return JSONAPI::OperationResult.new(result == :completed ? :no_content : :accepted, ) end |
#replace_fields ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/jsonapi/processor.rb', line 263 def replace_fields resource_id = params[:resource_id] include_directives = params[:include_directives] fields = params[:fields] serializer = params[:serializer] data = params[:data] resource = resource_klass.find_by_key(resource_id, context: context) result = resource.replace_fields(data) = { context: context, fields: fields, filters: { resource_klass._primary_key => resource.id }, include_directives: include_directives } resource_set = find_resource_set(resource_klass, include_directives, ) resource_set.populate!(serializer, context, ) return JSONAPI::ResourceSetOperationResult.new((result == :completed ? :ok : :accepted), resource_set, ) end |
#replace_polymorphic_to_one_relationship ⇒ Object
302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/jsonapi/processor.rb', line 302 def replace_polymorphic_to_one_relationship resource_id = params[:resource_id] relationship_type = params[:relationship_type].to_sym key_value = params[:key_value] key_type = params[:key_type] resource = resource_klass.find_by_key(resource_id, context: context) result = resource.replace_polymorphic_to_one_link(relationship_type, key_value, key_type) return JSONAPI::OperationResult.new(result == :completed ? :no_content : :accepted, ) end |
#replace_to_many_relationships ⇒ Object
325 326 327 328 329 330 331 332 333 334 |
# File 'lib/jsonapi/processor.rb', line 325 def replace_to_many_relationships resource_id = params[:resource_id] relationship_type = params[:relationship_type].to_sym data = params.fetch(:data) resource = resource_klass.find_by_key(resource_id, context: context) result = resource.replace_to_many_links(relationship_type, data) return JSONAPI::OperationResult.new(result == :completed ? :no_content : :accepted, ) end |
#replace_to_one_relationship ⇒ Object
291 292 293 294 295 296 297 298 299 300 |
# File 'lib/jsonapi/processor.rb', line 291 def replace_to_one_relationship resource_id = params[:resource_id] relationship_type = params[:relationship_type].to_sym key_value = params[:key_value] resource = resource_klass.find_by_key(resource_id, context: context) result = resource.replace_to_one_link(relationship_type, key_value) return JSONAPI::OperationResult.new(result == :completed ? :no_content : :accepted, ) end |
#show ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/jsonapi/processor.rb', line 85 def show include_directives = params[:include_directives] fields = params[:fields] id = params[:id] serializer = params[:serializer] key = resource_klass.verify_key(id, context) = { context: context, fields: fields, filters: { resource_klass._primary_key => key }, include_directives: include_directives } resource_set = find_resource_set(resource_klass, include_directives, ) fail JSONAPI::Exceptions::RecordNotFound.new(id) if resource_set.resource_klasses.empty? resource_set.populate!(serializer, context, ) return JSONAPI::ResourceSetOperationResult.new(:ok, resource_set, ) end |
#show_related_resource ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/jsonapi/processor.rb', line 141 def include_directives = params[:include_directives] source_klass = params[:source_klass] source_id = params[:source_id] relationship_type = params[:relationship_type] serializer = params[:serializer] fields = params[:fields] = { context: context, fields: fields, filters: {}, include_directives: include_directives } source_resource = source_klass.find_by_key(source_id, context: context, fields: fields) resource_set = (source_resource, relationship_type, include_directives, ) resource_set.populate!(serializer, context, ) return JSONAPI::ResourceSetOperationResult.new(:ok, resource_set, ) end |
#show_related_resources ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/jsonapi/processor.rb', line 168 def source_klass = params[:source_klass] source_id = params[:source_id] relationship_type = params[:relationship_type] filters = params[:filters] sort_criteria = params[:sort_criteria] paginator = params[:paginator] fields = params[:fields] include_directives = params[:include_directives] serializer = params[:serializer] verified_filters = resource_klass.verify_filters(filters, context) = { filters: verified_filters, sort_criteria: sort_criteria, paginator: paginator, fields: fields, context: context, include_directives: include_directives } source_resource = source_klass.find_by_key(source_id, context: context, fields: fields) resource_set = (source_resource, relationship_type, include_directives, ) resource_set.populate!(serializer, context, ) opts = if (() || (paginator && paginator.requires_record_count) || ()) opts[:record_count] = source_resource.class.( source_resource.identity, relationship_type, ) end if ( && opts[:record_count]) opts[:page_count] = paginator.calculate_page_count(opts[:record_count]) end opts[:pagination_params] = if paginator && JSONAPI.configuration.top_level_links_include_pagination = {} [:record_count] = opts[:record_count] if paginator.requires_record_count paginator.links_page_params(.merge(fetched_resources: resource_set)) else {} end return JSONAPI::RelatedResourcesSetOperationResult.new(:ok, source_resource, relationship_type, resource_set, opts) end |
#show_relationship ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/jsonapi/processor.rb', line 110 def show_relationship parent_key = params[:parent_key] relationship_type = params[:relationship_type].to_sym paginator = params[:paginator] sort_criteria = params[:sort_criteria] include_directives = params[:include_directives] fields = params[:fields] parent_resource = resource_klass.find_by_key(parent_key, context: context) = { context: context, sort_criteria: sort_criteria, paginator: paginator, fields: fields, include_directives: include_directives } resource_id_tree = (resource_klass, JSONAPI::ResourceIdentity.new(resource_klass, parent_key), relationship_type, , nil) return JSONAPI::RelationshipOperationResult.new(:ok, parent_resource, resource_klass._relationship(relationship_type), resource_id_tree.fragments.keys, ) end |
#top_level_meta_include_page_count ⇒ Object
389 390 391 |
# File 'lib/jsonapi/processor.rb', line 389 def JSONAPI.configuration. end |
#top_level_meta_include_record_count ⇒ Object
385 386 387 |
# File 'lib/jsonapi/processor.rb', line 385 def JSONAPI.configuration. end |