Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class String
- def >>(str)
- str[0,0] = self
- end
- end
- class BlankSlate
- instance_methods.each { |m| undef_method m unless m =~ /^__/ or m =~ /send/ }
- end
- class Remold < BlankSlate
- def initialize
- @cache = ''
- end
- def method_missing meth, *args, &block
- _remold_handle meth, *args, &block
- end
- def p *args, &block
- _remold_handle :p, *args, &block
- end
- def _remold_handle meth, *args, &block
- _remold_build_tag(meth, *args, &block)
- end
- # build a tag for `name`, using `args` and an optional block that
- # will be yielded
- def _remold_build_tag(name, args=nil, &block)
- @cache << "<#{name}"
- if block_given?
- @cache << ( args ? args.inject(''){ |s,v| s+%{ #{v[0]}="#{v[1]}"} } : '' )
- @cache << ">"
- yield
- @cache << "</#{name}>"
- else
- @cache << args.inject(''){ |s,v| s+%{ #{v[0]}="#{v[1]}"} } if args
- @cache << ' />'
- end
- @cache
- end
- def d *args
- Kernel.p args
- end
- end
- class Template < Remold
- def simple
- p
- end
- def dual
- p
- br
- end
- def nested
- p {
- br
- }
- end
- def nested_and_dual
- p {
- br
- br
- }
- end
- def dual_nested
- p {
- br
- }
- p {
- br
- }
- end
- def nested_nested
- p {
- p {
- br
- }
- }
- end
- end
- require 'micro/test'
- {
- :simple => '<p />',
- :dual => '<p /><br />',
- :nested => '<p><br /></p>',
- :nested_and_dual => '<p><br /><br /></p>',
- :dual_nested => '<p><br /></p><p><br /></p>',
- :nested_nested => '<p><p><br /></p></p>',
- }.test do |t|
- template = Template.new
- template.send(t)
- end
Add Comment
Please, Sign In to add comment