Sha256: e811987e597f745191ebbde2fcd2a1d8351d715d9698a913489a80b08d67f564

Contents?: true

Size: 1.62 KB

Versions: 19

Compression:

Stored size: 1.62 KB

Contents

# Copyright 2007 by Domizio Demichelis
# This library is free software. It may be used, redistributed and/or modified
# under the same terms as Ruby itself
#
# This extension is used in order to expose the object of the implementing class
# to liquid as it were a Drop. It also limits the liquid-callable methods of the instance
# to the allowed method passed with the liquid_methods call
# Example:
#
# class SomeClass
#   liquid_methods :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
#
# if you want to extend the drop to other methods you can defines more methods
# in the class <YourClass>::LiquidDropClass
#
#   class SomeClass::LiquidDropClass
#     def another_allowed_method
#       'and this from another allowed method'
#     end
#   end
# end
#
# usage:
# @something = SomeClass.new
#
# template:
# {{something.an_allowed_method}}{{something.unallowed_method}} {{something.another_allowed_method}}
#
# output:
# '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
        end
      end
    end
  end

end

Version data entries

19 entries across 19 versions & 3 rubygems

Version Path
locomotivecms-liquid-4.0.0 lib/liquid/module_ex.rb
liquid-3.0.6 lib/liquid/module_ex.rb
liquid-3.0.5 lib/liquid/module_ex.rb
liquid-2.6.3 lib/liquid/module_ex.rb
liquid-3.0.4 lib/liquid/module_ex.rb
liquid-3.0.3 lib/liquid/module_ex.rb
liquid-3.0.2 lib/liquid/module_ex.rb
locomotivecms-liquid-4.0.0.alpha2 lib/liquid/module_ex.rb
locomotivecms-liquid-4.0.0.alpha1 lib/liquid/module_ex.rb
locomotivecms-liquid-4.0.0.alpha lib/liquid/module_ex.rb
liquid-3.0.1 lib/liquid/module_ex.rb
liquid-2.6.2 lib/liquid/module_ex.rb
liquid-3.0.0 lib/liquid/module_ex.rb
liquid-3.0.0.rc1 lib/liquid/module_ex.rb
liquid-2.6.1 lib/liquid/module_ex.rb
mango-0.8.0 vendor/bundler/ruby/2.1.0/gems/liquid-2.6.0/lib/liquid/module_ex.rb
liquid-2.6.0 lib/liquid/module_ex.rb
liquid-2.6.0.rc1 lib/liquid/module_ex.rb
locomotivecms-liquid-2.6.0 lib/liquid/module_ex.rb