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_relationship ⇒ Object
- #find ⇒ Object
-
#initialize(resource_klass, operation_type, params) ⇒ Processor
constructor
A new instance of Processor.
- #process ⇒ Object
- #remove_resource ⇒ Object
- #remove_to_many_relationship ⇒ Object
- #remove_to_one_relationship ⇒ Object
- #replace_fields ⇒ Object
- #replace_polymorphic_to_one_relationship ⇒ Object
- #replace_to_many_relationship ⇒ 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
193 194 195 196 197 198 199 |
# File 'lib/jsonapi/processor.rb', line 193 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_relationship ⇒ Object
243 244 245 246 247 248 249 250 251 252 |
# File 'lib/jsonapi/processor.rb', line 243 def create_to_many_relationship 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 |
# 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) resource_records = resource_klass.find(verified_filters, context: context, include_directives: include_directives, sort_criteria: sort_criteria, paginator: paginator, fields: fields) = {} 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.calculate_page_count([:record_count]) end if JSONAPI.configuration.top_level_links_include_pagination && paginator [:pagination_params] = paginator.links_page_params() 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
201 202 203 204 205 206 207 208 |
# File 'lib/jsonapi/processor.rb', line 201 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_relationship ⇒ Object
265 266 267 268 269 270 271 272 273 274 |
# File 'lib/jsonapi/processor.rb', line 265 def remove_to_many_relationship resource_id = params[:resource_id] relationship_type = params[:relationship_type].to_sym associated_key = params[:associated_key] resource = resource_klass.find_by_key(resource_id, context: context) result = resource.remove_to_many_link(relationship_type, associated_key) return JSONAPI::OperationResult.new(result == :completed ? :no_content : :accepted) end |
#remove_to_one_relationship ⇒ Object
276 277 278 279 280 281 282 283 284 |
# File 'lib/jsonapi/processor.rb', line 276 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
210 211 212 213 214 215 216 217 218 |
# File 'lib/jsonapi/processor.rb', line 210 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
231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/jsonapi/processor.rb', line 231 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_relationship ⇒ Object
254 255 256 257 258 259 260 261 262 263 |
# File 'lib/jsonapi/processor.rb', line 254 def replace_to_many_relationship 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
220 221 222 223 224 225 226 227 228 229 |
# File 'lib/jsonapi/processor.rb', line 220 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
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/jsonapi/processor.rb', line 100 def show include_directives = params[:include_directives] fields = params[:fields] id = params[:id] key = resource_klass.verify_key(id, context) resource_record = resource_klass.find_by_key(key, context: context, include_directives: include_directives, fields: fields) return JSONAPI::ResourceOperationResult.new(:ok, resource_record) end |
#show_related_resource ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/jsonapi/processor.rb', line 126 def source_klass = params[:source_klass] source_id = params[:source_id] relationship_type = params[:relationship_type].to_sym fields = params[:fields] 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
139 140 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 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 |
# File 'lib/jsonapi/processor.rb', line 139 def source_klass = params[:source_klass] source_id = params[:source_id] relationship_type = params[:relationship_type] filters = params[:filters] include_directives = params[:include_directives] sort_criteria = params[:sort_criteria] paginator = params[:paginator] fields = params[:fields] source_resource ||= source_klass.find_by_key(source_id, context: context, fields: fields) = source_resource.public_send(relationship_type, context: context, filters: filters, include_directives: include_directives, sort_criteria: sort_criteria, paginator: paginator, fields: fields) if ((JSONAPI.configuration.) || (paginator && paginator.class.requires_record_count) || (JSONAPI.configuration.)) = source_resource.public_send("records_for_" + relationship_type) records = resource_klass.filter_records(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() 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
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/jsonapi/processor.rb', line 115 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 |