Module: Pluginator::Extensions::FirstAsk

Includes:
PluginsMap
Defined in:
lib/plugins/pluginator/extensions/first_ask.rb

Overview

Extension to find first plugin that answers the question with true

Instance Method Summary collapse

Methods included from PluginsMap

#plugins_map

Instance Method Details

#first_ask(type, method_name, *params) ⇒ Object

Call a method on plugin and return first one that returns ‘true`.

Parameters:

  • type (String)

    name of type to search for plugins

  • method_name (Symbol)

    name of the method to execute

  • params (Array)

    params to pass to the called method

Returns:

  • The first plugin that method call returns true



15
16
17
18
19
20
21
# File 'lib/plugins/pluginator/extensions/first_ask.rb', line 15

def first_ask(type, method_name, *params)
  @plugins[type] or return nil
  @plugins[type].detect do |plugin|
    plugin.respond_to?(method_name.to_sym) &&
    plugin.public_send(method_name.to_sym, *params)
  end
end

#first_ask!(type, method_name, *params) ⇒ Object

Call a method on plugin and return first one that returns ‘true`. Behaves like `first_ask` but throws exceptions if can not find anything.



25
26
27
28
29
30
31
32
# File 'lib/plugins/pluginator/extensions/first_ask.rb', line 25

def first_ask!(type, method_name, *params)
  @plugins[type] or raise Pluginator::MissingType.new(type, @plugins.keys)
  @plugins[type].detect do |plugin|
    plugin.respond_to?(method_name.to_sym) &&
    plugin.public_send(method_name, *params)
  end or
    raise Pluginator::MissingPlugin.new(type, "first_ask: #{method_name}", plugins_map(type).keys)
end