Class: LcpRuby::DataSource::Base

Inherits:
Object
  • Object
show all
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

CachedWrapper, Host, ResilientWrapper, RestJson

Instance Method Summary collapse

Instance Method Details

#count(params = {}) ⇒ Integer

Count records matching the given parameters.

Parameters:

  • params (Hash) (defaults to: {})

    filter parameters

Returns:

  • (Integer)


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

Raises:



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.

Parameters:

  • id (String, Integer)

    the record identifier

Returns:

  • (Object)

    the record

Raises:



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.

Parameters:

  • ids (Array<String, Integer>)

    the record identifiers

Returns:

  • (Array<Object>)

    the records (missing IDs omitted)



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

Raises:



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.

Parameters:

  • params (Hash) (defaults to: {})

    filter parameters

  • sort (Hash, nil) (defaults to: nil)

    e.g. { field: “name”, direction: “asc” }

  • page (Integer) (defaults to: 1)

    page number (1-based)

  • per (Integer) (defaults to: 25)

    records per page

Returns:

Raises:

  • (NotImplementedError)


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.

Parameters:

  • search (String, nil) (defaults to: nil)

    text search query

  • filter (Hash) (defaults to: {})

    filter criteria

  • sort (Hash, nil) (defaults to: nil)

    sort configuration

  • label_method (String) (defaults to: "to_label")

    method name for display label

  • limit (Integer) (defaults to: 200)

    max results

Returns:

  • (Array<Hash>)

    array of { id:, label: } hashes



55
56
57
58
59
60
61
62
63
# File 'lib/lcp_ruby/data_source/base.rb', line 55

def select_options(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_operatorsObject

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

Returns:

  • (Boolean)


74
75
76
# File 'lib/lcp_ruby/data_source/base.rb', line 74

def writable?
  false
end