Class: RubyTextmate

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

Class Method Summary collapse

Class Method Details

.open(thing, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ruby_textmate.rb', line 10

def open(thing, options = {})
  if thing
    if thing =~ /^http\:\/\//i
      open_url(thing, options)
    elsif thing !~ /[\s]/ && File.exist?(thing)
      open_file(thing, options)
    else
      open_text(thing, options)
    end
  end
end

.open_file(filename, options = {}) ⇒ Object



22
23
24
25
26
# File 'lib/ruby_textmate.rb', line 22

def open_file(filename, options = {})
  mate_options = build_mate_options(options)
  mate_options << filename
  system("mate", *mate_options)
end

.open_text(text, options = {}) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/ruby_textmate.rb', line 41

def open_text(text, options = {})
  Open3.popen3("mate #{build_mate_options(options).join(' ')}") do |stdin, stdout, stderr|
    stdin.write(text)
    stdin.close_write
    stdout.read if options[:wait]
  end
end

.open_url(url, options = {}) ⇒ Object



49
50
51
52
53
# File 'lib/ruby_textmate.rb', line 49

def open_url(url, options = {})
  Kernel.open(url) do |f|
    open_text(f.read, options)
  end
end

.with_tempfile(init_text, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ruby_textmate.rb', line 28

def with_tempfile(init_text, options = {})
  t = Tempfile.open('textmate')
  filename = t.path
  t.write(init_text)
  t.close
  open_file(filename, options.merge(:wait => true))
  t.open
  result = t.read
  t.close
  t.delete
  result
end