Module: OpenapiRuby::Hanami

Defined in:
lib/openapi_ruby/hanami.rb

Overview

Hanami 2 integration. Rails receives this wiring from the engine's initializer and from rspec-rails; Hanami has no engines, so the host app makes the calls itself.

::Hanami is spelled with leading colons throughout — inside this namespace a bare Hanami would resolve to OpenapiRuby::Hanami.

Defined Under Namespace

Modules: RackTestApp

Class Method Summary collapse

Class Method Details

.apply_component_path_default!Object

Requiring this file declares the host, so the component default no longer depends on ::Hanami being loaded before the configuration object was built. Only a pristine default is replaced.



60
61
62
63
64
65
# File 'lib/openapi_ruby/hanami.rb', line 60

def apply_component_path_default!
  config = OpenapiRuby.configuration
  return unless config.component_paths == Configuration::RAILS_COMPONENT_PATHS

  config.component_paths = Configuration::HANAMI_COMPONENT_PATHS.dup
end

.config_root(config) ⇒ Object



52
53
54
55
# File 'lib/openapi_ruby/hanami.rb', line 52

def config_root(config)
  root = config.respond_to?(:root) ? config.root : nil
  root unless root.to_s.empty?
end

.install_middleware!(config, root: nil) ⇒ Object

Mounts the runtime validation middleware. In config/app.rb, after requiring the file that holds your OpenapiRuby.configure block:

module MyApp
class App < Hanami::App
  OpenapiRuby::Hanami.install_middleware!(config)
end
end

Takes the app config (preferred — it carries the root) or a bare middleware stack.



34
35
36
37
38
39
# File 'lib/openapi_ruby/hanami.rb', line 34

def install_middleware!(config, root: nil)
  stack = config.respond_to?(:middleware) ? config.middleware : config
  root ||= config_root(config) || OpenapiRuby.app_root

  Middleware::Installer.install!(stack, root: root)
end

.install_rspec!(rspec_config = nil) ⇒ Object

Wires rack-test into :openapi example groups. Called for you by require "openapi_ruby/rspec"; exposed for suites that configure RSpec by hand.



44
45
46
47
48
49
50
# File 'lib/openapi_ruby/hanami.rb', line 44

def install_rspec!(rspec_config = nil)
  if rspec_config
    Adapters::RSpec.install_rack_test!(rspec_config, app_module: RackTestApp)
  else
    ::RSpec.configure { |config| Adapters::RSpec.install_rack_test!(config, app_module: RackTestApp) }
  end
end