Class: GraphqlRails::Controller::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql_rails/controller/configuration.rb

Overview

stores all graphql_rails controller specific config

Defined Under Namespace

Classes: InvalidActionConfiguration

Constant Summary collapse

LIB_REGEXP =
%r{/graphql_rails/lib/}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ Configuration

Returns a new instance of Configuration.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/graphql_rails/controller/configuration.rb', line 18

def initialize(controller)
  @controller = controller
  @hooks = {
    before: {},
    after: {},
    around: {}
  }

  @action_by_name = {}
  @action_default = nil
  @error_handlers = {}
end

Instance Attribute Details

#action_by_nameObject (readonly)

Returns the value of attribute action_by_name.



16
17
18
# File 'lib/graphql_rails/controller/configuration.rb', line 16

def action_by_name
  @action_by_name
end

#error_handlersObject (readonly)

Returns the value of attribute error_handlers.



16
17
18
# File 'lib/graphql_rails/controller/configuration.rb', line 16

def error_handlers
  @error_handlers
end

Instance Method Details

#action(method_name) {|| ... } ⇒ Object

Yields:

  • ()


70
71
72
73
74
75
76
77
78
79
# File 'lib/graphql_rails/controller/configuration.rb', line 70

def action(method_name)
  action_name = method_name.to_s.underscore
  @action_by_name[action_name] ||= action_default.dup_with(
    name: action_name,
    controller: controller,
    defined_at: dynamic_source_location
  )
  yield(@action_by_name[action_name]) if block_given?
  @action_by_name[action_name]
end

#action_config(method_name) ⇒ Object



81
82
83
84
# File 'lib/graphql_rails/controller/configuration.rb', line 81

def action_config(method_name)
  action_name = method_name.to_s.underscore
  @action_by_name.fetch(action_name) { raise_invalid_config_error(action_name) }
end

#action_default {|@action_default| ... } ⇒ Object

Yields:



64
65
66
67
68
# File 'lib/graphql_rails/controller/configuration.rb', line 64

def action_default
  @action_default ||= ActionConfiguration.new(name: :default, controller: nil)
  yield(@action_default) if block_given?
  @action_default
end

#action_hooks_for(hook_type, action_name) ⇒ Object



48
49
50
# File 'lib/graphql_rails/controller/configuration.rb', line 48

def action_hooks_for(hook_type, action_name)
  hooks[hook_type].values.select { |hook| hook.applicable_for?(action_name) }
end

#add_action_hook(hook_type, name = nil, **options, &block) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/graphql_rails/controller/configuration.rb', line 52

def add_action_hook(hook_type, name = nil, **options, &block)
  hook_name = name&.to_sym
  hook_key = hook_name || :"anonymous_#{block.hash}"

  hooks[hook_type][hook_key] = \
    ActionHook.new(name: hook_name, **options, &block)
end

#add_error_handler(error, with:, &block) ⇒ Object



60
61
62
# File 'lib/graphql_rails/controller/configuration.rb', line 60

def add_error_handler(error, with:, &block)
  @error_handlers[error] = with || block
end

#dup_with(controller:) ⇒ Object



42
43
44
45
46
# File 'lib/graphql_rails/controller/configuration.rb', line 42

def dup_with(controller:)
  dup.tap do |new_config|
    new_config.instance_variable_set(:@controller, controller)
  end
end

#initialize_copy(other) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/graphql_rails/controller/configuration.rb', line 31

def initialize_copy(other)
  super

  @action_by_name = other.instance_variable_get(:@action_by_name).transform_values(&:dup)

  hooks_to_copy = other.instance_variable_get(:@hooks)
  @hooks = hooks_to_copy.each.with_object({}) do |(hook_type, type_hooks), new_hooks|
    new_hooks[hook_type] = type_hooks.transform_values(&:dup)
  end
end

#model(model = nil) ⇒ Object



86
87
88
# File 'lib/graphql_rails/controller/configuration.rb', line 86

def model(model = nil)
  action_default.model(model)
end