Sha256: 3449612e2abea8db1c2afbd394c1890ee6411a2f9eb4d35848ca1a0384e598cc

Contents?: true

Size: 1.31 KB

Versions: 11

Compression:

Stored size: 1.31 KB

Contents

module Avo
  class ExecutionContext
    attr_accessor :target, :context, :params, :view_context, :current_user, :request, :include, :main_app, :avo

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

    delegate :result, to: :card
    delegate :authorize, to: Avo::Services::AuthorizationService

    # Return target if target is not callable, otherwise, execute target on this instance context
    def handle
      target.respond_to?(:call) ? instance_exec(&target) : target
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
avo-3.0.0.pre14 lib/avo/execution_context.rb
avo-3.0.0.pre13 lib/avo/execution_context.rb
avo-3.0.0.pre12 lib/avo/execution_context.rb
avo-3.0.0.pre10 lib/avo/execution_context.rb
avo-3.0.0.pre11 lib/avo/execution_context.rb
avo-3.0.0.pre8 lib/avo/execution_context.rb
avo-3.0.0.pre9 lib/avo/execution_context.rb
avo-3.0.0.pre7 lib/avo/execution_context.rb
avo-3.0.0.pre5 lib/avo/execution_context.rb
avo-3.0.0.pre6 lib/avo/execution_context.rb
avo-3.0.0.pre4 lib/avo/execution_context.rb