Class: Gritz::Controller

Inherits:
Object
  • Object
show all
Defined in:
/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/controller.rb,
tmp/docs-input/dynamic_api.rb

Overview

One instance is created per RPC; application actions use request and stream.

Direct Known Subclasses

Gritz::Compat::Gruf::Controller

Defined Under Namespace

Classes: Request, Stream

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context:) ⇒ Controller

Returns a new instance of Controller.



43
44
45
46
47
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/controller.rb', line 43

def initialize(context:)
  @context = context
  @request = Request.for(context)
  @stream = Stream.new(context.call, context)
end

Instance Attribute Details

#context ⇒ Object (readonly)



41
42
43
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/controller.rb', line 41

def context
  @context
end

#request ⇒ Object (readonly)



41
42
43
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/controller.rb', line 41

def request
  @request
end

#stream ⇒ Object (readonly)



41
42
43
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/controller.rb', line 41

def stream
  @stream
end

Class Method Details

.action_defined?(action) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/controller.rb', line 16

def action_defined?(action)
  public_method_defined?(action) && ancestors.take_while { |ancestor| ancestor != Controller }.include?(instance_method(action).owner)
end

.after_action(method = nil, only: nil, except: nil, &block) ⇒ Object

Registers a after callback by method name or block.

Parameters:

  • method (Symbol, nil) (defaults to: nil) —

    callback method name

  • only (Symbol, Array<Symbol>, nil) (defaults to: nil) —

    actions that run this callback

  • except (Symbol, Array<Symbol>, nil) (defaults to: nil) —

    actions that skip this callback



166
# File 'tmp/docs-input/dynamic_api.rb', line 166

def self.after_action(method = nil, only: nil, except: nil, &block); end

.around_action(method = nil, only: nil, except: nil, &block) ⇒ Object

Registers a around callback by method name or block.

Parameters:

  • method (Symbol, nil) (defaults to: nil) —

    callback method name

  • only (Symbol, Array<Symbol>, nil) (defaults to: nil) —

    actions that run this callback

  • except (Symbol, Array<Symbol>, nil) (defaults to: nil) —

    actions that skip this callback



160
# File 'tmp/docs-input/dynamic_api.rb', line 160

def self.around_action(method = nil, only: nil, except: nil, &block); end

.before_action(method = nil, only: nil, except: nil, &block) ⇒ Object

Registers a before callback by method name or block.

Parameters:

  • method (Symbol, nil) (defaults to: nil) —

    callback method name

  • only (Symbol, Array<Symbol>, nil) (defaults to: nil) —

    actions that run this callback

  • except (Symbol, Array<Symbol>, nil) (defaults to: nil) —

    actions that skip this callback



154
# File 'tmp/docs-input/dynamic_api.rb', line 154

def self.before_action(method = nil, only: nil, except: nil, &block); end

.bind(service) ⇒ Object

Raises:

  • (ArgumentError)


8
9
10
11
12
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/controller.rb', line 8

def bind(service)
  raise ArgumentError, "service must expose service_name and rpc_descs" unless service.respond_to?(:service_name) && service.respond_to?(:rpc_descs)

  @service_class = service
end

.filters ⇒ Object



29
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/controller.rb', line 29

def filters = (superclass.respond_to?(:filters) ? superclass.filters : []) + (@filters || [])

.rescue_from(*errors, with: nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
35
36
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/controller.rb', line 31

def rescue_from(*errors, with: nil, &block)
  handler = with || block
  raise ArgumentError, "handler required" unless handler

  (@rescue_handlers ||= []).concat(errors.map { |error| [error, handler] })
end

.rescue_handlers ⇒ Object



38
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/controller.rb', line 38

def rescue_handlers = (superclass.respond_to?(:rescue_handlers) ? superclass.rescue_handlers : []) + (@rescue_handlers || [])

.service_class ⇒ Object



14
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/controller.rb', line 14

def service_class = @service_class || (superclass.service_class if superclass.respond_to?(:service_class))

Instance Method Details

#fail!(code, message = nil, details: [], metadata: {}) ⇒ Object

Raises:



81
82
83
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/controller.rb', line 81

def fail!(code, message = nil, details: [], metadata: {})
  raise Errors.for_code(code).new(message, details:, metadata:)
end

#process(action) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/controller.rb', line 49

def process(action)
  raise Errors::Unimplemented, "unimplemented RPC action: #{action}" unless self.class.action_defined?(action)

  applicable = self.class.filters.select do |_, _, only, except|
    (only.empty? || only.include?(action)) && !except.include?(action)
  end
  endpoint = lambda do
    applicable.each { |kind, callback, _, _| invoke_callback(callback) if kind == :before }
    result = public_send(action)
    applicable.each { |kind, callback, _, _| invoke_callback(callback) if kind == :after }
    result
  end
  applicable.select { |kind,| kind == :around }.reverse_each do |_, callback, _, _|
    inner = endpoint
    endpoint = lambda do
      called = false
      result = nil
      callback_result = invoke_callback(callback, lambda {
        called = true
        result = inner.call
      })
      called ? result : callback_result
    end
  end
  endpoint.call
rescue StandardError => e
  handler = self.class.rescue_handlers.reverse.find { |klass, _| e.is_a?(klass) }
  raise unless handler

  invoke_callback(handler.last, e)
end