Class: RubyUIAdmin::ExecutionContext

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_ui_admin/execution_context.rb

Overview

Evaluates a value that may be a literal or a Proc. When it's a Proc, it's instance_exec'd with all the passed keyword arguments available as readers.

ExecutionContext.new(target: -> { record.name }, record: post).handle

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ ExecutionContext

Returns a new instance of ExecutionContext.



9
10
11
12
13
14
15
16
17
18
# File 'lib/ruby_ui_admin/execution_context.rb', line 9

def initialize(**args)
  @target = args.delete(:target)
  # When present, unknown methods (view/url helpers like `link_to`, `main_app`,
  # `*_path`) are delegated to this Rails view context inside the block.
  @view_context = args.delete(:view_context)
  @args = args
  args.each do |key, value|
    define_singleton_method(key) { value }
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **kwargs, &block) ⇒ Object



26
27
28
29
30
# File 'lib/ruby_ui_admin/execution_context.rb', line 26

def method_missing(name, *args, **kwargs, &block)
  return super unless @view_context.respond_to?(name)

  @view_context.public_send(name, *args, **kwargs, &block)
end

Instance Method Details

#handleObject



20
21
22
23
24
# File 'lib/ruby_ui_admin/execution_context.rb', line 20

def handle
  return @target unless @target.respond_to?(:call)

  instance_exec(&@target)
end

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

Returns:

  • (Boolean)


32
33
34
# File 'lib/ruby_ui_admin/execution_context.rb', line 32

def respond_to_missing?(name, include_private = false)
  (@view_context&.respond_to?(name)) || super
end