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.
Class Method Summary collapse
- ._processor_from_resource_type(resource_klass) ⇒ Object
- .processor_instance_for(resource_klass, operation_type, params) ⇒ Object
- .transactional_operation_type?(operation_type) ⇒ Boolean
Instance Method Summary collapse
- #create_resource ⇒ Object
- #create_to_many_relationships ⇒ Object
- #find ⇒ 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
Methods included from Callbacks
Constructor Details
#initialize(resource_klass, operation_type, params) ⇒ Processor
Returns a new instance of Processor.
46 47 48 49 50 51 52 53 |
# File 'lib/jsonapi/processor.rb', line 46 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.
44 45 46 |
# File 'lib/jsonapi/processor.rb', line 44 def context @context end |
#operation_type ⇒ Object (readonly)
Returns the value of attribute operation_type.
44 45 46 |
# File 'lib/jsonapi/processor.rb', line 44 def operation_type @operation_type end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
44 45 46 |
# File 'lib/jsonapi/processor.rb', line 44 def params @params end |
#resource_klass ⇒ Object (readonly)
Returns the value of attribute resource_klass.
44 45 46 |
# File 'lib/jsonapi/processor.rb', line 44 def resource_klass @resource_klass end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
44 45 46 |
# File 'lib/jsonapi/processor.rb', line 44 def result @result end |
#result_options ⇒ Object (readonly)
Returns the value of attribute result_options.
44 45 46 |
# File 'lib/jsonapi/processor.rb', line 44 def @result_options end |
Class Method Details
._processor_from_resource_type(resource_klass) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/jsonapi/processor.rb', line 25 def _processor_from_resource_type(resource_klass) processor = resource_klass.name.gsub(/Resource$/,'Processor').safe_constantize if processor.nil? processor = JSONAPI.configuration.default_processor_klass end return processor end |
.processor_instance_for(resource_klass, operation_type, params) ⇒ Object
21 22 23 |
# File 'lib/jsonapi/processor.rb', line 21 def processor_instance_for(resource_klass, operation_type, params) _processor_from_resource_type(resource_klass).new(resource_klass, operation_type, params) end |
.transactional_operation_type?(operation_type) ⇒ Boolean
34 35 36 37 38 39 40 41 |
# File 'lib/jsonapi/processor.rb', line 34 def transactional_operation_type?(operation_type) case operation_type when :find, :show, :show_related_resource, :show_related_resources return false else return true end end |
Instance Method Details
#create_resource ⇒ Object
230 231 232 233 234 235 236 |
# File 'lib/jsonapi/processor.rb', line 230 def create_resource data = params[:data] resource = resource_klass.create(context) result = resource.replace_fields(data) return JSONAPI::ResourceOperationResult.new((result == :completed ? :created : :accepted), resource) end |
#create_to_many_relationships ⇒ Object
280 281 282 283 284 285 286 287 288 289 |
# File 'lib/jsonapi/processor.rb', line 280 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
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/jsonapi/processor.rb', line 66 def find filters = params[:filters] include_directives = params[:include_directives] sort_criteria = params.fetch(:sort_criteria, []) paginator = params[:paginator] fields = params[:fields] verified_filters = resource_klass.verify_filters(filters, context) = { context: context, include_directives: include_directives, sort_criteria: sort_criteria, paginator: paginator, fields: fields } resource_records = if params[:cache_serializer] resource_klass.find_serialized_with_caching(verified_filters, params[:cache_serializer], ) else resource_klass.find(verified_filters, ) end = {} if (JSONAPI.configuration. || (paginator && paginator.class.requires_record_count)) [:record_count] = resource_klass.find_count(verified_filters, context: context, include_directives: include_directives) end if (JSONAPI.configuration. && [: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_records)) end return JSONAPI::ResourcesOperationResult.new(:ok, resource_records, ) end |
#process ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/jsonapi/processor.rb', line 55 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
238 239 240 241 242 243 244 245 |
# File 'lib/jsonapi/processor.rb', line 238 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
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/jsonapi/processor.rb', line 302 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
319 320 321 322 323 324 325 326 327 |
# File 'lib/jsonapi/processor.rb', line 319 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
247 248 249 250 251 252 253 254 255 |
# File 'lib/jsonapi/processor.rb', line 247 def replace_fields resource_id = params[:resource_id] data = params[:data] resource = resource_klass.find_by_key(resource_id, context: context) result = resource.replace_fields(data) return JSONAPI::ResourceOperationResult.new(result == :completed ? :ok : :accepted, resource) end |
#replace_polymorphic_to_one_relationship ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/jsonapi/processor.rb', line 268 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
291 292 293 294 295 296 297 298 299 300 |
# File 'lib/jsonapi/processor.rb', line 291 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
257 258 259 260 261 262 263 264 265 266 |
# File 'lib/jsonapi/processor.rb', line 257 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
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/jsonapi/processor.rb', line 109 def show include_directives = params[:include_directives] fields = params[:fields] id = params[:id] key = resource_klass.verify_key(id, context) = { context: context, include_directives: include_directives, fields: fields } resource_record = if params[:cache_serializer] resource_klass.find_by_key_serialized_with_caching(key, params[:cache_serializer], ) else resource_klass.find_by_key(key, ) end return JSONAPI::ResourceOperationResult.new(:ok, resource_record) end |
#show_related_resource ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/jsonapi/processor.rb', line 144 def source_klass = params[:source_klass] source_id = params[:source_id] relationship_type = params[:relationship_type].to_sym fields = params[:fields] # TODO Should fetch related_resource from cache if caching enabled source_resource = source_klass.find_by_key(source_id, context: context, fields: fields) = source_resource.public_send(relationship_type) return JSONAPI::ResourceOperationResult.new(:ok, ) end |
#show_related_resources ⇒ Object
158 159 160 161 162 163 164 165 166 167 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 228 |
# File 'lib/jsonapi/processor.rb', line 158 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] source_resource ||= source_klass.find_by_key(source_id, context: context, fields: fields) verified_filters = resource_klass.verify_filters(filters, context) rel_opts = { filters: verified_filters, sort_criteria: sort_criteria, paginator: paginator, fields: fields, context: context, include_directives: include_directives } = nil if params[:cache_serializer] # TODO Could also avoid instantiating source_resource as actual Resource by # allowing LinkBuilder to accept CachedResourceFragment as source in # relationships_related_link scope = source_resource.public_send(:"records_for_#{relationship_type}", rel_opts) relationship = source_klass._relationship(relationship_type) = relationship.resource_klass.find_serialized_with_caching( scope, params[:cache_serializer], rel_opts ) else = source_resource.public_send(relationship_type, rel_opts) end if ((JSONAPI.configuration.) || (paginator && paginator.class.requires_record_count) || (JSONAPI.configuration.)) = source_resource.public_send("records_for_" + relationship_type) records = resource_klass.filter_records(verified_filters, {}, ) record_count = resource_klass.count_records(records) end if (JSONAPI.configuration. && record_count) page_count = paginator.calculate_page_count(record_count) end pagination_params = if paginator && JSONAPI.configuration.top_level_links_include_pagination = {} [:record_count] = record_count if paginator.class.requires_record_count paginator.links_page_params(.merge(fetched_resources: )) else {} end opts = {} opts.merge!(pagination_params: pagination_params) if JSONAPI.configuration.top_level_links_include_pagination opts.merge!(record_count: record_count) if JSONAPI.configuration. opts.merge!(page_count: page_count) if JSONAPI.configuration. return JSONAPI::RelatedResourcesOperationResult.new(:ok, source_resource, relationship_type, , opts) end |
#show_relationship ⇒ Object
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/jsonapi/processor.rb', line 133 def show_relationship parent_key = params[:parent_key] relationship_type = params[:relationship_type].to_sym parent_resource = resource_klass.find_by_key(parent_key, context: context) return JSONAPI::LinksObjectOperationResult.new(:ok, parent_resource, resource_klass._relationship(relationship_type)) end |