Module: Wunderbar

Defined in:
lib/wunderbar/listen.rb,
lib/wunderbar/node.rb,
lib/wunderbar/rack.rb,
lib/wunderbar/asset.rb,
lib/wunderbar/logger.rb,
lib/wunderbar/script.rb,
lib/wunderbar/script.rb,
lib/wunderbar/server.rb,
lib/wunderbar/builder.rb,
lib/wunderbar/coderay.rb,
lib/wunderbar/polymer.rb,
lib/wunderbar/polymer.rb,
lib/wunderbar/sinatra.rb,
lib/wunderbar/version.rb,
lib/wunderbar/cssproxy.rb,
lib/wunderbar/markdown.rb,
lib/wunderbar/websocket.rb,
lib/wunderbar/cgi-methods.rb,
lib/wunderbar/environment.rb,
lib/wunderbar/job-control.rb,
lib/wunderbar/coffeescript.rb,
lib/wunderbar/html-methods.rb,
lib/wunderbar/installation.rb,
lib/wunderbar/jquery/filter.rb

Overview

Convert Wunderbar html syntax into JQuery calls

Defined Under Namespace

Modules: API, CompactNode, Filter, Listen, Options, ServerError, SinatraHelpers, SpacedNode, Template, VERSION Classes: Asset, BuilderBase, BuilderClass, CDATANode, CGI, Channel, ClientScriptNode, CommentNode, CssProxy, DocTypeNode, HtmlMarkup, IndentedTextNode, JsonBuilder, Node, Overridable, PreformattedNode, RackApp, Render, Scope, ScriptNode, StyleNode, TextBuilder, TextNode, XmlMarkup

Constant Summary collapse

CALLERS_TO_IGNORE =
[
  %r{/(wunderbar|webrick)/},
  %r{<internal:},
  %r{/gems/.*/lib/(builder|rack|sinatra|tilt)/}
]
@@options =
{indent: 2}
@@templates =
{}
@@files =
{}

Class Method Summary collapse

Class Method Details

.dataObject

Extract data from the script (after __END__)



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/wunderbar/installation.rb', line 3

