Class: Avo::ExecutionContext

Inherits:
Object
  • Object
show all
Includes:
Concerns::HasHelpers
Defined in:
lib/avo/execution_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::HasHelpers

#helpers

Constructor Details

#initialize(**args) ⇒ ExecutionContext

Returns a new instance of ExecutionContext.



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/avo/execution_context.rb', line 9

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 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 ||= Avo::Current.context
  @current_user ||= Avo::Current.user
  @params ||= Avo::Current.params
  @request ||= Avo::Current.request
  @view_context ||= Avo::Current.view_context
  @locale ||= Avo::Current.locale
  @main_app ||= @view_context&.main_app
  @avo ||= @view_context&.avo
end

Instance Attribute Details

#avoObject

Returns the value of attribute avo.



5
6
7
# File 'lib/avo/execution_context.rb', line 5

def avo
  @avo
end

#contextObject

Returns the value of attribute context.



5
6
7
# File 'lib/avo/execution_context.rb', line 5

def context
  @context
end

#current_userObject

Returns the value of attribute current_user.



5
6
7
# File 'lib/avo/execution_context.rb', line 5

def current_user
  @current_user
end

#includeObject

Returns the value of attribute include.



5
6
7
# File 'lib/avo/execution_context.rb', line 5

def include
  @include
end

#localeObject

Returns the value of attribute locale.



5
6
7
# File 'lib/avo/execution_context.rb', line 5

def locale
  @locale
end

#main_appObject

Returns the value of attribute main_app.



5
6
7
# File 'lib/avo/execution_context.rb', line 5

def main_app
  @main_app
end

#paramsObject

Returns the value of attribute params.



5
6
7
# File 'lib/avo/execution_context.rb', line 5

def params
  @params
end

#requestObject

Returns the value of attribute request.



5
6
7
# File 'lib/avo/execution_context.rb', line 5

def request
  @request
end

#targetObject

Returns the value of attribute target.



5
6
7
# File 'lib/avo/execution_context.rb', line 5

def target
  @target
end

#view_contextObject

Returns the value of attribute view_context.



5
6
7
# File 'lib/avo/execution_context.rb', line 5

def view_context
  @view_context
end

Instance Method Details

#handleObject

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



40
41
42
# File 'lib/avo/execution_context.rb', line 40

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