Class: Lyndon::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/lyndon/runtime.rb

Instance Method Summary collapse

Constructor Details

#initialize(dom = nil) ⇒ Runtime

Returns a new instance of Runtime.



5
6
7
8
9
10
11
12
13
14
# File 'lib/lyndon/runtime.rb', line 5

def initialize(dom = nil)
  @webView = WebView.new
  @webView.setFrameLoadDelegate(Delegate.new)

  @scripter = @webView.windowScriptObject
  @scripter.setValue(Ruby.new, forKey:"Ruby")

  load_dom(dom) if dom
  eval_file File.dirname(__FILE__) + '/../js/lyndon'
end

Instance Method Details

#eval(js) ⇒ Object



16
17
18
# File 'lib/lyndon/runtime.rb', line 16

def eval(js)
  @scripter.evaluateWebScript(js)
end

#eval_file(file) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/lyndon/runtime.rb', line 20

def eval_file(file)
  if File.exists? file = File.expand_path(file.to_s)
    eval File.read(file)
  elsif File.exists? file + '.js'
    eval File.read(file + '.js')
  end
end

#html_sourceObject



38
39
40
# File 'lib/lyndon/runtime.rb', line 38

def html_source
  '<html>'+eval("document.documentElement.innerHTML")+'</html>'
end

#load_dom(dom, base_url = nil) ⇒ Object



28
29
30
31
32
# File 'lib/lyndon/runtime.rb', line 28

def load_dom(dom, base_url = nil)
  @dom = File.exists?(dom) ? File.read(dom) : dom
  @webView.mainFrame.loadHTMLString(@dom, baseURL:base_url)
  NSApplication.sharedApplication.run
end

#to_sObject



34
35
36
# File 'lib/lyndon/runtime.rb', line 34

def to_s
  @dom ? html_source : super
end