Module: Hooks::Core::ComponentAccess
- Included in:
- Plugins::Auth::Base, Plugins::Handlers::Base, Plugins::Instruments::FailbotBase, Plugins::Instruments::StatsBase, Plugins::Lifecycle
- Defined in:
- lib/hooks/core/component_access.rb
Overview
Shared module providing access to global components (logger, stats, failbot, and user-defined components)
This module provides a consistent interface for accessing global components across all plugin types, eliminating code duplication and ensuring consistent behavior throughout the application.
In addition to built-in components (log, stats, failbot), this module provides dynamic access to any user-defined components passed to Hooks.build().
Instance Method Summary collapse
-
#failbot ⇒ Hooks::Plugins::Instruments::Failbot
Global failbot component accessor Provides access to the global failbot component for reporting errors to services like Sentry, Rollbar, etc.
-
#log ⇒ Hooks::Log
Short logger accessor Provides a convenient way to log messages without needing to reference the full Hooks::Log namespace.
-
#method_missing(method_name, *args, **kwargs, &block) ⇒ Object
Dynamic method access for user-defined components.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Respond to user-defined component names.
-
#stats ⇒ Hooks::Plugins::Instruments::Stats
Global stats component accessor Provides access to the global stats component for reporting metrics to services like DataDog, New Relic, etc.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, **kwargs, &block) ⇒ Object
Dynamic method access for user-defined components
This method enables handlers to call user-defined components as methods. For example, if a user registers a ‘publisher’ component, handlers can call ‘publisher` or `publisher.some_method` directly.
The method supports multiple usage patterns:
-
Direct access: Returns the component instance for further method calls
-
Callable access: If the component responds to #call, invokes it with provided arguments
-
Method chaining: Allows fluent interface patterns with registered components
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/hooks/core/component_access.rb', line 159 def method_missing(method_name, *args, **kwargs, &block) component = Hooks::Core::GlobalComponents.get_extra_component(method_name) if component # If called with arguments or block, try to call the component as a method if args.any? || kwargs.any? || block component.call(*args, **kwargs, &block) else # Otherwise return the component itself component end else # Fall back to normal method_missing behavior super end end |
Instance Method Details
#failbot ⇒ Hooks::Plugins::Instruments::Failbot
Global failbot component accessor Provides access to the global failbot component for reporting errors to services like Sentry, Rollbar, etc.
94 95 96 |
# File 'lib/hooks/core/component_access.rb', line 94 def failbot Hooks::Core::GlobalComponents.failbot end |
#log ⇒ Hooks::Log
Short logger accessor Provides a convenient way to log messages without needing to reference the full Hooks::Log namespace.
70 71 72 |
# File 'lib/hooks/core/component_access.rb', line 70 def log Hooks::Log.instance end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Respond to user-defined component names
This method ensures that handlers properly respond to user-defined component names, enabling proper method introspection and duck typing support.
218 219 220 |
# File 'lib/hooks/core/component_access.rb', line 218 def respond_to_missing?(method_name, include_private = false) Hooks::Core::GlobalComponents.extra_component_exists?(method_name) || super end |
#stats ⇒ Hooks::Plugins::Instruments::Stats
Global stats component accessor Provides access to the global stats component for reporting metrics to services like DataDog, New Relic, etc.
82 83 84 |
# File 'lib/hooks/core/component_access.rb', line 82 def stats Hooks::Core::GlobalComponents.stats end |