Class: Jav::ExecutionContext

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ ExecutionContext

Returns a new instance of ExecutionContext.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/jav/execution_context.rb', line 5

def initialize(**args)
  # Extend the class with custom modules if required.
  if args[:include].present?
    args[:include].each do |mod|
      self.class.send(:include, mod)
    end
  end

  # If you want this block to behave like a view you can delegate the missing methods to the view_context
  #
  # Ex: Jav::ExecutionContext.new(target: ..., delegate_missing_to: :view_context).handle
  self.class.send(:delegate_missing_to, args[:delegate_missing_to]) if args[:delegate_missing_to].present?

  # If target doesn't respond to call, we don't need to initialize the others attr_accessors.
  return unless (@target = args[:target]).respond_to? :call

  args.except(:target).each do |key, value|
    singleton_class.class_eval { attr_accessor key }
    instance_variable_set(:"@#{key}", value)
  end

  # Set defaults on not initialized accessors
  @context ||= Jav::App.context
  @params ||= Jav::App.params
  @view_context ||= Jav::App.view_context
  @current_user ||= Jav::App.current_user
  @request ||= view_context&.request
  @main_app ||= view_context&.main_app
  @jav ||= view_context&.jav
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



3
4
5
# File 'lib/jav/execution_context.rb', line 3

def context
  @context
end

#current_userObject

Returns the value of attribute current_user.



3
4
5
# File 'lib/jav/execution_context.rb', line 3

def current_user
  @current_user
end

#includeObject

Returns the value of attribute include.



3
4
5
# File 'lib/jav/execution_context.rb', line 3

def include
  @include
end

#javObject

Returns the value of attribute jav.



3
4
5
# File 'lib/jav/execution_context.rb', line 3

def jav
  @jav
end

#main_appObject

Returns the value of attribute main_app.



3
4
5
# File 'lib/jav/execution_context.rb', line 3

def main_app
  @main_app
end

#paramsObject

Returns the value of attribute params.



3
4
5
# File 'lib/jav/execution_context.rb', line 3

def params
  @params
end

#requestObject

Returns the value of attribute request.



3
4
5
# File 'lib/jav/execution_context.rb', line 3

def request
  @request
end

#targetObject

Returns the value of attribute target.



3
4
5
# File 'lib/jav/execution_context.rb', line 3

def target
  @target
end

#view_contextObject

Returns the value of attribute view_context.



3
4
5
# File 'lib/jav/execution_context.rb', line 3

def view_context
  @view_context
end

Instance Method Details

#handleObject

Return target if target is not callable, otherwise, execute target on this instance context



37
38
39
# File 'lib/jav/execution_context.rb', line 37

def handle
  target.respond_to?(:call) ? instance_exec(&target) : target
end