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 2.16 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. tmp = yield
  36. @cache << tmp unless @cache.include? tmp.to_s
  37. @cache << "</#{name}>"
  38. else
  39. @cache << args.inject(''){ |s,v| s+%{ #{v[0]}="#{v[1]}"} } if args
  40. @cache << ' />'
  41. end
  42. @cache
  43. end
  44.  
  45. def d *args
  46. Kernel.p args
  47. end
  48. end
  49.  
  50. class Template < Remold
  51. def simple
  52. p
  53. end
  54.  
  55. def dual
  56. p
  57. br
  58. end
  59.  
  60. def nested
  61. p {
  62. br
  63. }
  64. end
  65.  
  66. def nested_and_dual
  67. p {
  68. br
  69. br
  70. }
  71. end
  72.  
  73. def dual_nested
  74. p {
  75. br
  76. }
  77. p {
  78. br
  79. }
  80. end
  81.  
  82. def nested_nested
  83. p {
  84. p {
  85. br
  86. }
  87. }
  88. end
  89.  
  90. def html_preset
  91. html{
  92. head{
  93. title{'Hello there'}
  94. meta :name => 'foo', :content => 'bar'
  95. }
  96. body{
  97. h1{'Hello World!'}
  98. div(:class => 'foo'){'More world here'}
  99. }
  100. }
  101. end
  102. end
  103.  
  104. require 'micro/test'
  105.  
  106. {
  107. :simple => '<p />',
  108. :dual => '<p /><br />',
  109. :nested => '<p><br /></p>',
  110. :nested_and_dual => '<p><br /><br /></p>',
  111. :dual_nested => '<p><br /></p><p><br /></p>',
  112. :nested_nested => '<p><p><br /></p></p>',
  113. :html_preset => '<html><head><title>Hello there</title><meta name="foo" content="bar" /></head><body><h1>Hello World!</h1><div class="foo">More world here</div></body></html>',
  114. }.test do |t|
  115. template = Template.new
  116. template.send(t)
  117. end
Add Comment
Please, Sign In to add comment