lib/spontaneous/site/helpers.rb in spontaneous-0.2.0.beta1 vs lib/spontaneous/site/helpers.rb in spontaneous-0.2.0.beta2

- old
+ new

@@ -5,14 +5,19 @@ extend Spontaneous::Concern module ClassMethods def helper(*formats, &block) helper_module = Module.new(&block) - register_helper(helper_module, *formats.flatten) + register_helper(helper_module, helper_module_name(caller), *formats.flatten) helper_module end + def helper_module_name(_caller) + path = _caller.first.split(':').first.split('/')[-5..-1] + path.join('_').gsub(/[^A-Za-z_]/, '_').gsub(/_+/, '_').gsub(/^_+/, '').capitalize + end + # Private: Generates a module including all helper methods for the requested format(s) def context(*formats) formats = [:*].concat formats.flatten.map { |f| f.to_sym } helper_module = Module.new formats.each do |format| @@ -26,17 +31,29 @@ end end helper_module end - def register_helper(helper_module, *formats) - site = instance + def register_helper(helper_module, module_name, *formats) + site = instance + helpers = site.registered_helpers if formats.empty? - site.registered_helpers[:*] << helper_module + helpers[:*] << helper_module else formats.each do |format| - site.registered_helpers[format.to_sym] << helper_module + helpers[format.to_sym] << helper_module end + end + # Spontaneous::Site::Helpers.const_set(module_name, helper_module) + def helper_module.__finalize + Spontaneous::Site.unregister_helper(self) + end + end + + def unregister_helper(helper_module) + helpers = instance.registered_helpers + helpers.each do |format, helpers| + helpers.delete(helper_module) end end def registered_helpers instance.registered_helpers