lib/locomotive/plugin/liquid.rb in locomotive_plugins-1.0.0.beta2 vs lib/locomotive/plugin/liquid.rb in locomotive_plugins-1.0.0.beta3

- old
+ new

@@ -22,64 +22,61 @@ # and the tag classes are modified so that they check the liquid # context to determine whether they are enabled and should render # normally def prefixed_liquid_tags(prefix) self.liquid_tags.inject({}) do |hash, (tag_name, tag_class)| - hash["#{prefix}_#{tag_name}"] = tag_subclass(tag_class) + hash["#{prefix}_#{tag_name}"] = tag_subclass(prefix, tag_class) hash end end protected # Creates a nested subclass to handle rendering this tag - def tag_subclass(tag_class) + def tag_subclass(prefix, tag_class) tag_class.class_eval <<-CODE class TagSubclass < #{tag_class.to_s} include ::Locomotive::Plugin::TagSubclassMethods + + def self.prefix + '#{prefix}' + end end CODE tag_class::TagSubclass end end # Gets the module to include as a filter in liquid. It prefixes the # filter methods with the given string def prefixed_liquid_filter_module(prefix) - # Build up a string to eval into the module so we only need to reopen - # it once - strings_to_eval = [] + # Create the module to be returned + @prefixed_liquid_filter_module = Module.new do + include ::Locomotive::Plugin::Liquid::PrefixedFilterModule + end + # Add the prefixed methods to the module raw_filter_modules = [self.class.liquid_filters].flatten.compact raw_filter_modules.each do |mod| mod.public_instance_methods.each do |meth| - strings_to_eval << <<-CODE - def #{prefix}_#{meth}(input) - self._passthrough_filter_call_for_#{prefix}('#{meth}', input) + @prefixed_liquid_filter_module.module_eval do + define_method(:"#{prefix}_#{meth}") do |input| + self._passthrough_filter_call(prefix, meth, input) end - CODE + end end end - strings_to_eval << <<-CODE + # Add a method which returns the modules to include for this prefix + @prefixed_liquid_filter_module.module_eval do protected - def _passthrough_object_for_#{prefix} - @_passthrough_object_for_#{prefix} ||= \ - self._build_passthrough_object([#{raw_filter_modules.join(',')}]) + define_method(:"_modules_for_#{prefix}") do + raw_filter_modules end - - def _passthrough_filter_call_for_#{prefix}(meth, input) - self._passthrough_object_for_#{prefix}.public_send(meth, input) - end - CODE - - # Eval the dynamic methods in - @prefixed_liquid_filter_module = Module.new do - include ::Locomotive::Plugin::Liquid::PrefixedFilterModule end - @prefixed_liquid_filter_module.class_eval strings_to_eval.join("\n") + @prefixed_liquid_filter_module end end end