Skip to content
Browse files

Remove $x global variable

  • Loading branch information...
1 parent 35f989c commit 2b1fca4e08b440f9ee1df1aa4111a33925bcfbe3 @rubys committed Feb 18, 2012
Showing with 54 additions and 69 deletions.
  1. +0 −1 README
  2. +47 −10 lib/wunderbar/builder.rb
  3. +0 −1 lib/wunderbar/environment.rb
  4. +3 −52 lib/wunderbar/html-methods.rb
  5. +4 −4 test/test_builder.rb
  6. +0 −1 test/test_html_markup.rb
View
1 README
@@ -47,7 +47,6 @@ number of other convenience methods are defined:
* $cgi - Common Gateway Interface
* $param - Access to parameters (read-only OpenStruct like interface)
* $env - Access to environment variables (read-only OpenStruct like interface)
-* $x - XmlBuilder instance
* $USER - Host user id
* $HOME - Home directory
* $SERVER- Server name
View
57 lib/wunderbar/builder.rb
@@ -1,6 +1,6 @@
-# add indented_text!, indented_data! and traceback! methods to builder
-module Builder
- class XmlMarkup
+# add indented_text!, indented_data!, system, and post? methods to builder
+module Wunderbar
+ class XmlMarkup < Builder::XmlMarkup
unless method_defined? :indented_text!
def indented_text!(text)
indented_data!(text) {|data| text! data}
@@ -32,14 +32,51 @@ def indented_data!(data, &block)
end
end
- unless method_defined? :traceback!
- def traceback!(exception=$!, klass='traceback')
- pre :class=>klass do
- text! exception.inspect
- _newline
- exception.backtrace.each {|frame| text!((' '*@indent)+frame + "\n")}
- end
+ # execute a system command, echoing stdin, stdout, and stderr
+ def system(command, opts={})
+ ::Kernel.require 'open3'
+ tag = opts[:tag] || 'pre'
+ output_class = opts[:class] || {}
+ stdin = output_class[:stdin] || '_stdin'
+ stdout = output_class[:stdout] || '_stdout'
+ stderr = output_class[:stderr] || '_stderr'
+
+ tag! tag, command, :class=>stdin unless opts[:echo] == false
+
+ ::Kernel.require 'thread'
+ semaphore = ::Mutex.new
+ ::Open3.popen3(command) do |pin, pout, perr|
+ [
+ ::Thread.new do
+ until pout.eof?
+ out_line = pout.readline.chomp
+ semaphore.synchronize { tag! tag, out_line, :class=>stdout }
+ end
+ end,
+
+ ::Thread.new do
+ until perr.eof?
+ err_line = perr.readline.chomp
+ semaphore.synchronize { tag! tag, err_line, :class=>stderr }
+ end
+ end,
+
+ ::Thread.new do
+ if opts[:stdin].respond_to? :read
+ require 'fileutils'
+ FileUtils.copy_stream opts[:stdin], pin
+ elsif opts[:stdin]
+ pin.write opts[:stdin].to_s
+ end
+ pin.close
+ end
+ ].each {|thread| thread.join}
end
end
+
+ # was this invoked via HTTP POST?
+ def post?
+ $HTTP_POST
+ end
end
end
View
1 lib/wunderbar/environment.rb
@@ -12,7 +12,6 @@
# standard objects
$cgi = CGI.new
$param = $cgi.params
-$x = Builder::XmlMarkup.new :indent => 2
# implied request types
$HTTP_GET ||= ($cgi.request_method == 'GET')
View
55 lib/wunderbar/html-methods.rb
@@ -1,49 +1,3 @@
-# execute a system command, echoing stdin, stdout, and stderr
-def $x.system(command, opts={})
- ::Kernel.require 'open3'
- output_class = opts[:class] || {}
- stdin = output_class[:stdin] || '_stdin'
- stdout = output_class[:stdout] || '_stdout'
- stderr = output_class[:stderr] || '_stderr'
-
- $x.pre command, :class=>stdin unless opts[:echo] == false
-
- ::Kernel.require 'thread'
- semaphore = ::Mutex.new
- ::Open3.popen3(command) do |pin, pout, perr|
- [
- ::Thread.new do
- until pout.eof?
- out_line = pout.readline.chomp
- semaphore.synchronize { $x.pre out_line, :class=>stdout }
- end
- end,
-
- ::Thread.new do
- until perr.eof?
- err_line = perr.readline.chomp
- semaphore.synchronize { $x.pre err_line, :class=>stderr }
- end
- end,
-
- ::Thread.new do
- if opts[:stdin].respond_to? :read
- require 'fileutils'
- FileUtils.copy_stream opts[:stdin], pin
- elsif opts[:stdin]
- pin.write opts[:stdin].to_s
- end
- pin.close
- end
- ].each {|thread| thread.join}
- end
-end
-
-# was this invoked via HTTP POST?
-def $x.post?
- $HTTP_POST
-end
-
# Wrapper class that understands HTML
class HtmlMarkup
VOID = %w(
@@ -52,10 +6,7 @@ class HtmlMarkup
)
def initialize(*args, &block)
- # as a migration aide, use the global variable, but consider that
- # to be deprecated.
- $x ||= Builder::XmlMarkup.new :indent => 2
- @x = $x
+ @x = Wunderbar::XmlMarkup.new :indent => 2
end
def html(*args, &block)
@@ -135,9 +86,9 @@ def method_missing(name, *args, &block)
end
if traceback_class
- $x.pre text, :class=>traceback_class
+ @x.pre text, :class=>traceback_class
else
- $x.pre text, :style=>traceback_style
+ @x.pre text, :style=>traceback_style
end
end
end
View
8 test/test_builder.rb
@@ -4,25 +4,25 @@
class BuilderTest < Test::Unit::TestCase
def test_empty
- x = Builder::XmlMarkup.new :indent => 2
+ x = Wunderbar::XmlMarkup.new :indent => 2
x.script { x.indented_text! '' }
assert_equal %{<script>\n</script>\n}, x.target!
end
def test_unindented_input
- x = Builder::XmlMarkup.new :indent => 2
+ x = Wunderbar::XmlMarkup.new :indent => 2
x.script { x.indented_text! "{\n x: 1\n}" }
assert_equal %{<script>\n {\n x: 1\n }\n</script>\n}, x.target!
end
def test_indented_input
- x = Builder::XmlMarkup.new :indent => 2
+ x = Wunderbar::XmlMarkup.new :indent => 2
x.script { x.indented_text! " alert('danger');" }
assert_equal %{<script>\n alert('danger');\n</script>\n}, x.target!
end
def test_exception
- x = Builder::XmlMarkup.new :indent => 2
+ x = Wunderbar::XmlMarkup.new :indent => 2
x.body do
begin
x.p { raise Exception.new('boom') }
View
1 test/test_html_markup.rb
@@ -4,7 +4,6 @@
class HtmlMarkupTest < Test::Unit::TestCase
def setup
- $x = nil # until this hack is removed html-methods.rb
@original_log_level = Wunderbar.logger.level
Wunderbar.log_level = :fatal
end

0 comments on commit 2b1fca4

Please sign in to comment.
Something went wrong with that request. Please try again.