Class: Kabk::ResourceBuilder

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

Overview

DSL builder for configuring resources and their fields

Instance Method Summary collapse

Constructor Details

#initialize(resource) ⇒ ResourceBuilder

Returns a new instance of ResourceBuilder.



12
13
14
# File 'lib/kabk/resource_builder.rb', line 12

def initialize(resource)
  @resource = resource
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, **kwargs) ⇒ Object

Delegate dynamic attribute setting (e.g. title, icon, plural_name)



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/kabk/resource_builder.rb', line 17

def method_missing(method_name, *args, **kwargs, &)
  if @resource.respond_to?("#{method_name}=")
    value = if kwargs.any? && args.empty?
              kwargs
            else
              args.first
            end
    @resource.send("#{method_name}=", value)
  else
    super
  end
end

Instance Method Details

#after_create {|record, context| ... } ⇒ Object

Registers a callback executed after the record is created.

Yields:

  • (record, context)

    The newly created record hash and host context.



67
68
69
# File 'lib/kabk/resource_builder.rb', line 67

def after_create(&block)
  @resource.hooks[:after_create] = block
end

#after_delete {|id, context| ... } ⇒ Object

Registers a callback executed after the record is deleted.

Yields:

  • (id, context)

    The deleted record ID and host context.



91
92
93
# File 'lib/kabk/resource_builder.rb', line 91

def after_delete(&block)
  @resource.hooks[:after_delete] = block
end

#after_update {|record, context| ... } ⇒ Object

Registers a callback executed after the record is updated.

Yields:

  • (record, context)

    The updated record hash and host context.



79
80
81
# File 'lib/kabk/resource_builder.rb', line 79

def after_update(&block)
  @resource.hooks[:after_update] = block
end

#before_create {|params, context| ... } ⇒ Object

Registers a callback executed before the record is created.

Yields:

  • (params, context)

    The incoming parameters and host context.



61
62
63
# File 'lib/kabk/resource_builder.rb', line 61

def before_create(&block)
  @resource.hooks[:before_create] = block
end

#before_delete {|id, context| ... } ⇒ Object

Registers a callback executed before the record is deleted.

Yields:

  • (id, context)

    The record ID and host context.



85
86
87
# File 'lib/kabk/resource_builder.rb', line 85

def before_delete(&block)
  @resource.hooks[:before_delete] = block
end

#before_update {|id, params, context| ... } ⇒ Object

Registers a callback executed before the record is updated.

Yields:

  • (id, params, context)

    The record ID, incoming parameters, and host context.



73
74
75
# File 'lib/kabk/resource_builder.rb', line 73

def before_update(&block)
  @resource.hooks[:before_update] = block
end

#field(name, type:, form_type:, **attributes) ⇒ Object

Define a field with attributes

Raises:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/kabk/resource_builder.rb', line 35

def field(name, type:, form_type:, **attributes)
  # Validate required keys per spec
  raise InvalidFieldError, "Field '#{name}' is missing 'type'" unless type
  raise InvalidFieldError, "Field '#{name}' is missing 'form_type'" unless form_type

  # Extract label, if not provided, default to titleized name
  label = attributes.delete(:label)
  if label.nil?
    label = attributes.slice(:fa, :en)
    label = name.to_s.capitalize.tr("_", " ") if label.empty?
    attributes.reject! { |k, _v| %i[fa en].include?(k) }
  end

  f = Field.new(name: name, type: type, form_type: form_type, label: label, **attributes)
  @resource.fields << f
end

#permissions(**perms) ⇒ Object

Configure permissions explicitly



53
54
55
# File 'lib/kabk/resource_builder.rb', line 53

def permissions(**perms)
  @resource.permissions.merge!(perms)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/kabk/resource_builder.rb', line 30

def respond_to_missing?(method_name, include_private = false)
  @resource.respond_to?("#{method_name}=") || super
end