Module: ActionHandler

Defined in:
lib/action_handler/installer.rb,
lib/action_handler.rb,
lib/action_handler/args.rb,
lib/action_handler/call.rb,
lib/action_handler/equip.rb,
lib/action_handler/config.rb,
lib/action_handler/version.rb,
lib/action_handler/args_maker.rb,
lib/action_handler/controller.rb,
lib/action_handler/args/params.rb,
lib/action_handler/args/default.rb,
lib/action_handler/response_evaluator.rb

Overview

It is better if there is a way to return streaming response. (‘self.response_body = ` or `response.stream.write`?)

Defined Under Namespace

Modules: Args, Controller, ControllerExtension, Equip, HandlerExtension Classes: ActionArgumentError, ArgsMaker, Call, Config, Installer, ResponseEvaluator

Constant Summary collapse

CONFIG_VAR_NAME =
:@_action_handler_config
VERSION =
'0.2.0'

Class Method Summary collapse

Class Method Details

.autoload_handlers_from_controller_fileObject

Enable to autoload handlers defined in a controller file. Rails autoloading works only if the constant is defined in a file matching its name. So if ‘FooHandler` is defined in `foo_controller.rb`, it cannot be autoloaded. (guides.rubyonrails.org/autoloading_and_reloading_constants.html)

So this hooks const_missing and load the corresponding controller. If the controller exists, its handler will be loaded as well.

Currently this supports only the handlers defined in the top level scope.



19
20
21
22
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
50
# File 'lib/action_handler.rb', line 19

module_function def autoload_handlers_from_controller_file
  unless defined? Rails
    raise 'Rails is not defined. This method is supposed to use in Rails environment.'
  end

  return if @hook_registered

  @hook_registered = true

  # Perhaps this warning is a RuboCop's bug.
  # rubocop:disable Lint/NestedMethodDefinition
  def Object.const_missing(name)
    # rubocop:enable Lint/NestedMethodDefinition

    return super unless name =~ /\A[a-zA-Z0-9_]+Handler\z/
    return super if name == :ActionHandler

    # Try to autoload the corresponding controller.
    prefix = name.to_s.sub(/Handler\z/, '')
    begin
      const_get("::#{prefix}Controller")
    rescue NameError
      super
    end

    # Return the handler if loaded.
    return const_get(name) if Object.const_defined?(name)

    # Otherwise raise the NameError.
    super
  end
end