Module: Innate::HelpersHelper
- Includes:
- Optioned
- Defined in:
- lib/innate/helper.rb
Overview
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)
Constant Summary collapse
- EXTS =
%w[rb so bundle]
Class Method Summary collapse
-
.each(*names) ⇒ Object
Yield all the modules we can find for the given names of helpers, try to require them if not available.
-
.each_extend(into, *names, &block) ⇒ Object
Shortcut to extend
into
with Helper modules corresponding to *names. -
.each_include(into, *names, &block) ⇒ Object
Shortcut to include Helper modules corresponding to *names on
into
. - .find_helper(name) ⇒ Object
-
.get(name) ⇒ Object
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.
-
.try_require(name) ⇒ Object
Figure out files that might have the helper we ask for and then require the first we find, if any.
Methods included from Optioned
Class Method Details
.each(*names) ⇒ Object
Yield all the modules we can find for the given names of helpers, try to require them if not available.
NOTE: Unlike usual #each, 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
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/innate/helper.rb', line 102 def each(*names) names.map do |name| if name.class == Module yield(name) if block_given? name elsif mod = get(name) yield(mod) if block_given? mod elsif try_require(name) redo else raise LoadError, "Helper #{name} not found" end end end |
.each_extend(into, *names, &block) ⇒ Object
Shortcut to extend into
with Helper modules corresponding to *names. into
has to respond to #extend.
Usage:
class Hi
Innate::HelpersHelper.each_extend(self, :cgi, :link, :aspect)
end
126 127 128 129 |
# File 'lib/innate/helper.rb', line 126 def each_extend(into, *names, &block) return if names.empty? into.extend(*each(*names, &block)) end |
.each_include(into, *names, &block) ⇒ Object
Shortcut to include Helper modules corresponding to *names on into
. into
has to respond to #include. #__send__(:include) is used in case #include raises due to being private/protected
in case #include is a private/protected method.
Usage:
class Hi
Innate::HelpersHelper.each_include(self, :cgi, :link, :aspect)
end
143 144 145 146 |
# File 'lib/innate/helper.rb', line 143 def each_include(into, *names, &block) return if names.compact.empty? into.__send__(:include, *each(*names, &block)) end |
.find_helper(name) ⇒ Object
175 176 177 178 179 180 181 182 183 |
# File 'lib/innate/helper.rb', line 175 def find_helper(name) .paths.uniq.find do |path| base = ::File.join(path, 'helper', name) .exts.find do |ext| full = "#{base}.#{ext}" return full if ::File.file?(full) end end end |
.get(name) ⇒ Object
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
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/innate/helper.rb', line 154 def get(name) module_name = /^#{name.to_s.dup.delete('_')}$/i .namespaces.each do |namespace| found = namespace.constants.grep(module_name).first return namespace.const_get(found) if found end nil end |
.try_require(name) ⇒ Object
Figure out files that might have the helper we ask for and then require the first we find, if any.
167 168 169 170 171 172 173 |
# File 'lib/innate/helper.rb', line 167 def try_require(name) if found = find_helper(name.to_s) require(found) || true else raise(LoadError, "Helper #{name} not found") end end |