def self.data
  data = DATA.read 

  # process argument overrides
  data.scan(/^\s*([A-Z]\w*)\s*=\s*(['"]).*\2$/).each do |name, q|
    override = ARGV.find {|arg| arg =~ /--#{name}=(.*)/i}
    data[/^\s*#{name}\s*=\s*(.*)/,1] = $1.inspect if override
  end

  data
end

.debug(*args, &block) ⇒ Object

convenience methods



46
47
48
# File 'lib/wunderbar/logger.rb', line 46

def self.debug(*args, &block)
  logger.debug(*args, &block)
end

.default_log_level=(level) ⇒ Object



33
34
35
# File 'lib/wunderbar/logger.rb', line 33

def self.default_log_level=(level)
  self.log_level = level unless @logger
end

.error(*args, &block) ⇒ Object



58
59
60
# File 'lib/wunderbar/logger.rb', line 58

def self.error(*args, &block)
  logger.error(*args, &block)
end

.fatal(*args, &block) ⇒ Object



62
63
64
# File 'lib/wunderbar/logger.rb', line 62

def self.fatal(*args, &block)
  logger.fatal(*args, &block)
end

.filesObject



22
23
24
# File 'lib/wunderbar/environment.rb', line 22

def self.files
  @@files
end

.html(*args, &block) ⇒ Object

canonical interface



213
214
215
# File 'lib/wunderbar/cgi-methods.rb', line 213

def self.html(*args, &block)
  @queue << [:html, args, block]
end

.info(*args, &block) ⇒ Object



50
51
52
# File 'lib/wunderbar/logger.rb', line 50

def self.info(*args, &block)
  logger.info(*args, &block)
end

.json(*args, &block) ⇒ Object



221
222
223
# File 'lib/wunderbar/cgi-methods.rb', line 221

def self.json(*args, &block)
  @queue << [:json, args, block]
end

.log_levelObject



37
38
39
40
41
42
43
# File 'lib/wunderbar/logger.rb', line 37

def self.log_level
  return 'debug' if logger.level == Logger::DEBUG
  return 'info'  if logger.level == Logger::INFO
  return 'warn'  if logger.level == Logger::WARN
  return 'error' if logger.level == Logger::ERROR
  return 'fatal' if logger.level == Logger::FATAL
end

.log_level=(level) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/wunderbar/logger.rb', line 19

def self.log_level=(level)
  return unless level

  case level.to_s.downcase
  when 'debug'; logger.level = Logger::DEBUG
  when 'info';  logger.level = Logger::INFO
  when 'warn';  logger.level = Logger::WARN
  when 'error'; logger.level = Logger::ERROR
  when 'fatal'; logger.level = Logger::FATAL
  else
    warn "Invalid log_level specified: #{level}"
  end
end

.loggerObject



4
5
6
7
8
9
10
11
12
13
# File 'lib/wunderbar/logger.rb', line 4

def self.logger
  @logger ||= nil
  return @logger if @logger
  @logger = Logger.new($stderr)
  @logger.level = Logger::WARN
  @logger.formatter = proc { |severity, datetime, progname, msg|
    "_#{severity} #{msg}\n"
  }
  @logger
end

.logger=(new_logger) ⇒ Object



15
16
17
# File 'lib/wunderbar/logger.rb', line 15

def self.logger= new_logger
  @logger = new_logger
end

.option(values = {}) ⇒ Object



6
7
8
9
# File 'lib/wunderbar/builder.rb', line 6

def self.option(values={})
  @@options.merge!(values)
  @@options
end

.polymer_element(*args, &block) ⇒ Object



16
17
18
19
20
21
# File 'lib/wunderbar/polymer.rb', line 16

def self.polymer_element(*args, &block)
  callback = Proc.new do |scope, args, block| 
    polymer_element(scope, *args, &block)
  end
  @queue << [callback, args, block]
end

.queueObject



229
230
231
# File 'lib/wunderbar/cgi-methods.rb', line 229

def self.queue
  @queue
end

.ruby2js(*args, &block) ⇒ Object



48
49
50
51
52
53
# File 'lib/wunderbar/script.rb', line 48

def self.ruby2js(*args, &block)
  callback = Proc.new do |scope, callback_args, callback_block| 
    ruby2js(scope, *callback_args, &callback_block)
  end
  @queue << [callback, args, block]
end

.submit(cmd = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/wunderbar/job-control.rb', line 6

def self.submit(cmd=nil)
  fork do
    # detach from tty
    Process.setsid
    fork and exit

    # clear working directory and mask
    Dir.chdir '/'
    File.umask 0000

    # close open files
    STDIN.reopen '/dev/null'
    if RbConfig::CONFIG['host_os'] =~ /darwin|mac os/
      STDOUT.reopen '/dev/null'
    else
      STDOUT.reopen '/dev/null', 'a'
    end
    STDERR.reopen STDOUT

    # clear environment of cgi cruft
    require 'cgi'
    ENV.keys.select {|key| key =~ /^HTTP_/}.each do |key|
      ENV.delete key
    end
    ::CGI::QueryExtension.public_instance_methods.each do |method|
      ENV.delete method.to_s.upcase
    end

    # run cmd and/or block
    system({'USER' => $USER, 'HOME' => $HOME}, cmd) if cmd
    yield if block_given?
  end
end

.templatesObject



18
19
20
# File 'lib/wunderbar/environment.rb', line 18

def self.templates
  @@templates
end

.text(*args, &block) ⇒ Object



225
226
227
# File 'lib/wunderbar/cgi-methods.rb', line 225

def self.text(*args, &block)
  @queue << [:text, args, block]
end

.warn(*args, &block) ⇒ Object



54
55
56
# File 'lib/wunderbar/logger.rb', line 54

def self.warn(*args, &block)
  logger.warn(*args, &block)
end

.websocket(opts = {}, &block) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/wunderbar/websocket.rb', line 171

def self.websocket(opts={}, &block)
  opts = {:port => opts} if Fixnum === opts
  port = opts[:port]
  buffer = opts.fetch(:buffer,1)

  if not port
    socket = TCPServer.new(0)
    port = Socket.unpack_sockaddr_in(socket.getsockname).first
    socket.close
  end

  sock1 = nil

  proc = Proc.new do
    begin
	channel = Wunderbar::Channel.new(port, buffer, opts[:locals])
	if sock1
 sock1.send('x',0)
 sock1.close
	end
	channel.instance_eval(&block)
    rescue Exception => exception
	channel._ :type=>:stderr, :line=>exception.inspect
	exception.backtrace.each do |frame| 
 next if Wunderbar::CALLERS_TO_IGNORE.any? {|re| frame =~ re}
 channel._ :type=>:stderr, :line=>"  #{frame}"
	end
    ensure
	if channel
 channel.complete = true
 sleep 5
 sleep 60 unless channel.connected or opts[:sync]
 channel.close
	end
    end
  end

  if opts[:sync]
    instance_eval &proc
  else
    sock1, sock2 = UNIXSocket.pair
    submit(&proc)
    sleep 0.3 while sock2.recv(1) != 'x'
    sock2.close
  end

  port
end

.xhtml(*args, &block) ⇒ Object



217
218
219
# File 'lib/wunderbar/cgi-methods.rb', line 217

def self.xhtml(*args, &block)
  @queue << [:xhtml, args, block]
end