Class: Kabk::ResourceBuilder
- Inherits:
-
Object
- Object
- Kabk::ResourceBuilder
- Defined in:
- lib/kabk/resource_builder.rb
Overview
DSL builder for resources
Instance Method Summary collapse
-
#after_create {|record, context| ... } ⇒ Object
Registers a callback executed after the record is created.
-
#after_delete {|id, context| ... } ⇒ Object
Registers a callback executed after the record is deleted.
-
#after_update {|record, context| ... } ⇒ Object
Registers a callback executed after the record is updated.
-
#before_create {|params, context| ... } ⇒ Object
Registers a callback executed before the record is created.
-
#before_delete {|id, context| ... } ⇒ Object
Registers a callback executed before the record is deleted.
-
#before_update {|id, params, context| ... } ⇒ Object
Registers a callback executed before the record is updated.
-
#field(name, type:, form_type:, **attributes) ⇒ Object
Define a field with attributes.
-
#initialize(resource) ⇒ ResourceBuilder
constructor
A new instance of ResourceBuilder.
-
#method_missing(method_name, *args, **kwargs) ⇒ Object
Delegate dynamic attribute setting (e.g. title, icon, plural_name).
-
#permissions(**perms) ⇒ Object
Configure permissions explicitly.
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Constructor Details
#initialize(resource) ⇒ ResourceBuilder
Returns a new instance of ResourceBuilder.
10 11 12 |
# File 'lib/kabk/resource_builder.rb', line 10 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)
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/kabk/resource_builder.rb', line 15 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.
65 66 67 |
# File 'lib/kabk/resource_builder.rb', line 65 def after_create(&block) @resource.hooks[:after_create] = block end |
#after_delete {|id, context| ... } ⇒ Object
Registers a callback executed after the record is deleted.
89 90 91 |
# File 'lib/kabk/resource_builder.rb', line 89 def after_delete(&block) @resource.hooks[:after_delete] = block end |
#after_update {|record, context| ... } ⇒ Object
Registers a callback executed after the record is updated.
77 78 79 |
# File 'lib/kabk/resource_builder.rb', line 77 def after_update(&block) @resource.hooks[:after_update] = block end |
#before_create {|params, context| ... } ⇒ Object
Registers a callback executed before the record is created.
59 60 61 |
# File 'lib/kabk/resource_builder.rb', line 59 def before_create(&block) @resource.hooks[:before_create] = block end |
#before_delete {|id, context| ... } ⇒ Object
Registers a callback executed before the record is deleted.
83 84 85 |
# File 'lib/kabk/resource_builder.rb', line 83 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.
71 72 73 |
# File 'lib/kabk/resource_builder.rb', line 71 def before_update(&block) @resource.hooks[:before_update] = block end |
#field(name, type:, form_type:, **attributes) ⇒ Object
Define a field with attributes
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/kabk/resource_builder.rb', line 33 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
51 52 53 |
# File 'lib/kabk/resource_builder.rb', line 51 def (**perms) @resource..merge!(perms) end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
28 29 30 |
# File 'lib/kabk/resource_builder.rb', line 28 def respond_to_missing?(method_name, include_private = false) @resource.respond_to?("#{method_name}=") || super end |