Guest User

Untitled

a guest
Feb 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. class String
  2. def >>(str)
  3. str[0,0] = self
  4. end
  5. end
  6.  
  7. class BlankSlate
  8. instance_methods.each { |m| undef_method m unless m =~ /^__/ or m =~ /send/ }
  9. end
  10.  
  11. class Remold < BlankSlate
  12. def initialize
  13. @cache = ''
  14. end
  15.  
  16. def method_missing meth, *args, &block
  17. _remold_handle meth, *args, &block
  18. end
  19.  
  20. def p *args, &block
  21. _remold_handle :p, *args, &block
  22. end
  23.  
  24. def _remold_handle meth, *args, &block
  25. _remold_build_tag(meth, *args, &block)
  26. end
  27.  
  28. # build a tag for `name`, using `args` and an optional block that
  29. # will be yielded
  30. def _remold_build_tag(name, args=nil, &block)
  31. @cache << "<#{name}"
  32. if block_given?
  33. @cache << ( args ? args.inject(''){ |s,v| s+%{ #{v[0]}="#{v[1]}"} } : '' )
  34. @cache << ">"
  35. yield
  36. @cache << "</#{name}>"
  37. else
  38. @cache << args.inject(''){ |s,v| s+%{ #{v[0]}="#{v[1]}"} } if args
  39. @cache << ' />'
  40. end
  41. @cache
  42. end
  43.  
  44. def d *args
  45. Kernel.p args
  46. end
  47. end
  48.  
  49. class Template < Remold
  50. def simple
  51. p
  52. end
  53.  
  54. def dual
  55. p
  56. br
  57. end
  58.  
  59. def nested
  60. p {
  61. br
  62. }
  63. end
  64.  
  65. def nested_and_dual
  66. p {
  67. br
  68. br
  69. }
  70. end
  71.  
  72. def dual_nested
  73. p {
  74. br
  75. }
  76. p {
  77. br
  78. }
  79. end
  80.  
  81. def nested_nested
  82. p {
  83. p {
  84. br
  85. }
  86. }
  87. end
  88. end
  89.  
  90. require 'micro/test'
  91.  
  92. {
  93. :simple => '<p />',
  94. :dual => '<p /><br />',
  95. :nested => '<p><br /></p>',
  96. :nested_and_dual => '<p><br /><br /></p>',
  97. :dual_nested => '<p><br /></p><p><br /></p>',
  98. :nested_nested => '<p><p><br /></p></p>',
  99. }.test do |t|
  100. template = Template.new
  101. template.send(t)
  102. end
Add Comment
Please, Sign In to add comment