liquid_droppable.rb 890 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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, Kernel.const_set("#{name}Drop", Class.new(Drop))
  21. end
  22. def to_liquid
  23. self.class::Drop.new(self)
  24. end
  25. require 'uri'
  26. class URIDrop < Drop
  27. URI::Generic::COMPONENT.each { |attr|
  28. define_method(attr) {
  29. @object.__send__(attr)
  30. }
  31. }
  32. end
  33. class ::URI::Generic
  34. def to_liquid
  35. URIDrop.new(self)
  36. end
  37. end
  38. end