Class: Clef::Plugins::Registry
- Inherits:
-
Object
- Object
- Clef::Plugins::Registry
- Defined in:
- lib/clef/plugins/registry.rb
Constant Summary collapse
- ERROR_POLICIES =
%i[raise warn collect].freeze
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Instance Method Summary collapse
- #clear ⇒ Array
-
#initialize(error_policy: :raise) ⇒ Registry
constructor
A new instance of Registry.
- #plugins ⇒ Array<Plugins::Base>
- #register(plugin_or_class, *args, priority: 0, **kwargs) ⇒ Plugins::Base
- #run_hook(hook_name, *args) ⇒ Array<Object>
- #unregister(plugin_or_class) ⇒ Plugins::Base?
Constructor Details
#initialize(error_policy: :raise) ⇒ Registry
Returns a new instance of Registry.
11 12 13 14 15 16 17 18 |
# File 'lib/clef/plugins/registry.rb', line 11 def initialize(error_policy: :raise) raise ArgumentError, "unsupported hook error policy" unless ERROR_POLICIES.include?(error_policy) @entries = [] @next_order = 0 @error_policy = error_policy @errors = [] end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
8 9 10 |
# File 'lib/clef/plugins/registry.rb', line 8 def errors @errors end |
Instance Method Details
#clear ⇒ Array
49 50 51 |
# File 'lib/clef/plugins/registry.rb', line 49 def clear @entries.clear end |
#plugins ⇒ Array<Plugins::Base>
21 22 23 |
# File 'lib/clef/plugins/registry.rb', line 21 def plugins @entries.map { |entry| entry[:plugin] } end |
#register(plugin_or_class, *args, priority: 0, **kwargs) ⇒ Plugins::Base
30 31 32 33 34 35 36 |
# File 'lib/clef/plugins/registry.rb', line 30 def register(plugin_or_class, *args, priority: 0, **kwargs) plugin = build_plugin(plugin_or_class, *args, **kwargs) @entries << {plugin: plugin, priority: priority, order: @next_order} @next_order += 1 @entries.sort_by! { |entry| [entry[:priority], entry[:order]] } plugin end |
#run_hook(hook_name, *args) ⇒ Array<Object>
56 57 58 59 60 61 62 |
# File 'lib/clef/plugins/registry.rb', line 56 def run_hook(hook_name, *args) plugins.each_with_object([]) do |plugin, results| next unless plugin.respond_to?(hook_name) results << run_plugin_hook(plugin, hook_name, args) end end |
#unregister(plugin_or_class) ⇒ Plugins::Base?
40 41 42 43 44 45 46 |
# File 'lib/clef/plugins/registry.rb', line 40 def unregister(plugin_or_class) entry = @entries.find { |candidate| plugin_match?(candidate[:plugin], plugin_or_class) } return unless entry @entries.delete(entry) entry[:plugin] end |