Class: LcpRuby::DataSource::Base
- Inherits:
-
Object
- Object
- LcpRuby::DataSource::Base
- Defined in:
- lib/lcp_ruby/data_source/base.rb
Overview
Abstract base class defining the data source contract. All data source adapters (RestJson, Host, etc.) must implement this interface.
Direct Known Subclasses
Instance Method Summary collapse
-
#count(params = {}) ⇒ Integer
Count records matching the given parameters.
- #destroy(_id) ⇒ Object
-
#find(id) ⇒ Object
Find a single record by ID.
-
#find_many(ids) ⇒ Array<Object>
Find multiple records by IDs.
-
#save(_record) ⇒ Object
Write operations — Phase 1 is read-only.
-
#search(params = {}, sort: nil, page: 1, per: 25) ⇒ SearchResult
Search for records with filtering, sorting, and pagination.
-
#select_options(search: nil, filter: {}, sort: nil, label_method: "to_label", limit: 200) ⇒ Array<Hash>
Fetch options for association select dropdowns.
-
#supported_operators ⇒ Object
Returns the set of filter operators this data source supports.
- #writable? ⇒ Boolean
Instance Method Details
#count(params = {}) ⇒ Integer
Count records matching the given parameters.
43 44 45 46 |
# File 'lib/lcp_ruby/data_source/base.rb', line 43 def count(params = {}) result = search(params, page: 1, per: 1) result.total_count end |
#destroy(_id) ⇒ Object
70 71 72 |
# File 'lib/lcp_ruby/data_source/base.rb', line 70 def destroy(_id) raise ReadonlyError, "#{self.class} is read-only (Phase 1)" end |
#find(id) ⇒ Object
Find a single record by ID.
14 15 16 |
# File 'lib/lcp_ruby/data_source/base.rb', line 14 def find(id) raise NotImplementedError, "#{self.class}#find must be implemented" end |
#find_many(ids) ⇒ Array<Object>
Find multiple records by IDs. Default implementation calls find sequentially.
22 23 24 25 26 27 28 |
# File 'lib/lcp_ruby/data_source/base.rb', line 22 def find_many(ids) ids.filter_map do |id| find(id) rescue RecordNotFound nil end end |
#save(_record) ⇒ Object
Write operations — Phase 1 is read-only
66 67 68 |
# File 'lib/lcp_ruby/data_source/base.rb', line 66 def save(_record) raise ReadonlyError, "#{self.class} is read-only (Phase 1)" end |
#search(params = {}, sort: nil, page: 1, per: 25) ⇒ SearchResult
Search for records with filtering, sorting, and pagination.
36 37 38 |
# File 'lib/lcp_ruby/data_source/base.rb', line 36 def search(params = {}, sort: nil, page: 1, per: 25) raise NotImplementedError, "#{self.class}#search must be implemented" end |
#select_options(search: nil, filter: {}, sort: nil, label_method: "to_label", limit: 200) ⇒ Array<Hash>
Fetch options for association select dropdowns.
55 56 57 58 59 60 61 62 63 |
# File 'lib/lcp_ruby/data_source/base.rb', line 55 def (search: nil, filter: {}, sort: nil, label_method: "to_label", limit: 200) result = self.search(filter, sort: sort, page: 1, per: limit) result.map do |record| { id: record.id, label: record.respond_to?(label_method) ? record.send(label_method).to_s : record.to_s } end end |
#supported_operators ⇒ Object
Returns the set of filter operators this data source supports. Subclasses can override to restrict operators.
80 81 82 |
# File 'lib/lcp_ruby/data_source/base.rb', line 80 def supported_operators %w[eq not_eq cont lt lteq gt gteq in null not_null start end] end |
#writable? ⇒ Boolean
74 75 76 |
# File 'lib/lcp_ruby/data_source/base.rb', line 74 def writable? false end |