Class: Rails::GraphQL::Callback

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/graphql/callback.rb

Overview

Rails GraphQL Callback

An extra powerful proc that can handle way more situations than the original block caller

Constant Summary collapse

EXCLUSIVE_ARGUMENT =
:exclusive_callback

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, event_name, *args, **xargs, &block) ⇒ Callback

Returns a new instance of Callback.

Raises:

  • (::ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rails/graphql/callback.rb', line 23

def initialize(target, event_name, *args, **xargs, &block)
  raise ::ArgumentError, (+<<~MSG).squish if block.nil? && !args.first.present?
    Either provide a block or a method name when setting a #{event_name}
    callback on #{target.inspect}.
  MSG

  if block.nil?
    block = args.shift
    valid_format = block.is_a?(Symbol) || block.is_a?(Proc)
    raise ::ArgumentError, (+<<~MSG).squish unless valid_format
      The given #{block.class.name} class is not a valid callback.
    MSG
  end

  @target = target
  @event_name = event_name

  @exclusive = xargs.delete(EXCLUSIVE_ARGUMENT)
  @exclusive = target.try(:default_exclusive?, event_name) if @exclusive.nil?
  @exclusive = true if @exclusive.nil?

  @pre_args = args
  @pre_xargs = xargs.slice!(*event_filters.keys)
  @filters = xargs

  @block = block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



12
13
14
# File 'lib/rails/graphql/callback.rb', line 12

def block
  @block
end

#event_nameObject (readonly)

Returns the value of attribute event_name.



12
13
14
# File 'lib/rails/graphql/callback.rb', line 12

def event_name
  @event_name
end

#filtersObject (readonly)

Returns the value of attribute filters.



12
13
14
# File 'lib/rails/graphql/callback.rb', line 12

def filters
  @filters
end

#targetObject (readonly)

Returns the value of attribute target.



12
13
14
# File 'lib/rails/graphql/callback.rb', line 12

def target
  @target
end

Class Method Details

.set_context(item, context) ⇒ Object

Directives need to be contextualized by the given instance as context



19
20
21
# File 'lib/rails/graphql/callback.rb', line 19

def self.set_context(item, context)
  lambda { |*args, **xargs| item.call(*args, _callback_context: context, **xargs) }
end

Instance Method Details

#call(event, *args, _callback_context: nil, **xargs) ⇒ Object

This does the whole checking and preparation in order to really execute the callback method



53
54
55
56
57
58
59
# File 'lib/rails/graphql/callback.rb', line 53

def call(event, *args, _callback_context: nil, **xargs)
  return unless event.event_name === event_name && can_run?(event)

  block.is_a?(Symbol) \
    ? call_symbol(event, *args, **xargs) \
    : call_proc(event, _callback_context, *args, **xargs)
end

#exclusive?Boolean

Return if this event is exclusive, so that only the original source of the callback will be allowed to receive it

Returns:

  • (Boolean)


63
64
65
# File 'lib/rails/graphql/callback.rb', line 63

def exclusive?
  @exclusive
end

#source_locationObject

Get a described source location for the callback



68
69
70
71
72
# File 'lib/rails/graphql/callback.rb', line 68

def source_location
  block.is_a?(Proc) ? block.source_location : begin
    [+"(symbolized-callback/#{target.inspect})", block]
  end
end

#to_procObject

This basically allows the class to be passed as &block



75
76
77
78
79
# File 'lib/rails/graphql/callback.rb', line 75

def to_proc
  method(:call).to_proc.tap do |block|
    block.define_singleton_method(:source_location, &method(:source_location))
  end
end