module Pluginator::Extensions::FirstAsk
Extension to find first plugin that answers the question with true
Public Instance Methods
Call a method on plugin and return first one that returns `true`.
@param type [String] name of type to search for plugins @param method_name [Symbol] name of the method to execute @param params [Array] params to pass to the called method @return [Class] The first plugin that method call returns true
# File lib/plugins/pluginator/extensions/first_ask.rb, line 34 def first_ask(type, method_name, *params) @plugins[type] or return nil try_to_find(type, method_name, params) end
Call a method on plugin and return first one that returns `true`. Behaves like `first_ask` but throws exceptions if can not find anything. @param type [String] name of type to search for plugins @param method_name [Symbol] name of the method to execute @param params [Array] params to pass to the called method @return [Class] The first plugin that method call returns true @raise [Pluginator::MissingPlugin] when can not find plugin
# File lib/plugins/pluginator/extensions/first_ask.rb, line 46 def first_ask!(type, method_name, *params) @plugins[type] or raise Pluginator::MissingType.new(type, @plugins.keys) try_to_find(type, method_name, params) or raise Pluginator::MissingPlugin.new(type, "first_ask: #{method_name}", plugins_map(type).keys) end
Private Instance Methods
need to use this trick because of old rubies support
# File lib/plugins/pluginator/extensions/first_ask.rb, line 62 def has_public_method?(plugin, method_name) plugin.public_methods.map(&:to_sym).include?(method_name.to_sym) end
# File lib/plugins/pluginator/extensions/first_ask.rb, line 54 def try_to_find(type, method_name, params) @plugins[type].detect do |plugin| has_public_method?(plugin, method_name) and plugin.send(method_name.to_sym, *params) end end