Class: Kabk::Adapters::SequelAdapter
- Defined in:
- lib/kabk/adapters/sequel_adapter.rb
Overview
Sequel adapter for Kabk resources
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#create(attributes, context: {}) ⇒ Hash
Protocol response hash with success, message, data.
-
#delete(id, context: {}) ⇒ Hash
Protocol response hash indicating success.
-
#get(id, context: {}) ⇒ Hash
Protocol response hash with success, data.
-
#list(params, context: {}) ⇒ Hash
Protocol response hash with success, data, meta.
-
#update(id, attributes, context: {}) ⇒ Hash
Protocol response hash with success, message, data.
Methods inherited from Base
Constructor Details
This class inherits a constructor from Kabk::Adapters::Base
Instance Method Details
#create(attributes, context: {}) ⇒ Hash
Returns Protocol response hash with success, message, data.
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/kabk/adapters/sequel_adapter.rb', line 66 def create(attributes, context: {}) record = @resource.model_class.create(attributes) hydrated_records = RelationHydrator.hydrate(@resource, [record]) { success: true, message: "Operation completed successfully", data: hydrated_records.first } rescue Sequel::UniqueConstraintViolation raise ApiError.new("Unique constraint violated", code: "CONFLICT", http_status: 409) end |
#delete(id, context: {}) ⇒ Hash
Returns Protocol response hash indicating success.
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/kabk/adapters/sequel_adapter.rb', line 112 def delete(id, context: {}) record = @resource.model_class[id] raise NotFoundError, "Record not found" unless record record.destroy { success: true, message: "Record successfully deleted" } end |
#get(id, context: {}) ⇒ Hash
Returns Protocol response hash with success, data.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/kabk/adapters/sequel_adapter.rb', line 50 def get(id, context: {}) record = @resource.model_class[id] raise NotFoundError, "Record not found" unless record hydrated_records = RelationHydrator.hydrate(@resource, [record]) { success: true, data: hydrated_records.first } end |
#list(params, context: {}) ⇒ Hash
Returns Protocol response hash with success, data, meta.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/kabk/adapters/sequel_adapter.rb', line 15 def list(params, context: {}) dataset = QueryBuilder.build(@resource, params) if dataset.respond_to?(:pagination_record_count) total = dataset.pagination_record_count page = dataset.current_page per_page = dataset.page_size last_page = dataset.page_count records = dataset.all else records = dataset.all total = records.size page = 1 per_page = total last_page = 1 end hydrated_records = RelationHydrator.hydrate(@resource, records) { success: true, data: hydrated_records, meta: { total: total, page: page, per_page: per_page, last_page: last_page } } end |
#update(id, attributes, context: {}) ⇒ Hash
Returns Protocol response hash with success, message, data.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/kabk/adapters/sequel_adapter.rb', line 86 def update(id, attributes, context: {}) record = @resource.model_class[id] raise NotFoundError, "Record not found" unless record Concurrency.check!(@resource, record, attributes) c_field = @resource.concurrency_field&.to_sym attributes.delete(c_field) if c_field record.update(attributes) hydrated_records = RelationHydrator.hydrate(@resource, [record]) { success: true, message: "Operation completed successfully", data: hydrated_records.first } rescue Sequel::UniqueConstraintViolation raise ApiError.new("Unique constraint violated", code: "CONFLICT", http_status: 409) end |