From 23a23c6419081aa433c756900a83c348bf43263f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dorian=20Mari=C3=A9?= Date: Tue, 24 Mar 2015 14:09:08 +0100 Subject: [PATCH] Update module_ex.rb code documentation and code style Didn't look good on rubydoc.info: http://i.imgur.com/469N92P.png --- lib/liquid/module_ex.rb | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/liquid/module_ex.rb b/lib/liquid/module_ex.rb index 40fa3a4..9c7cc00 100644 --- a/lib/liquid/module_ex.rb +++ b/lib/liquid/module_ex.rb @@ -7,16 +7,17 @@ # to the allowed method passed with the liquid_methods call # Example: # -# class SomeClass -# liquid_methods :an_allowed_method +# class SomeClass +# liquid_methods :an_allowed_method # -# def an_allowed_method -# 'this comes from an allowed method' +# def an_allowed_method +# 'this comes from an allowed method' +# end +# +# def unallowed_method +# 'this will never be an output' +# end # end -# def unallowed_method -# 'this will never be an output' -# end -# end # # if you want to extend the drop to other methods you can defines more methods # in the class ::LiquidDropClass @@ -26,31 +27,33 @@ # 'and this from another allowed method' # end # end -# end +# # # usage: -# @something = SomeClass.new +# @something = SomeClass.new # # template: -# {{something.an_allowed_method}}{{something.unallowed_method}} {{something.another_allowed_method}} +# {{something.an_allowed_method}}{{something.unallowed_method}} {{something.another_allowed_method}} # # output: -# 'this comes from an allowed method and this from another allowed method' +# 'this comes from an allowed method and this from another allowed method' # # You can also chain associations, by adding the liquid_method call in the # association models. # class Module - def liquid_methods(*allowed_methods) drop_class = eval "class #{self.to_s}::LiquidDropClass < Liquid::Drop; self; end" + define_method :to_liquid do drop_class.new(self) end + drop_class.class_eval do def initialize(object) @object = object end + allowed_methods.each do |sym| define_method sym do @object.send sym @@ -58,5 +61,4 @@ class Module end end end - end