Guest User

Untitled

a guest
Feb 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. class BlankSlate
  2. instance_methods.each { |m| undef_method m unless m =~ /^__/ or m =~ /send/ }
  3. end
  4.  
  5. class Gestalt < BlankSlate
  6. def initialize
  7. @cache = ''
  8. end
  9.  
  10. def method_missing meth, *args, &block
  11. _gestalt_handle meth, *args, &block
  12. end
  13.  
  14. def p *args, &block
  15. _gestalt_handle :p, *args, &block
  16. end
  17.  
  18. def _gestalt_handle meth, *args, &block
  19. _gestalt_build_tag(meth, *args, &block)
  20. end
  21.  
  22. # build a tag for `name`, using `args` and an optional block that
  23. # will be yielded
  24. def _gestalt_build_tag(name, args=nil, &block)
  25. @cache << "<#{name}"
  26. if block_given?
  27. @cache << ( args ? args.inject(''){ |s,v| s+%{ #{v[0]}="#{v[1]}"} } : '' )
  28. @cache << ">"
  29. tmp = yield
  30. @cache << tmp unless @cache.include? tmp.to_s
  31. @cache << "</#{name}>"
  32. else
  33. @cache << args.inject(''){ |s,v| s+%{ #{v[0]}="#{v[1]}"} } if args
  34. @cache << ' />'
  35. end
  36. @cache
  37. end
  38. end
Add Comment
Please, Sign In to add comment