Here come the utility methods used from the HelperAccess#helper method, we do this to keep method count at a minimum and because HelpersHelper is such an awesome name that just can’t be wasted.
Usage if you want to only extend with helpers:
class Hi Innate::HelpersHelper.each_extend(self, :cgi, :link, :aspect) end
Usage if you only want to include helpers:
class Hi Innate::HelpersHelper.each_include(self, :cgi, :link, :aspect) end
Usage for iteration:
Innate::HelpersHelper.each(:cgi, :link, :aspect) do |mod| p mod end
Usage for translating helpers to modules:
p Innate::HelpersHelper.each(:cgi, :link, :aspect)
Yield all the modules we can find for the given names of helpers, try to require them if not available.
NOTE: Unlike usual #, this will actually return an Array of the found
modules instead of the given +*names+
Usage:
Innate::HelpersHelper.each(:cgi, :link, :aspect) do |mod| p mod end
# File lib/innate/helper.rb, line 102 102: def each(*names) 103: names.map do |name| 104: if name.class == Module 105: yield(name) if block_given? 106: name 107: elsif mod = get(name) 108: yield(mod) if block_given? 109: mod 110: elsif try_require(name) 111: redo 112: else 113: raise LoadError, "Helper #{name} not found" 114: end 115: end 116: end
Shortcut to extend into with Helper modules corresponding to +*names+. into has to respond to #.
Usage:
class Hi Innate::HelpersHelper.each_extend(self, :cgi, :link, :aspect) end
# File lib/innate/helper.rb, line 126 126: def each_extend(into, *names, &block) 127: return if names.empty? 128: into.extend(*each(*names, &block)) 129: end
Shortcut to include Helper modules corresponding to +*names+ on into. into has to respond to #. #send(:include) is used in case # raises due to being private/protected
in case # is a private/protected method.
Usage:
class Hi Innate::HelpersHelper.each_include(self, :cgi, :link, :aspect) end
# File lib/innate/helper.rb, line 143 143: def each_include(into, *names, &block) 144: return if names.compact.empty? 145: into.__send__(:include, *each(*names, &block)) 146: end
# File lib/innate/helper.rb, line 175 175: def find_helper(name) 176: options.paths.uniq.find do |path| 177: base = ::File.join(path, 'helper', name) 178: options.exts.find do |ext| 179: full = "#{base}.#{ext}" 180: return full if ::File.file?(full) 181: end 182: end 183: end
Based on a simple set of rules we will first construct the most likely name for the helper and then grep the constants in the Innate::Helper module for any matches.
helper :foo_bar # => FooBar helper :foo # => Foo
# File lib/innate/helper.rb, line 154 154: def get(name) 155: module_name = /^#{name.to_s.dup.delete('_')}(?:helper)?$/ 156: 157: options.namespaces.each do |namespace| 158: found = namespace.constants.grep(module_name).first 159: return namespace.const_get(found) if found 160: end 161: 162: nil 163: end
Figure out files that might have the helper we ask for and then require the first we find, if any.
# File lib/innate/helper.rb, line 167 167: def try_require(name) 168: if found = find_helper(name.to_s) 169: require(found) || true 170: else 171: raise(LoadError, "Helper #{name} not found") 172: end 173: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.