Class: Kabk::RestEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/kabk/rest_engine.rb

Overview

Generic CRUD engine for any registered resource, agnostic to the underlying database

Instance Method Summary collapse

Constructor Details

#initialize(resource_name) ⇒ RestEngine

Returns a new instance of RestEngine.

Parameters:

  • resource_name (String, Symbol)

Raises:



9
10
11
12
13
14
15
# File 'lib/kabk/rest_engine.rb', line 9

def initialize(resource_name)
  @resource = Registry.instance.get(resource_name)
  raise NotFoundError, "Resource not found" unless @resource
  
  @adapter = @resource.adapter
  raise StandardError, "Resource has no adapter configured" unless @adapter
end

Instance Method Details

#create(params, context: {}) ⇒ Hash

Create a new resource. Executes lifecycle hooks, validates parameters, and delegates to the adapter.

Parameters:

  • params (Hash)

    The raw input attributes from the request payload.

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

    Context provided by host framework.

Returns:

  • (Hash)

    A standard protocol response hash or an error hash if a hook/validation fails.



41
42
43
44
45
46
47
48
49
50
# File 'lib/kabk/rest_engine.rb', line 41

def create(params, context: {})
  @resource.hooks[:before_create]&.call(params, context)
  sanitized = Validator.validate_and_sanitize!(@resource, params)
  result = @adapter.create(sanitized, context: context)
  
  @resource.hooks[:after_create]&.call(result[:data], context)
  result
rescue ApiError => e
  e.to_h
end

#delete(id, context: {}) ⇒ Hash

Delete a resource by ID. Executes lifecycle hooks and delegates to the adapter.

Parameters:

  • id (Integer, String)

    The primary key of the record.

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

    Context provided by host framework.

Returns:

  • (Hash)

    A standard protocol response hash indicating success or an error hash if a hook fails.



74
75
76
77
78
79
80
81
82
# File 'lib/kabk/rest_engine.rb', line 74

def delete(id, context: {})
  @resource.hooks[:before_delete]&.call(id, context)
  result = @adapter.delete(id, context: context)
  
  @resource.hooks[:after_delete]&.call(id, context)
  result
rescue ApiError => e
  e.to_h
end

#get(id, context: {}) ⇒ Hash

Get single resource by ID.

Parameters:

  • id (Integer, String)

    The primary key of the record.

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

    Context provided by host framework.

Returns:

  • (Hash)

    A standard protocol response hash with success and data properties.

Raises:



32
33
34
# File 'lib/kabk/rest_engine.rb', line 32

def get(id, context: {})
  @adapter.get(id, context: context)
end

#list(params, context: {}) ⇒ Hash

List resources with pagination, sorting, filtering.

Parameters:

  • params (Hash)

    Request parameters (page, per_page, sort, filters).

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

    Context provided by host framework.

Returns:

  • (Hash)

    A standard protocol response hash with success, data, and meta properties.



22
23
24
# File 'lib/kabk/rest_engine.rb', line 22

def list(params, context: {})
  @adapter.list(params, context: context)
end

#update(id, params, context: {}) ⇒ Hash

Update an existing resource. Executes lifecycle hooks and validates Optimistic Concurrency Control (OCC).

Parameters:

  • id (Integer, String)

    The primary key of the record.

  • params (Hash)

    The updated attributes from the request payload.

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

    Context provided by host framework.

Returns:

  • (Hash)

    A standard protocol response hash or an error hash if a hook/validation fails.



58
59
60
61
62
63
64
65
66
67
# File 'lib/kabk/rest_engine.rb', line 58

def update(id, params, context: {})
  @resource.hooks[:before_update]&.call(id, params, context)
  sanitized = Validator.validate_and_sanitize!(@resource, params, is_update: true)
  result = @adapter.update(id, sanitized, context: context)
  
  @resource.hooks[:after_update]&.call(result[:data], context)
  result
rescue ApiError => e
  e.to_h
end