Class: RbsMacros::LibraryRegistry
- Inherits:
-
Object
- Object
- RbsMacros::LibraryRegistry
- Extended by:
- SingleForwardable
- Defined in:
- lib/rbs_macros/library_registry.rb
Overview
RbsMacros allow you to define a reusable set of macro definitions called a library. This is usually registered to the global singleton of LibraryRegistry like:
# my_library/rbs_macros.rb
RbsMacros::LibraryRegistry.register_macros("my_library/rbs_macros") do |macros|
macros << MyMacro1
macros << MyMacro2
end
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ LibraryRegistry
constructor
A new instance of LibraryRegistry.
- #lookup_macros(name, soft_fail: false) ⇒ Object
- #register_macros(name, macros = [], &block) ⇒ Object
- #require_library(name, soft_fail: false) ⇒ Object
Constructor Details
#initialize ⇒ LibraryRegistry
Returns a new instance of LibraryRegistry.
20 21 22 |
# File 'lib/rbs_macros/library_registry.rb', line 20 def initialize @libraries = {} end |
Class Method Details
.require_library(name, soft_fail: false) ⇒ Object
46 47 48 49 50 |
# File 'lib/rbs_macros/library_registry.rb', line 46 def @global.require_library(name, soft_fail: false) require name rescue LoadError raise unless soft_fail end |
Instance Method Details
#lookup_macros(name, soft_fail: false) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/rbs_macros/library_registry.rb', line 31 def lookup_macros(name, soft_fail: false) unless @libraries.key?(name) require_library(name, soft_fail:) raise ArgumentError, "Unknown library: #{name}" if !@libraries.key?(name) && !soft_fail end @libraries[name] || [] end |
#register_macros(name, macros = [], &block) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/rbs_macros/library_registry.rb', line 24 def register_macros(name, macros = [], &block) a = @libraries.fetch(name) { |k| @libraries[k] = [] } a.push(*macros) block&.(a) nil end |
#require_library(name, soft_fail: false) ⇒ Object
40 41 42 43 |
# File 'lib/rbs_macros/library_registry.rb', line 40 def require_library(name, soft_fail: false) # To be implemented by subclasses raise ArgumentError, "Unknown library: #{name}" unless soft_fail end |