liquid_droppable.rb 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Include this mix-in to make a class droppable to Liquid, and adjust
  2. # its behavior in Liquid by implementing its dedicated Drop class
  3. # named with a "Drop" suffix.
  4. module LiquidDroppable
  5. extend ActiveSupport::Concern
  6. class Drop < Liquid::Drop
  7. def initialize(object)
  8. @object = object
  9. end
  10. def to_s
  11. @object.to_s
  12. end
  13. def each
  14. (public_instance_methods - Drop.public_instance_methods).each { |name|
  15. yield [name, __send__(name)]
  16. }
  17. end
  18. end
  19. included do
  20. const_set :Drop,
  21. if Kernel.const_defined?(drop_name = "#{name}Drop")
  22. Kernel.const_get(drop_name)
  23. else
  24. Kernel.const_set(drop_name, Class.new(Drop))
  25. end
  26. end
  27. def to_liquid
  28. self.class::Drop.new(self)
  29. end
  30. require 'uri'
  31. class URIDrop < Drop
  32. URI::Generic::COMPONENT.each { |attr|
  33. define_method(attr) {
  34. @object.__send__(attr)
  35. }
  36. }
  37. end
  38. class ::URI::Generic
  39. def to_liquid
  40. URIDrop.new(self)
  41. end
  42. end
  43. end